├── .babelrc ├── .eslintrc ├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── README.md ├── dist ├── rx-store.amd.js ├── rx-store.browser.js ├── rx-store.browser.min.js ├── rx-store.cjs.js ├── rx-store.es-modules.js ├── rx-store.umd.js └── rx-store.umd.min.js ├── package.json ├── rollup.config.js ├── src └── index.js └── test ├── index_test.js └── mocha.opts /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | presets: ["es2015", "stage-2"] 3 | } 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es6": true, 5 | "mocha": true 6 | }, 7 | 8 | "parser": "babel-eslint", 9 | 10 | "ecmaFeatures": { 11 | "modules": true 12 | }, 13 | 14 | "globals": { 15 | "assert": false 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | script: npm run lint && npm test 3 | sudo: false 4 | node_js: 5 | - "6.0.0" 6 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. 6 | 7 | Examples of unacceptable behavior by participants include: 8 | 9 | * The use of sexualized language or imagery 10 | * Personal attacks 11 | * Trolling or insulting/derogatory comments 12 | * Public or private harassment 13 | * Publishing other's private information, such as physical or electronic 14 | addresses, without explicit permission 15 | * Other unethical or unprofessional conduct 16 | 17 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 18 | 19 | By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. 20 | 21 | This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. 22 | 23 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 24 | 25 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.3.0, available at [http://contributor-covenant.org/version/1/3/0/][version] 26 | 27 | [homepage]: http://contributor-covenant.org 28 | [version]: http://contributor-covenant.org/version/1/3/0/ 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![npm version](https://badge.fury.io/js/rx-store.svg)](http://badge.fury.io/js/rx-store) 2 | [![Build Status](https://secure.travis-ci.org/jdlehman/rx-store.svg?branch=master)](http://travis-ci.org/jdlehman/rx-store) 3 | [![Dependency Status](https://david-dm.org/jdlehman/rx-store.svg)](https://david-dm.org/jdlehman/rx-store) 4 | 5 | # rx-store 6 | 7 | `rx-store` is a reactive solution for managing state. It is framework and view agnostic, though it can be used as the basis for a Flux pattern. 8 | 9 | ## Installation 10 | 11 | `npm install --save rx-store` 12 | 13 | ## Usage 14 | 15 | ### Import library 16 | 17 | #### ES6 18 | 19 | ```js 20 | import {createRxStore} from 'rx-store'; 21 | ``` 22 | 23 | #### ES5 with modules 24 | 25 | ```js 26 | var createRxStore = require('rx-store').createRxStore; 27 | ``` 28 | 29 | #### ES5 30 | 31 | ```html 32 | 33 | // window.RxStore.createRxStore 34 | ``` 35 | 36 | ### Create a store 37 | 38 | `createRxStore` takes a [reduce function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) as an argument as well as an optional initial state. If not specified the initial state starts out as undefined. Note that the state can be any valid JS type (object, array, number, etc.). 39 | 40 | ```js 41 | function sum(previousState, nextState) { 42 | return previousState + nextState; 43 | } 44 | var initialState = 0; 45 | var store = createRxStore(sum, initialState); 46 | ``` 47 | 48 | ### Subscribe to State Changes 49 | 50 | ```js 51 | store.subscribe(function(state) { 52 | // do stuff with state 53 | }); 54 | 55 | // which is equivalent to 56 | 57 | store.state$.subscribe(function(state) { 58 | // do stuff with state 59 | }); 60 | ``` 61 | 62 | ### Modify State 63 | 64 | ```js 65 | store.subscribe(function(state) { 66 | console.log('State: ' + state); 67 | }); 68 | 69 | var action = 4; 70 | store.dispatch(action); 71 | 72 | // logs: 73 | // State: 0 74 | // State: 4 75 | ``` 76 | 77 | ### Modify State from Browser Event 78 | 79 | ```js 80 | var addTwo$ = Rx.Observable.fromEvent(btnNode, 'click').map(e => { 81 | return 2; 82 | }); 83 | 84 | // send each new action to the store 85 | addTwo$.subscribe(function(action) { 86 | store.dispatch(action); 87 | }); 88 | ``` 89 | 90 | ## Using as a Flux Pattern 91 | 92 | ```js 93 | function reducer(state, action) { 94 | switch (action.type) { 95 | case 'ADD': 96 | var addState = state; 97 | addState.count += action.payload; 98 | return addState; 99 | case 'ACTION_TWO': 100 | return actionTwo(state); 101 | default: 102 | return state; 103 | } 104 | } 105 | var initialState = {count: 0, somethingElse: 'data'}; 106 | var store = createRxStore(reducer, initialState); 107 | 108 | function add(data) { 109 | return { 110 | type: 'ADD', 111 | payload: data 112 | }; 113 | } 114 | 115 | store.subscribe(function(data) { 116 | console.log(data); 117 | }); 118 | 119 | var addAction = add(4); 120 | var addAction2 = add(-1); 121 | store.dispatch(addAction); 122 | store.dispatch(addAction2); 123 | // logs: 124 | // {count: 0, somethingElse: 'data'} 125 | // {count: 4, somethingElse: 'data'); 126 | // {count: 3, somethingElse: 'data'); 127 | ``` 128 | 129 | ## Using with React Views 130 | 131 | ```js 132 | class MyComponent extends React.Component { 133 | componentDidMount() { 134 | this.countSubscription = this.props.countStream.subscribe((count) => { 135 | this.setState({count: count}); 136 | }); 137 | } 138 | 139 | componentWillUnmount() { 140 | this.countSubscription.unsubscribe(); 141 | } 142 | 143 | render() { 144 | return
{this.state.count}
; 145 | } 146 | } 147 | 148 | var count$ = store.state$.map(function(data) { 149 | return data.count; 150 | }); 151 | ReactDOM.render(, domNode); 152 | ``` 153 | -------------------------------------------------------------------------------- /dist/rx-store.amd.js: -------------------------------------------------------------------------------- 1 | define('rx-store', ['exports'], function (exports) { 'use strict'; 2 | 3 | var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; 4 | 5 | 6 | 7 | 8 | 9 | function createCommonjsModule(fn, module) { 10 | return module = { exports: {} }, fn(module, module.exports), module.exports; 11 | } 12 | 13 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { 14 | return typeof obj; 15 | } : function (obj) { 16 | return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; 17 | }; 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | var get$1 = function get$1(object, property, receiver) { 34 | if (object === null) object = Function.prototype; 35 | var desc = Object.getOwnPropertyDescriptor(object, property); 36 | 37 | if (desc === undefined) { 38 | var parent = Object.getPrototypeOf(object); 39 | 40 | if (parent === null) { 41 | return undefined; 42 | } else { 43 | return get$1(parent, property, receiver); 44 | } 45 | } else if ("value" in desc) { 46 | return desc.value; 47 | } else { 48 | var getter = desc.get; 49 | 50 | if (getter === undefined) { 51 | return undefined; 52 | } 53 | 54 | return getter.call(receiver); 55 | } 56 | }; 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | var set = function set(object, property, value, receiver) { 75 | var desc = Object.getOwnPropertyDescriptor(object, property); 76 | 77 | if (desc === undefined) { 78 | var parent = Object.getPrototypeOf(object); 79 | 80 | if (parent !== null) { 81 | set(parent, property, value, receiver); 82 | } 83 | } else if ("value" in desc && desc.writable) { 84 | desc.value = value; 85 | } else { 86 | var setter = desc.set; 87 | 88 | if (setter !== undefined) { 89 | setter.call(receiver, value); 90 | } 91 | } 92 | 93 | return value; 94 | }; 95 | 96 | var root = createCommonjsModule(function (module, exports) { 97 | "use strict"; 98 | /** 99 | * window: browser in DOM main thread 100 | * self: browser in WebWorker 101 | * global: Node.js/other 102 | */ 103 | 104 | exports.root = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) == 'object' && window.window === window && window || (typeof self === 'undefined' ? 'undefined' : _typeof(self)) == 'object' && self.self === self && self || _typeof(commonjsGlobal) == 'object' && commonjsGlobal.global === commonjsGlobal && commonjsGlobal; 105 | if (!exports.root) { 106 | throw new Error('RxJS could not find any global context (window, self, global)'); 107 | } 108 | }); 109 | 110 | function isFunction(x) { 111 | return typeof x === 'function'; 112 | } 113 | var isFunction_2 = isFunction; 114 | 115 | var isFunction_1$1 = { 116 | isFunction: isFunction_2 117 | }; 118 | 119 | var isArray_1$1 = Array.isArray || function (x) { 120 | return x && typeof x.length === 'number'; 121 | }; 122 | 123 | var isArray = { 124 | isArray: isArray_1$1 125 | }; 126 | 127 | function isObject(x) { 128 | return x != null && (typeof x === "undefined" ? "undefined" : _typeof(x)) === 'object'; 129 | } 130 | var isObject_2 = isObject; 131 | 132 | var isObject_1$1 = { 133 | isObject: isObject_2 134 | }; 135 | 136 | // typeof any so that it we don't have to cast when comparing a result to the error object 137 | 138 | var errorObject_1$2 = { e: {} }; 139 | 140 | var errorObject = { 141 | errorObject: errorObject_1$2 142 | }; 143 | 144 | var errorObject_1$1 = errorObject; 145 | var tryCatchTarget; 146 | function tryCatcher() { 147 | try { 148 | return tryCatchTarget.apply(this, arguments); 149 | } catch (e) { 150 | errorObject_1$1.errorObject.e = e; 151 | return errorObject_1$1.errorObject; 152 | } 153 | } 154 | function tryCatch(fn) { 155 | tryCatchTarget = fn; 156 | return tryCatcher; 157 | } 158 | var tryCatch_2 = tryCatch; 159 | 160 | 161 | var tryCatch_1$1 = { 162 | tryCatch: tryCatch_2 163 | }; 164 | 165 | var __extends$3 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 166 | for (var p in b) { 167 | if (b.hasOwnProperty(p)) d[p] = b[p]; 168 | }function __() { 169 | this.constructor = d; 170 | } 171 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 172 | }; 173 | /** 174 | * An error thrown when one or more errors have occurred during the 175 | * `unsubscribe` of a {@link Subscription}. 176 | */ 177 | var UnsubscriptionError = function (_super) { 178 | __extends$3(UnsubscriptionError, _super); 179 | function UnsubscriptionError(errors) { 180 | _super.call(this); 181 | this.errors = errors; 182 | var err = Error.call(this, errors ? errors.length + " errors occurred during unsubscription:\n " + errors.map(function (err, i) { 183 | return i + 1 + ") " + err.toString(); 184 | }).join('\n ') : ''); 185 | this.name = err.name = 'UnsubscriptionError'; 186 | this.stack = err.stack; 187 | this.message = err.message; 188 | } 189 | return UnsubscriptionError; 190 | }(Error); 191 | var UnsubscriptionError_2 = UnsubscriptionError; 192 | 193 | var UnsubscriptionError_1$1 = { 194 | UnsubscriptionError: UnsubscriptionError_2 195 | }; 196 | 197 | var isArray_1 = isArray; 198 | var isObject_1 = isObject_1$1; 199 | var isFunction_1$3 = isFunction_1$1; 200 | var tryCatch_1 = tryCatch_1$1; 201 | var errorObject_1 = errorObject; 202 | var UnsubscriptionError_1 = UnsubscriptionError_1$1; 203 | /** 204 | * Represents a disposable resource, such as the execution of an Observable. A 205 | * Subscription has one important method, `unsubscribe`, that takes no argument 206 | * and just disposes the resource held by the subscription. 207 | * 208 | * Additionally, subscriptions may be grouped together through the `add()` 209 | * method, which will attach a child Subscription to the current Subscription. 210 | * When a Subscription is unsubscribed, all its children (and its grandchildren) 211 | * will be unsubscribed as well. 212 | * 213 | * @class Subscription 214 | */ 215 | var Subscription = function () { 216 | /** 217 | * @param {function(): void} [unsubscribe] A function describing how to 218 | * perform the disposal of resources when the `unsubscribe` method is called. 219 | */ 220 | function Subscription(unsubscribe) { 221 | /** 222 | * A flag to indicate whether this Subscription has already been unsubscribed. 223 | * @type {boolean} 224 | */ 225 | this.closed = false; 226 | if (unsubscribe) { 227 | this._unsubscribe = unsubscribe; 228 | } 229 | } 230 | /** 231 | * Disposes the resources held by the subscription. May, for instance, cancel 232 | * an ongoing Observable execution or cancel any other type of work that 233 | * started when the Subscription was created. 234 | * @return {void} 235 | */ 236 | Subscription.prototype.unsubscribe = function () { 237 | var hasErrors = false; 238 | var errors; 239 | if (this.closed) { 240 | return; 241 | } 242 | this.closed = true; 243 | var _a = this, 244 | _unsubscribe = _a._unsubscribe, 245 | _subscriptions = _a._subscriptions; 246 | this._subscriptions = null; 247 | if (isFunction_1$3.isFunction(_unsubscribe)) { 248 | var trial = tryCatch_1.tryCatch(_unsubscribe).call(this); 249 | if (trial === errorObject_1.errorObject) { 250 | hasErrors = true; 251 | (errors = errors || []).push(errorObject_1.errorObject.e); 252 | } 253 | } 254 | if (isArray_1.isArray(_subscriptions)) { 255 | var index = -1; 256 | var len = _subscriptions.length; 257 | while (++index < len) { 258 | var sub = _subscriptions[index]; 259 | if (isObject_1.isObject(sub)) { 260 | var trial = tryCatch_1.tryCatch(sub.unsubscribe).call(sub); 261 | if (trial === errorObject_1.errorObject) { 262 | hasErrors = true; 263 | errors = errors || []; 264 | var err = errorObject_1.errorObject.e; 265 | if (err instanceof UnsubscriptionError_1.UnsubscriptionError) { 266 | errors = errors.concat(err.errors); 267 | } else { 268 | errors.push(err); 269 | } 270 | } 271 | } 272 | } 273 | } 274 | if (hasErrors) { 275 | throw new UnsubscriptionError_1.UnsubscriptionError(errors); 276 | } 277 | }; 278 | /** 279 | * Adds a tear down to be called during the unsubscribe() of this 280 | * Subscription. 281 | * 282 | * If the tear down being added is a subscription that is already 283 | * unsubscribed, is the same reference `add` is being called on, or is 284 | * `Subscription.EMPTY`, it will not be added. 285 | * 286 | * If this subscription is already in an `closed` state, the passed 287 | * tear down logic will be executed immediately. 288 | * 289 | * @param {TeardownLogic} teardown The additional logic to execute on 290 | * teardown. 291 | * @return {Subscription} Returns the Subscription used or created to be 292 | * added to the inner subscriptions list. This Subscription can be used with 293 | * `remove()` to remove the passed teardown logic from the inner subscriptions 294 | * list. 295 | */ 296 | Subscription.prototype.add = function (teardown) { 297 | if (!teardown || teardown === Subscription.EMPTY) { 298 | return Subscription.EMPTY; 299 | } 300 | if (teardown === this) { 301 | return this; 302 | } 303 | var sub = teardown; 304 | switch (typeof teardown === 'undefined' ? 'undefined' : _typeof(teardown)) { 305 | case 'function': 306 | sub = new Subscription(teardown); 307 | case 'object': 308 | if (sub.closed || typeof sub.unsubscribe !== 'function') { 309 | break; 310 | } else if (this.closed) { 311 | sub.unsubscribe(); 312 | } else { 313 | (this._subscriptions || (this._subscriptions = [])).push(sub); 314 | } 315 | break; 316 | default: 317 | throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.'); 318 | } 319 | return sub; 320 | }; 321 | /** 322 | * Removes a Subscription from the internal list of subscriptions that will 323 | * unsubscribe during the unsubscribe process of this Subscription. 324 | * @param {Subscription} subscription The subscription to remove. 325 | * @return {void} 326 | */ 327 | Subscription.prototype.remove = function (subscription) { 328 | // HACK: This might be redundant because of the logic in `add()` 329 | if (subscription == null || subscription === this || subscription === Subscription.EMPTY) { 330 | return; 331 | } 332 | var subscriptions = this._subscriptions; 333 | if (subscriptions) { 334 | var subscriptionIndex = subscriptions.indexOf(subscription); 335 | if (subscriptionIndex !== -1) { 336 | subscriptions.splice(subscriptionIndex, 1); 337 | } 338 | } 339 | }; 340 | Subscription.EMPTY = function (empty) { 341 | empty.closed = true; 342 | return empty; 343 | }(new Subscription()); 344 | return Subscription; 345 | }(); 346 | var Subscription_2 = Subscription; 347 | 348 | var Subscription_1$2 = { 349 | Subscription: Subscription_2 350 | }; 351 | 352 | var empty = { 353 | closed: true, 354 | next: function next(value) {}, 355 | error: function error(err) { 356 | throw err; 357 | }, 358 | complete: function complete() {} 359 | }; 360 | 361 | var Observer = { 362 | empty: empty 363 | }; 364 | 365 | var root_1$2 = root; 366 | var _Symbol = root_1$2.root.Symbol; 367 | var $$rxSubscriber = typeof _Symbol === 'function' && typeof _Symbol.for === 'function' ? _Symbol.for('rxSubscriber') : '@@rxSubscriber'; 368 | 369 | var rxSubscriber = { 370 | $$rxSubscriber: $$rxSubscriber 371 | }; 372 | 373 | var __extends$2 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 374 | for (var p in b) { 375 | if (b.hasOwnProperty(p)) d[p] = b[p]; 376 | }function __() { 377 | this.constructor = d; 378 | } 379 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 380 | }; 381 | var isFunction_1 = isFunction_1$1; 382 | var Subscription_1$1 = Subscription_1$2; 383 | var Observer_1$1 = Observer; 384 | var rxSubscriber_1$2 = rxSubscriber; 385 | /** 386 | * Implements the {@link Observer} interface and extends the 387 | * {@link Subscription} class. While the {@link Observer} is the public API for 388 | * consuming the values of an {@link Observable}, all Observers get converted to 389 | * a Subscriber, in order to provide Subscription-like capabilities such as 390 | * `unsubscribe`. Subscriber is a common type in RxJS, and crucial for 391 | * implementing operators, but it is rarely used as a public API. 392 | * 393 | * @class Subscriber 394 | */ 395 | var Subscriber = function (_super) { 396 | __extends$2(Subscriber, _super); 397 | /** 398 | * @param {Observer|function(value: T): void} [destinationOrNext] A partially 399 | * defined Observer or a `next` callback function. 400 | * @param {function(e: ?any): void} [error] The `error` callback of an 401 | * Observer. 402 | * @param {function(): void} [complete] The `complete` callback of an 403 | * Observer. 404 | */ 405 | function Subscriber(destinationOrNext, error, complete) { 406 | _super.call(this); 407 | this.syncErrorValue = null; 408 | this.syncErrorThrown = false; 409 | this.syncErrorThrowable = false; 410 | this.isStopped = false; 411 | switch (arguments.length) { 412 | case 0: 413 | this.destination = Observer_1$1.empty; 414 | break; 415 | case 1: 416 | if (!destinationOrNext) { 417 | this.destination = Observer_1$1.empty; 418 | break; 419 | } 420 | if ((typeof destinationOrNext === 'undefined' ? 'undefined' : _typeof(destinationOrNext)) === 'object') { 421 | if (destinationOrNext instanceof Subscriber) { 422 | this.destination = destinationOrNext; 423 | this.destination.add(this); 424 | } else { 425 | this.syncErrorThrowable = true; 426 | this.destination = new SafeSubscriber(this, destinationOrNext); 427 | } 428 | break; 429 | } 430 | default: 431 | this.syncErrorThrowable = true; 432 | this.destination = new SafeSubscriber(this, destinationOrNext, error, complete); 433 | break; 434 | } 435 | } 436 | Subscriber.prototype[rxSubscriber_1$2.$$rxSubscriber] = function () { 437 | return this; 438 | }; 439 | /** 440 | * A static factory for a Subscriber, given a (potentially partial) definition 441 | * of an Observer. 442 | * @param {function(x: ?T): void} [next] The `next` callback of an Observer. 443 | * @param {function(e: ?any): void} [error] The `error` callback of an 444 | * Observer. 445 | * @param {function(): void} [complete] The `complete` callback of an 446 | * Observer. 447 | * @return {Subscriber} A Subscriber wrapping the (partially defined) 448 | * Observer represented by the given arguments. 449 | */ 450 | Subscriber.create = function (next, error, complete) { 451 | var subscriber = new Subscriber(next, error, complete); 452 | subscriber.syncErrorThrowable = false; 453 | return subscriber; 454 | }; 455 | /** 456 | * The {@link Observer} callback to receive notifications of type `next` from 457 | * the Observable, with a value. The Observable may call this method 0 or more 458 | * times. 459 | * @param {T} [value] The `next` value. 460 | * @return {void} 461 | */ 462 | Subscriber.prototype.next = function (value) { 463 | if (!this.isStopped) { 464 | this._next(value); 465 | } 466 | }; 467 | /** 468 | * The {@link Observer} callback to receive notifications of type `error` from 469 | * the Observable, with an attached {@link Error}. Notifies the Observer that 470 | * the Observable has experienced an error condition. 471 | * @param {any} [err] The `error` exception. 472 | * @return {void} 473 | */ 474 | Subscriber.prototype.error = function (err) { 475 | if (!this.isStopped) { 476 | this.isStopped = true; 477 | this._error(err); 478 | } 479 | }; 480 | /** 481 | * The {@link Observer} callback to receive a valueless notification of type 482 | * `complete` from the Observable. Notifies the Observer that the Observable 483 | * has finished sending push-based notifications. 484 | * @return {void} 485 | */ 486 | Subscriber.prototype.complete = function () { 487 | if (!this.isStopped) { 488 | this.isStopped = true; 489 | this._complete(); 490 | } 491 | }; 492 | Subscriber.prototype.unsubscribe = function () { 493 | if (this.closed) { 494 | return; 495 | } 496 | this.isStopped = true; 497 | _super.prototype.unsubscribe.call(this); 498 | }; 499 | Subscriber.prototype._next = function (value) { 500 | this.destination.next(value); 501 | }; 502 | Subscriber.prototype._error = function (err) { 503 | this.destination.error(err); 504 | this.unsubscribe(); 505 | }; 506 | Subscriber.prototype._complete = function () { 507 | this.destination.complete(); 508 | this.unsubscribe(); 509 | }; 510 | return Subscriber; 511 | }(Subscription_1$1.Subscription); 512 | var Subscriber_2 = Subscriber; 513 | /** 514 | * We need this JSDoc comment for affecting ESDoc. 515 | * @ignore 516 | * @extends {Ignored} 517 | */ 518 | var SafeSubscriber = function (_super) { 519 | __extends$2(SafeSubscriber, _super); 520 | function SafeSubscriber(_parent, observerOrNext, error, complete) { 521 | _super.call(this); 522 | this._parent = _parent; 523 | var next; 524 | var context = this; 525 | if (isFunction_1.isFunction(observerOrNext)) { 526 | next = observerOrNext; 527 | } else if (observerOrNext) { 528 | context = observerOrNext; 529 | next = observerOrNext.next; 530 | error = observerOrNext.error; 531 | complete = observerOrNext.complete; 532 | if (isFunction_1.isFunction(context.unsubscribe)) { 533 | this.add(context.unsubscribe.bind(context)); 534 | } 535 | context.unsubscribe = this.unsubscribe.bind(this); 536 | } 537 | this._context = context; 538 | this._next = next; 539 | this._error = error; 540 | this._complete = complete; 541 | } 542 | SafeSubscriber.prototype.next = function (value) { 543 | if (!this.isStopped && this._next) { 544 | var _parent = this._parent; 545 | if (!_parent.syncErrorThrowable) { 546 | this.__tryOrUnsub(this._next, value); 547 | } else if (this.__tryOrSetError(_parent, this._next, value)) { 548 | this.unsubscribe(); 549 | } 550 | } 551 | }; 552 | SafeSubscriber.prototype.error = function (err) { 553 | if (!this.isStopped) { 554 | var _parent = this._parent; 555 | if (this._error) { 556 | if (!_parent.syncErrorThrowable) { 557 | this.__tryOrUnsub(this._error, err); 558 | this.unsubscribe(); 559 | } else { 560 | this.__tryOrSetError(_parent, this._error, err); 561 | this.unsubscribe(); 562 | } 563 | } else if (!_parent.syncErrorThrowable) { 564 | this.unsubscribe(); 565 | throw err; 566 | } else { 567 | _parent.syncErrorValue = err; 568 | _parent.syncErrorThrown = true; 569 | this.unsubscribe(); 570 | } 571 | } 572 | }; 573 | SafeSubscriber.prototype.complete = function () { 574 | if (!this.isStopped) { 575 | var _parent = this._parent; 576 | if (this._complete) { 577 | if (!_parent.syncErrorThrowable) { 578 | this.__tryOrUnsub(this._complete); 579 | this.unsubscribe(); 580 | } else { 581 | this.__tryOrSetError(_parent, this._complete); 582 | this.unsubscribe(); 583 | } 584 | } else { 585 | this.unsubscribe(); 586 | } 587 | } 588 | }; 589 | SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) { 590 | try { 591 | fn.call(this._context, value); 592 | } catch (err) { 593 | this.unsubscribe(); 594 | throw err; 595 | } 596 | }; 597 | SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) { 598 | try { 599 | fn.call(this._context, value); 600 | } catch (err) { 601 | parent.syncErrorValue = err; 602 | parent.syncErrorThrown = true; 603 | return true; 604 | } 605 | return false; 606 | }; 607 | SafeSubscriber.prototype._unsubscribe = function () { 608 | var _parent = this._parent; 609 | this._context = null; 610 | this._parent = null; 611 | _parent.unsubscribe(); 612 | }; 613 | return SafeSubscriber; 614 | }(Subscriber); 615 | 616 | var Subscriber_1$2 = { 617 | Subscriber: Subscriber_2 618 | }; 619 | 620 | var Subscriber_1$1 = Subscriber_1$2; 621 | var rxSubscriber_1$1 = rxSubscriber; 622 | var Observer_1 = Observer; 623 | function toSubscriber(nextOrObserver, error, complete) { 624 | if (nextOrObserver) { 625 | if (nextOrObserver instanceof Subscriber_1$1.Subscriber) { 626 | return nextOrObserver; 627 | } 628 | if (nextOrObserver[rxSubscriber_1$1.$$rxSubscriber]) { 629 | return nextOrObserver[rxSubscriber_1$1.$$rxSubscriber](); 630 | } 631 | } 632 | if (!nextOrObserver && !error && !complete) { 633 | return new Subscriber_1$1.Subscriber(Observer_1.empty); 634 | } 635 | return new Subscriber_1$1.Subscriber(nextOrObserver, error, complete); 636 | } 637 | var toSubscriber_2 = toSubscriber; 638 | 639 | var toSubscriber_1$1 = { 640 | toSubscriber: toSubscriber_2 641 | }; 642 | 643 | var root_1$3 = root; 644 | function getSymbolObservable(context) { 645 | var $$observable; 646 | var _Symbol = context.Symbol; 647 | if (typeof _Symbol === 'function') { 648 | if (_Symbol.observable) { 649 | $$observable = _Symbol.observable; 650 | } else { 651 | $$observable = _Symbol('observable'); 652 | _Symbol.observable = $$observable; 653 | } 654 | } else { 655 | $$observable = '@@observable'; 656 | } 657 | return $$observable; 658 | } 659 | var getSymbolObservable_1 = getSymbolObservable; 660 | var $$observable = getSymbolObservable(root_1$3.root); 661 | 662 | var observable = { 663 | getSymbolObservable: getSymbolObservable_1, 664 | $$observable: $$observable 665 | }; 666 | 667 | var root_1 = root; 668 | var toSubscriber_1 = toSubscriber_1$1; 669 | var observable_1 = observable; 670 | /** 671 | * A representation of any set of values over any amount of time. This the most basic building block 672 | * of RxJS. 673 | * 674 | * @class Observable 675 | */ 676 | var Observable = function () { 677 | /** 678 | * @constructor 679 | * @param {Function} subscribe the function that is called when the Observable is 680 | * initially subscribed to. This function is given a Subscriber, to which new values 681 | * can be `next`ed, or an `error` method can be called to raise an error, or 682 | * `complete` can be called to notify of a successful completion. 683 | */ 684 | function Observable(subscribe) { 685 | this._isScalar = false; 686 | if (subscribe) { 687 | this._subscribe = subscribe; 688 | } 689 | } 690 | /** 691 | * Creates a new Observable, with this Observable as the source, and the passed 692 | * operator defined as the new observable's operator. 693 | * @method lift 694 | * @param {Operator} operator the operator defining the operation to take on the observable 695 | * @return {Observable} a new observable with the Operator applied 696 | */ 697 | Observable.prototype.lift = function (operator) { 698 | var observable$$1 = new Observable(); 699 | observable$$1.source = this; 700 | observable$$1.operator = operator; 701 | return observable$$1; 702 | }; 703 | Observable.prototype.subscribe = function (observerOrNext, error, complete) { 704 | var operator = this.operator; 705 | var sink = toSubscriber_1.toSubscriber(observerOrNext, error, complete); 706 | if (operator) { 707 | operator.call(sink, this); 708 | } else { 709 | sink.add(this._subscribe(sink)); 710 | } 711 | if (sink.syncErrorThrowable) { 712 | sink.syncErrorThrowable = false; 713 | if (sink.syncErrorThrown) { 714 | throw sink.syncErrorValue; 715 | } 716 | } 717 | return sink; 718 | }; 719 | /** 720 | * @method forEach 721 | * @param {Function} next a handler for each value emitted by the observable 722 | * @param {PromiseConstructor} [PromiseCtor] a constructor function used to instantiate the Promise 723 | * @return {Promise} a promise that either resolves on observable completion or 724 | * rejects with the handled error 725 | */ 726 | Observable.prototype.forEach = function (next, PromiseCtor) { 727 | var _this = this; 728 | if (!PromiseCtor) { 729 | if (root_1.root.Rx && root_1.root.Rx.config && root_1.root.Rx.config.Promise) { 730 | PromiseCtor = root_1.root.Rx.config.Promise; 731 | } else if (root_1.root.Promise) { 732 | PromiseCtor = root_1.root.Promise; 733 | } 734 | } 735 | if (!PromiseCtor) { 736 | throw new Error('no Promise impl found'); 737 | } 738 | return new PromiseCtor(function (resolve, reject) { 739 | var subscription = _this.subscribe(function (value) { 740 | if (subscription) { 741 | // if there is a subscription, then we can surmise 742 | // the next handling is asynchronous. Any errors thrown 743 | // need to be rejected explicitly and unsubscribe must be 744 | // called manually 745 | try { 746 | next(value); 747 | } catch (err) { 748 | reject(err); 749 | subscription.unsubscribe(); 750 | } 751 | } else { 752 | // if there is NO subscription, then we're getting a nexted 753 | // value synchronously during subscription. We can just call it. 754 | // If it errors, Observable's `subscribe` will ensure the 755 | // unsubscription logic is called, then synchronously rethrow the error. 756 | // After that, Promise will trap the error and send it 757 | // down the rejection path. 758 | next(value); 759 | } 760 | }, reject, resolve); 761 | }); 762 | }; 763 | Observable.prototype._subscribe = function (subscriber) { 764 | return this.source.subscribe(subscriber); 765 | }; 766 | /** 767 | * An interop point defined by the es7-observable spec https://github.com/zenparsing/es-observable 768 | * @method Symbol.observable 769 | * @return {Observable} this instance of the observable 770 | */ 771 | Observable.prototype[observable_1.$$observable] = function () { 772 | return this; 773 | }; 774 | // HACK: Since TypeScript inherits static properties too, we have to 775 | // fight against TypeScript here so Subject can have a different static create signature 776 | /** 777 | * Creates a new cold Observable by calling the Observable constructor 778 | * @static true 779 | * @owner Observable 780 | * @method create 781 | * @param {Function} subscribe? the subscriber function to be passed to the Observable constructor 782 | * @return {Observable} a new cold observable 783 | */ 784 | Observable.create = function (subscribe) { 785 | return new Observable(subscribe); 786 | }; 787 | return Observable; 788 | }(); 789 | var Observable_2 = Observable; 790 | 791 | var Observable_1$1 = { 792 | Observable: Observable_2 793 | }; 794 | 795 | var __extends$4 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 796 | for (var p in b) { 797 | if (b.hasOwnProperty(p)) d[p] = b[p]; 798 | }function __() { 799 | this.constructor = d; 800 | } 801 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 802 | }; 803 | /** 804 | * An error thrown when an action is invalid because the object has been 805 | * unsubscribed. 806 | * 807 | * @see {@link Subject} 808 | * @see {@link BehaviorSubject} 809 | * 810 | * @class ObjectUnsubscribedError 811 | */ 812 | var ObjectUnsubscribedError = function (_super) { 813 | __extends$4(ObjectUnsubscribedError, _super); 814 | function ObjectUnsubscribedError() { 815 | var err = _super.call(this, 'object unsubscribed'); 816 | this.name = err.name = 'ObjectUnsubscribedError'; 817 | this.stack = err.stack; 818 | this.message = err.message; 819 | } 820 | return ObjectUnsubscribedError; 821 | }(Error); 822 | var ObjectUnsubscribedError_2 = ObjectUnsubscribedError; 823 | 824 | var ObjectUnsubscribedError_1$2 = { 825 | ObjectUnsubscribedError: ObjectUnsubscribedError_2 826 | }; 827 | 828 | var __extends$5 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 829 | for (var p in b) { 830 | if (b.hasOwnProperty(p)) d[p] = b[p]; 831 | }function __() { 832 | this.constructor = d; 833 | } 834 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 835 | }; 836 | var Subscription_1$4 = Subscription_1$2; 837 | /** 838 | * We need this JSDoc comment for affecting ESDoc. 839 | * @ignore 840 | * @extends {Ignored} 841 | */ 842 | var SubjectSubscription = function (_super) { 843 | __extends$5(SubjectSubscription, _super); 844 | function SubjectSubscription(subject, subscriber) { 845 | _super.call(this); 846 | this.subject = subject; 847 | this.subscriber = subscriber; 848 | this.closed = false; 849 | } 850 | SubjectSubscription.prototype.unsubscribe = function () { 851 | if (this.closed) { 852 | return; 853 | } 854 | this.closed = true; 855 | var subject = this.subject; 856 | var observers = subject.observers; 857 | this.subject = null; 858 | if (!observers || observers.length === 0 || subject.isStopped || subject.closed) { 859 | return; 860 | } 861 | var subscriberIndex = observers.indexOf(this.subscriber); 862 | if (subscriberIndex !== -1) { 863 | observers.splice(subscriberIndex, 1); 864 | } 865 | }; 866 | return SubjectSubscription; 867 | }(Subscription_1$4.Subscription); 868 | var SubjectSubscription_2 = SubjectSubscription; 869 | 870 | var SubjectSubscription_1$1 = { 871 | SubjectSubscription: SubjectSubscription_2 872 | }; 873 | 874 | var __extends$1 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 875 | for (var p in b) { 876 | if (b.hasOwnProperty(p)) d[p] = b[p]; 877 | }function __() { 878 | this.constructor = d; 879 | } 880 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 881 | }; 882 | var Observable_1 = Observable_1$1; 883 | var Subscriber_1 = Subscriber_1$2; 884 | var Subscription_1 = Subscription_1$2; 885 | var ObjectUnsubscribedError_1$1 = ObjectUnsubscribedError_1$2; 886 | var SubjectSubscription_1 = SubjectSubscription_1$1; 887 | var rxSubscriber_1 = rxSubscriber; 888 | /** 889 | * @class SubjectSubscriber 890 | */ 891 | var SubjectSubscriber = function (_super) { 892 | __extends$1(SubjectSubscriber, _super); 893 | function SubjectSubscriber(destination) { 894 | _super.call(this, destination); 895 | this.destination = destination; 896 | } 897 | return SubjectSubscriber; 898 | }(Subscriber_1.Subscriber); 899 | var SubjectSubscriber_1 = SubjectSubscriber; 900 | /** 901 | * @class Subject 902 | */ 903 | var Subject = function (_super) { 904 | __extends$1(Subject, _super); 905 | function Subject() { 906 | _super.call(this); 907 | this.observers = []; 908 | this.closed = false; 909 | this.isStopped = false; 910 | this.hasError = false; 911 | this.thrownError = null; 912 | } 913 | Subject.prototype[rxSubscriber_1.$$rxSubscriber] = function () { 914 | return new SubjectSubscriber(this); 915 | }; 916 | Subject.prototype.lift = function (operator) { 917 | var subject = new AnonymousSubject(this, this); 918 | subject.operator = operator; 919 | return subject; 920 | }; 921 | Subject.prototype.next = function (value) { 922 | if (this.closed) { 923 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 924 | } 925 | if (!this.isStopped) { 926 | var observers = this.observers; 927 | var len = observers.length; 928 | var copy = observers.slice(); 929 | for (var i = 0; i < len; i++) { 930 | copy[i].next(value); 931 | } 932 | } 933 | }; 934 | Subject.prototype.error = function (err) { 935 | if (this.closed) { 936 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 937 | } 938 | this.hasError = true; 939 | this.thrownError = err; 940 | this.isStopped = true; 941 | var observers = this.observers; 942 | var len = observers.length; 943 | var copy = observers.slice(); 944 | for (var i = 0; i < len; i++) { 945 | copy[i].error(err); 946 | } 947 | this.observers.length = 0; 948 | }; 949 | Subject.prototype.complete = function () { 950 | if (this.closed) { 951 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 952 | } 953 | this.isStopped = true; 954 | var observers = this.observers; 955 | var len = observers.length; 956 | var copy = observers.slice(); 957 | for (var i = 0; i < len; i++) { 958 | copy[i].complete(); 959 | } 960 | this.observers.length = 0; 961 | }; 962 | Subject.prototype.unsubscribe = function () { 963 | this.isStopped = true; 964 | this.closed = true; 965 | this.observers = null; 966 | }; 967 | Subject.prototype._subscribe = function (subscriber) { 968 | if (this.closed) { 969 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 970 | } else if (this.hasError) { 971 | subscriber.error(this.thrownError); 972 | return Subscription_1.Subscription.EMPTY; 973 | } else if (this.isStopped) { 974 | subscriber.complete(); 975 | return Subscription_1.Subscription.EMPTY; 976 | } else { 977 | this.observers.push(subscriber); 978 | return new SubjectSubscription_1.SubjectSubscription(this, subscriber); 979 | } 980 | }; 981 | Subject.prototype.asObservable = function () { 982 | var observable = new Observable_1.Observable(); 983 | observable.source = this; 984 | return observable; 985 | }; 986 | Subject.create = function (destination, source) { 987 | return new AnonymousSubject(destination, source); 988 | }; 989 | return Subject; 990 | }(Observable_1.Observable); 991 | var Subject_2 = Subject; 992 | /** 993 | * @class AnonymousSubject 994 | */ 995 | var AnonymousSubject = function (_super) { 996 | __extends$1(AnonymousSubject, _super); 997 | function AnonymousSubject(destination, source) { 998 | _super.call(this); 999 | this.destination = destination; 1000 | this.source = source; 1001 | } 1002 | AnonymousSubject.prototype.next = function (value) { 1003 | var destination = this.destination; 1004 | if (destination && destination.next) { 1005 | destination.next(value); 1006 | } 1007 | }; 1008 | AnonymousSubject.prototype.error = function (err) { 1009 | var destination = this.destination; 1010 | if (destination && destination.error) { 1011 | this.destination.error(err); 1012 | } 1013 | }; 1014 | AnonymousSubject.prototype.complete = function () { 1015 | var destination = this.destination; 1016 | if (destination && destination.complete) { 1017 | this.destination.complete(); 1018 | } 1019 | }; 1020 | AnonymousSubject.prototype._subscribe = function (subscriber) { 1021 | var source = this.source; 1022 | if (source) { 1023 | return this.source.subscribe(subscriber); 1024 | } else { 1025 | return Subscription_1.Subscription.EMPTY; 1026 | } 1027 | }; 1028 | return AnonymousSubject; 1029 | }(Subject); 1030 | var AnonymousSubject_1 = AnonymousSubject; 1031 | 1032 | var Subject_1$1 = { 1033 | SubjectSubscriber: SubjectSubscriber_1, 1034 | Subject: Subject_2, 1035 | AnonymousSubject: AnonymousSubject_1 1036 | }; 1037 | 1038 | var __extends = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 1039 | for (var p in b) { 1040 | if (b.hasOwnProperty(p)) d[p] = b[p]; 1041 | }function __() { 1042 | this.constructor = d; 1043 | } 1044 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 1045 | }; 1046 | var Subject_1 = Subject_1$1; 1047 | var ObjectUnsubscribedError_1 = ObjectUnsubscribedError_1$2; 1048 | /** 1049 | * @class BehaviorSubject 1050 | */ 1051 | var BehaviorSubject = function (_super) { 1052 | __extends(BehaviorSubject, _super); 1053 | function BehaviorSubject(_value) { 1054 | _super.call(this); 1055 | this._value = _value; 1056 | } 1057 | Object.defineProperty(BehaviorSubject.prototype, "value", { 1058 | get: function get() { 1059 | return this.getValue(); 1060 | }, 1061 | enumerable: true, 1062 | configurable: true 1063 | }); 1064 | BehaviorSubject.prototype._subscribe = function (subscriber) { 1065 | var subscription = _super.prototype._subscribe.call(this, subscriber); 1066 | if (subscription && !subscription.closed) { 1067 | subscriber.next(this._value); 1068 | } 1069 | return subscription; 1070 | }; 1071 | BehaviorSubject.prototype.getValue = function () { 1072 | if (this.hasError) { 1073 | throw this.thrownError; 1074 | } else if (this.closed) { 1075 | throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError(); 1076 | } else { 1077 | return this._value; 1078 | } 1079 | }; 1080 | BehaviorSubject.prototype.next = function (value) { 1081 | _super.prototype.next.call(this, this._value = value); 1082 | }; 1083 | return BehaviorSubject; 1084 | }(Subject_1.Subject); 1085 | var BehaviorSubject_2 = BehaviorSubject; 1086 | 1087 | function createRxStore(reducer, initialState) { 1088 | var state$ = new BehaviorSubject_2(initialState); 1089 | return { 1090 | state$: state$, 1091 | dispatch: function dispatch(action) { 1092 | return state$.next(reducer(state$.value, action)); 1093 | }, 1094 | subscribe: function subscribe() { 1095 | return state$.subscribe.apply(state$, arguments); 1096 | } 1097 | }; 1098 | } 1099 | 1100 | exports.createRxStore = createRxStore; 1101 | 1102 | Object.defineProperty(exports, '__esModule', { value: true }); 1103 | 1104 | }); 1105 | -------------------------------------------------------------------------------- /dist/rx-store.browser.js: -------------------------------------------------------------------------------- 1 | (function (exports) { 2 | 'use strict'; 3 | 4 | var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; 5 | 6 | 7 | 8 | 9 | 10 | function createCommonjsModule(fn, module) { 11 | return module = { exports: {} }, fn(module, module.exports), module.exports; 12 | } 13 | 14 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { 15 | return typeof obj; 16 | } : function (obj) { 17 | return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; 18 | }; 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | var get$1 = function get$1(object, property, receiver) { 35 | if (object === null) object = Function.prototype; 36 | var desc = Object.getOwnPropertyDescriptor(object, property); 37 | 38 | if (desc === undefined) { 39 | var parent = Object.getPrototypeOf(object); 40 | 41 | if (parent === null) { 42 | return undefined; 43 | } else { 44 | return get$1(parent, property, receiver); 45 | } 46 | } else if ("value" in desc) { 47 | return desc.value; 48 | } else { 49 | var getter = desc.get; 50 | 51 | if (getter === undefined) { 52 | return undefined; 53 | } 54 | 55 | return getter.call(receiver); 56 | } 57 | }; 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | var set = function set(object, property, value, receiver) { 76 | var desc = Object.getOwnPropertyDescriptor(object, property); 77 | 78 | if (desc === undefined) { 79 | var parent = Object.getPrototypeOf(object); 80 | 81 | if (parent !== null) { 82 | set(parent, property, value, receiver); 83 | } 84 | } else if ("value" in desc && desc.writable) { 85 | desc.value = value; 86 | } else { 87 | var setter = desc.set; 88 | 89 | if (setter !== undefined) { 90 | setter.call(receiver, value); 91 | } 92 | } 93 | 94 | return value; 95 | }; 96 | 97 | var root = createCommonjsModule(function (module, exports) { 98 | "use strict"; 99 | /** 100 | * window: browser in DOM main thread 101 | * self: browser in WebWorker 102 | * global: Node.js/other 103 | */ 104 | 105 | exports.root = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) == 'object' && window.window === window && window || (typeof self === 'undefined' ? 'undefined' : _typeof(self)) == 'object' && self.self === self && self || _typeof(commonjsGlobal) == 'object' && commonjsGlobal.global === commonjsGlobal && commonjsGlobal; 106 | if (!exports.root) { 107 | throw new Error('RxJS could not find any global context (window, self, global)'); 108 | } 109 | }); 110 | 111 | function isFunction(x) { 112 | return typeof x === 'function'; 113 | } 114 | var isFunction_2 = isFunction; 115 | 116 | var isFunction_1$1 = { 117 | isFunction: isFunction_2 118 | }; 119 | 120 | var isArray_1$1 = Array.isArray || function (x) { 121 | return x && typeof x.length === 'number'; 122 | }; 123 | 124 | var isArray = { 125 | isArray: isArray_1$1 126 | }; 127 | 128 | function isObject(x) { 129 | return x != null && (typeof x === "undefined" ? "undefined" : _typeof(x)) === 'object'; 130 | } 131 | var isObject_2 = isObject; 132 | 133 | var isObject_1$1 = { 134 | isObject: isObject_2 135 | }; 136 | 137 | // typeof any so that it we don't have to cast when comparing a result to the error object 138 | 139 | var errorObject_1$2 = { e: {} }; 140 | 141 | var errorObject = { 142 | errorObject: errorObject_1$2 143 | }; 144 | 145 | var errorObject_1$1 = errorObject; 146 | var tryCatchTarget; 147 | function tryCatcher() { 148 | try { 149 | return tryCatchTarget.apply(this, arguments); 150 | } catch (e) { 151 | errorObject_1$1.errorObject.e = e; 152 | return errorObject_1$1.errorObject; 153 | } 154 | } 155 | function tryCatch(fn) { 156 | tryCatchTarget = fn; 157 | return tryCatcher; 158 | } 159 | var tryCatch_2 = tryCatch; 160 | 161 | 162 | var tryCatch_1$1 = { 163 | tryCatch: tryCatch_2 164 | }; 165 | 166 | var __extends$3 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 167 | for (var p in b) { 168 | if (b.hasOwnProperty(p)) d[p] = b[p]; 169 | }function __() { 170 | this.constructor = d; 171 | } 172 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 173 | }; 174 | /** 175 | * An error thrown when one or more errors have occurred during the 176 | * `unsubscribe` of a {@link Subscription}. 177 | */ 178 | var UnsubscriptionError = function (_super) { 179 | __extends$3(UnsubscriptionError, _super); 180 | function UnsubscriptionError(errors) { 181 | _super.call(this); 182 | this.errors = errors; 183 | var err = Error.call(this, errors ? errors.length + " errors occurred during unsubscription:\n " + errors.map(function (err, i) { 184 | return i + 1 + ") " + err.toString(); 185 | }).join('\n ') : ''); 186 | this.name = err.name = 'UnsubscriptionError'; 187 | this.stack = err.stack; 188 | this.message = err.message; 189 | } 190 | return UnsubscriptionError; 191 | }(Error); 192 | var UnsubscriptionError_2 = UnsubscriptionError; 193 | 194 | var UnsubscriptionError_1$1 = { 195 | UnsubscriptionError: UnsubscriptionError_2 196 | }; 197 | 198 | var isArray_1 = isArray; 199 | var isObject_1 = isObject_1$1; 200 | var isFunction_1$3 = isFunction_1$1; 201 | var tryCatch_1 = tryCatch_1$1; 202 | var errorObject_1 = errorObject; 203 | var UnsubscriptionError_1 = UnsubscriptionError_1$1; 204 | /** 205 | * Represents a disposable resource, such as the execution of an Observable. A 206 | * Subscription has one important method, `unsubscribe`, that takes no argument 207 | * and just disposes the resource held by the subscription. 208 | * 209 | * Additionally, subscriptions may be grouped together through the `add()` 210 | * method, which will attach a child Subscription to the current Subscription. 211 | * When a Subscription is unsubscribed, all its children (and its grandchildren) 212 | * will be unsubscribed as well. 213 | * 214 | * @class Subscription 215 | */ 216 | var Subscription = function () { 217 | /** 218 | * @param {function(): void} [unsubscribe] A function describing how to 219 | * perform the disposal of resources when the `unsubscribe` method is called. 220 | */ 221 | function Subscription(unsubscribe) { 222 | /** 223 | * A flag to indicate whether this Subscription has already been unsubscribed. 224 | * @type {boolean} 225 | */ 226 | this.closed = false; 227 | if (unsubscribe) { 228 | this._unsubscribe = unsubscribe; 229 | } 230 | } 231 | /** 232 | * Disposes the resources held by the subscription. May, for instance, cancel 233 | * an ongoing Observable execution or cancel any other type of work that 234 | * started when the Subscription was created. 235 | * @return {void} 236 | */ 237 | Subscription.prototype.unsubscribe = function () { 238 | var hasErrors = false; 239 | var errors; 240 | if (this.closed) { 241 | return; 242 | } 243 | this.closed = true; 244 | var _a = this, 245 | _unsubscribe = _a._unsubscribe, 246 | _subscriptions = _a._subscriptions; 247 | this._subscriptions = null; 248 | if (isFunction_1$3.isFunction(_unsubscribe)) { 249 | var trial = tryCatch_1.tryCatch(_unsubscribe).call(this); 250 | if (trial === errorObject_1.errorObject) { 251 | hasErrors = true; 252 | (errors = errors || []).push(errorObject_1.errorObject.e); 253 | } 254 | } 255 | if (isArray_1.isArray(_subscriptions)) { 256 | var index = -1; 257 | var len = _subscriptions.length; 258 | while (++index < len) { 259 | var sub = _subscriptions[index]; 260 | if (isObject_1.isObject(sub)) { 261 | var trial = tryCatch_1.tryCatch(sub.unsubscribe).call(sub); 262 | if (trial === errorObject_1.errorObject) { 263 | hasErrors = true; 264 | errors = errors || []; 265 | var err = errorObject_1.errorObject.e; 266 | if (err instanceof UnsubscriptionError_1.UnsubscriptionError) { 267 | errors = errors.concat(err.errors); 268 | } else { 269 | errors.push(err); 270 | } 271 | } 272 | } 273 | } 274 | } 275 | if (hasErrors) { 276 | throw new UnsubscriptionError_1.UnsubscriptionError(errors); 277 | } 278 | }; 279 | /** 280 | * Adds a tear down to be called during the unsubscribe() of this 281 | * Subscription. 282 | * 283 | * If the tear down being added is a subscription that is already 284 | * unsubscribed, is the same reference `add` is being called on, or is 285 | * `Subscription.EMPTY`, it will not be added. 286 | * 287 | * If this subscription is already in an `closed` state, the passed 288 | * tear down logic will be executed immediately. 289 | * 290 | * @param {TeardownLogic} teardown The additional logic to execute on 291 | * teardown. 292 | * @return {Subscription} Returns the Subscription used or created to be 293 | * added to the inner subscriptions list. This Subscription can be used with 294 | * `remove()` to remove the passed teardown logic from the inner subscriptions 295 | * list. 296 | */ 297 | Subscription.prototype.add = function (teardown) { 298 | if (!teardown || teardown === Subscription.EMPTY) { 299 | return Subscription.EMPTY; 300 | } 301 | if (teardown === this) { 302 | return this; 303 | } 304 | var sub = teardown; 305 | switch (typeof teardown === 'undefined' ? 'undefined' : _typeof(teardown)) { 306 | case 'function': 307 | sub = new Subscription(teardown); 308 | case 'object': 309 | if (sub.closed || typeof sub.unsubscribe !== 'function') { 310 | break; 311 | } else if (this.closed) { 312 | sub.unsubscribe(); 313 | } else { 314 | (this._subscriptions || (this._subscriptions = [])).push(sub); 315 | } 316 | break; 317 | default: 318 | throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.'); 319 | } 320 | return sub; 321 | }; 322 | /** 323 | * Removes a Subscription from the internal list of subscriptions that will 324 | * unsubscribe during the unsubscribe process of this Subscription. 325 | * @param {Subscription} subscription The subscription to remove. 326 | * @return {void} 327 | */ 328 | Subscription.prototype.remove = function (subscription) { 329 | // HACK: This might be redundant because of the logic in `add()` 330 | if (subscription == null || subscription === this || subscription === Subscription.EMPTY) { 331 | return; 332 | } 333 | var subscriptions = this._subscriptions; 334 | if (subscriptions) { 335 | var subscriptionIndex = subscriptions.indexOf(subscription); 336 | if (subscriptionIndex !== -1) { 337 | subscriptions.splice(subscriptionIndex, 1); 338 | } 339 | } 340 | }; 341 | Subscription.EMPTY = function (empty) { 342 | empty.closed = true; 343 | return empty; 344 | }(new Subscription()); 345 | return Subscription; 346 | }(); 347 | var Subscription_2 = Subscription; 348 | 349 | var Subscription_1$2 = { 350 | Subscription: Subscription_2 351 | }; 352 | 353 | var empty = { 354 | closed: true, 355 | next: function next(value) {}, 356 | error: function error(err) { 357 | throw err; 358 | }, 359 | complete: function complete() {} 360 | }; 361 | 362 | var Observer = { 363 | empty: empty 364 | }; 365 | 366 | var root_1$2 = root; 367 | var _Symbol = root_1$2.root.Symbol; 368 | var $$rxSubscriber = typeof _Symbol === 'function' && typeof _Symbol.for === 'function' ? _Symbol.for('rxSubscriber') : '@@rxSubscriber'; 369 | 370 | var rxSubscriber = { 371 | $$rxSubscriber: $$rxSubscriber 372 | }; 373 | 374 | var __extends$2 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 375 | for (var p in b) { 376 | if (b.hasOwnProperty(p)) d[p] = b[p]; 377 | }function __() { 378 | this.constructor = d; 379 | } 380 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 381 | }; 382 | var isFunction_1 = isFunction_1$1; 383 | var Subscription_1$1 = Subscription_1$2; 384 | var Observer_1$1 = Observer; 385 | var rxSubscriber_1$2 = rxSubscriber; 386 | /** 387 | * Implements the {@link Observer} interface and extends the 388 | * {@link Subscription} class. While the {@link Observer} is the public API for 389 | * consuming the values of an {@link Observable}, all Observers get converted to 390 | * a Subscriber, in order to provide Subscription-like capabilities such as 391 | * `unsubscribe`. Subscriber is a common type in RxJS, and crucial for 392 | * implementing operators, but it is rarely used as a public API. 393 | * 394 | * @class Subscriber 395 | */ 396 | var Subscriber = function (_super) { 397 | __extends$2(Subscriber, _super); 398 | /** 399 | * @param {Observer|function(value: T): void} [destinationOrNext] A partially 400 | * defined Observer or a `next` callback function. 401 | * @param {function(e: ?any): void} [error] The `error` callback of an 402 | * Observer. 403 | * @param {function(): void} [complete] The `complete` callback of an 404 | * Observer. 405 | */ 406 | function Subscriber(destinationOrNext, error, complete) { 407 | _super.call(this); 408 | this.syncErrorValue = null; 409 | this.syncErrorThrown = false; 410 | this.syncErrorThrowable = false; 411 | this.isStopped = false; 412 | switch (arguments.length) { 413 | case 0: 414 | this.destination = Observer_1$1.empty; 415 | break; 416 | case 1: 417 | if (!destinationOrNext) { 418 | this.destination = Observer_1$1.empty; 419 | break; 420 | } 421 | if ((typeof destinationOrNext === 'undefined' ? 'undefined' : _typeof(destinationOrNext)) === 'object') { 422 | if (destinationOrNext instanceof Subscriber) { 423 | this.destination = destinationOrNext; 424 | this.destination.add(this); 425 | } else { 426 | this.syncErrorThrowable = true; 427 | this.destination = new SafeSubscriber(this, destinationOrNext); 428 | } 429 | break; 430 | } 431 | default: 432 | this.syncErrorThrowable = true; 433 | this.destination = new SafeSubscriber(this, destinationOrNext, error, complete); 434 | break; 435 | } 436 | } 437 | Subscriber.prototype[rxSubscriber_1$2.$$rxSubscriber] = function () { 438 | return this; 439 | }; 440 | /** 441 | * A static factory for a Subscriber, given a (potentially partial) definition 442 | * of an Observer. 443 | * @param {function(x: ?T): void} [next] The `next` callback of an Observer. 444 | * @param {function(e: ?any): void} [error] The `error` callback of an 445 | * Observer. 446 | * @param {function(): void} [complete] The `complete` callback of an 447 | * Observer. 448 | * @return {Subscriber} A Subscriber wrapping the (partially defined) 449 | * Observer represented by the given arguments. 450 | */ 451 | Subscriber.create = function (next, error, complete) { 452 | var subscriber = new Subscriber(next, error, complete); 453 | subscriber.syncErrorThrowable = false; 454 | return subscriber; 455 | }; 456 | /** 457 | * The {@link Observer} callback to receive notifications of type `next` from 458 | * the Observable, with a value. The Observable may call this method 0 or more 459 | * times. 460 | * @param {T} [value] The `next` value. 461 | * @return {void} 462 | */ 463 | Subscriber.prototype.next = function (value) { 464 | if (!this.isStopped) { 465 | this._next(value); 466 | } 467 | }; 468 | /** 469 | * The {@link Observer} callback to receive notifications of type `error` from 470 | * the Observable, with an attached {@link Error}. Notifies the Observer that 471 | * the Observable has experienced an error condition. 472 | * @param {any} [err] The `error` exception. 473 | * @return {void} 474 | */ 475 | Subscriber.prototype.error = function (err) { 476 | if (!this.isStopped) { 477 | this.isStopped = true; 478 | this._error(err); 479 | } 480 | }; 481 | /** 482 | * The {@link Observer} callback to receive a valueless notification of type 483 | * `complete` from the Observable. Notifies the Observer that the Observable 484 | * has finished sending push-based notifications. 485 | * @return {void} 486 | */ 487 | Subscriber.prototype.complete = function () { 488 | if (!this.isStopped) { 489 | this.isStopped = true; 490 | this._complete(); 491 | } 492 | }; 493 | Subscriber.prototype.unsubscribe = function () { 494 | if (this.closed) { 495 | return; 496 | } 497 | this.isStopped = true; 498 | _super.prototype.unsubscribe.call(this); 499 | }; 500 | Subscriber.prototype._next = function (value) { 501 | this.destination.next(value); 502 | }; 503 | Subscriber.prototype._error = function (err) { 504 | this.destination.error(err); 505 | this.unsubscribe(); 506 | }; 507 | Subscriber.prototype._complete = function () { 508 | this.destination.complete(); 509 | this.unsubscribe(); 510 | }; 511 | return Subscriber; 512 | }(Subscription_1$1.Subscription); 513 | var Subscriber_2 = Subscriber; 514 | /** 515 | * We need this JSDoc comment for affecting ESDoc. 516 | * @ignore 517 | * @extends {Ignored} 518 | */ 519 | var SafeSubscriber = function (_super) { 520 | __extends$2(SafeSubscriber, _super); 521 | function SafeSubscriber(_parent, observerOrNext, error, complete) { 522 | _super.call(this); 523 | this._parent = _parent; 524 | var next; 525 | var context = this; 526 | if (isFunction_1.isFunction(observerOrNext)) { 527 | next = observerOrNext; 528 | } else if (observerOrNext) { 529 | context = observerOrNext; 530 | next = observerOrNext.next; 531 | error = observerOrNext.error; 532 | complete = observerOrNext.complete; 533 | if (isFunction_1.isFunction(context.unsubscribe)) { 534 | this.add(context.unsubscribe.bind(context)); 535 | } 536 | context.unsubscribe = this.unsubscribe.bind(this); 537 | } 538 | this._context = context; 539 | this._next = next; 540 | this._error = error; 541 | this._complete = complete; 542 | } 543 | SafeSubscriber.prototype.next = function (value) { 544 | if (!this.isStopped && this._next) { 545 | var _parent = this._parent; 546 | if (!_parent.syncErrorThrowable) { 547 | this.__tryOrUnsub(this._next, value); 548 | } else if (this.__tryOrSetError(_parent, this._next, value)) { 549 | this.unsubscribe(); 550 | } 551 | } 552 | }; 553 | SafeSubscriber.prototype.error = function (err) { 554 | if (!this.isStopped) { 555 | var _parent = this._parent; 556 | if (this._error) { 557 | if (!_parent.syncErrorThrowable) { 558 | this.__tryOrUnsub(this._error, err); 559 | this.unsubscribe(); 560 | } else { 561 | this.__tryOrSetError(_parent, this._error, err); 562 | this.unsubscribe(); 563 | } 564 | } else if (!_parent.syncErrorThrowable) { 565 | this.unsubscribe(); 566 | throw err; 567 | } else { 568 | _parent.syncErrorValue = err; 569 | _parent.syncErrorThrown = true; 570 | this.unsubscribe(); 571 | } 572 | } 573 | }; 574 | SafeSubscriber.prototype.complete = function () { 575 | if (!this.isStopped) { 576 | var _parent = this._parent; 577 | if (this._complete) { 578 | if (!_parent.syncErrorThrowable) { 579 | this.__tryOrUnsub(this._complete); 580 | this.unsubscribe(); 581 | } else { 582 | this.__tryOrSetError(_parent, this._complete); 583 | this.unsubscribe(); 584 | } 585 | } else { 586 | this.unsubscribe(); 587 | } 588 | } 589 | }; 590 | SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) { 591 | try { 592 | fn.call(this._context, value); 593 | } catch (err) { 594 | this.unsubscribe(); 595 | throw err; 596 | } 597 | }; 598 | SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) { 599 | try { 600 | fn.call(this._context, value); 601 | } catch (err) { 602 | parent.syncErrorValue = err; 603 | parent.syncErrorThrown = true; 604 | return true; 605 | } 606 | return false; 607 | }; 608 | SafeSubscriber.prototype._unsubscribe = function () { 609 | var _parent = this._parent; 610 | this._context = null; 611 | this._parent = null; 612 | _parent.unsubscribe(); 613 | }; 614 | return SafeSubscriber; 615 | }(Subscriber); 616 | 617 | var Subscriber_1$2 = { 618 | Subscriber: Subscriber_2 619 | }; 620 | 621 | var Subscriber_1$1 = Subscriber_1$2; 622 | var rxSubscriber_1$1 = rxSubscriber; 623 | var Observer_1 = Observer; 624 | function toSubscriber(nextOrObserver, error, complete) { 625 | if (nextOrObserver) { 626 | if (nextOrObserver instanceof Subscriber_1$1.Subscriber) { 627 | return nextOrObserver; 628 | } 629 | if (nextOrObserver[rxSubscriber_1$1.$$rxSubscriber]) { 630 | return nextOrObserver[rxSubscriber_1$1.$$rxSubscriber](); 631 | } 632 | } 633 | if (!nextOrObserver && !error && !complete) { 634 | return new Subscriber_1$1.Subscriber(Observer_1.empty); 635 | } 636 | return new Subscriber_1$1.Subscriber(nextOrObserver, error, complete); 637 | } 638 | var toSubscriber_2 = toSubscriber; 639 | 640 | var toSubscriber_1$1 = { 641 | toSubscriber: toSubscriber_2 642 | }; 643 | 644 | var root_1$3 = root; 645 | function getSymbolObservable(context) { 646 | var $$observable; 647 | var _Symbol = context.Symbol; 648 | if (typeof _Symbol === 'function') { 649 | if (_Symbol.observable) { 650 | $$observable = _Symbol.observable; 651 | } else { 652 | $$observable = _Symbol('observable'); 653 | _Symbol.observable = $$observable; 654 | } 655 | } else { 656 | $$observable = '@@observable'; 657 | } 658 | return $$observable; 659 | } 660 | var getSymbolObservable_1 = getSymbolObservable; 661 | var $$observable = getSymbolObservable(root_1$3.root); 662 | 663 | var observable = { 664 | getSymbolObservable: getSymbolObservable_1, 665 | $$observable: $$observable 666 | }; 667 | 668 | var root_1 = root; 669 | var toSubscriber_1 = toSubscriber_1$1; 670 | var observable_1 = observable; 671 | /** 672 | * A representation of any set of values over any amount of time. This the most basic building block 673 | * of RxJS. 674 | * 675 | * @class Observable 676 | */ 677 | var Observable = function () { 678 | /** 679 | * @constructor 680 | * @param {Function} subscribe the function that is called when the Observable is 681 | * initially subscribed to. This function is given a Subscriber, to which new values 682 | * can be `next`ed, or an `error` method can be called to raise an error, or 683 | * `complete` can be called to notify of a successful completion. 684 | */ 685 | function Observable(subscribe) { 686 | this._isScalar = false; 687 | if (subscribe) { 688 | this._subscribe = subscribe; 689 | } 690 | } 691 | /** 692 | * Creates a new Observable, with this Observable as the source, and the passed 693 | * operator defined as the new observable's operator. 694 | * @method lift 695 | * @param {Operator} operator the operator defining the operation to take on the observable 696 | * @return {Observable} a new observable with the Operator applied 697 | */ 698 | Observable.prototype.lift = function (operator) { 699 | var observable$$1 = new Observable(); 700 | observable$$1.source = this; 701 | observable$$1.operator = operator; 702 | return observable$$1; 703 | }; 704 | Observable.prototype.subscribe = function (observerOrNext, error, complete) { 705 | var operator = this.operator; 706 | var sink = toSubscriber_1.toSubscriber(observerOrNext, error, complete); 707 | if (operator) { 708 | operator.call(sink, this); 709 | } else { 710 | sink.add(this._subscribe(sink)); 711 | } 712 | if (sink.syncErrorThrowable) { 713 | sink.syncErrorThrowable = false; 714 | if (sink.syncErrorThrown) { 715 | throw sink.syncErrorValue; 716 | } 717 | } 718 | return sink; 719 | }; 720 | /** 721 | * @method forEach 722 | * @param {Function} next a handler for each value emitted by the observable 723 | * @param {PromiseConstructor} [PromiseCtor] a constructor function used to instantiate the Promise 724 | * @return {Promise} a promise that either resolves on observable completion or 725 | * rejects with the handled error 726 | */ 727 | Observable.prototype.forEach = function (next, PromiseCtor) { 728 | var _this = this; 729 | if (!PromiseCtor) { 730 | if (root_1.root.Rx && root_1.root.Rx.config && root_1.root.Rx.config.Promise) { 731 | PromiseCtor = root_1.root.Rx.config.Promise; 732 | } else if (root_1.root.Promise) { 733 | PromiseCtor = root_1.root.Promise; 734 | } 735 | } 736 | if (!PromiseCtor) { 737 | throw new Error('no Promise impl found'); 738 | } 739 | return new PromiseCtor(function (resolve, reject) { 740 | var subscription = _this.subscribe(function (value) { 741 | if (subscription) { 742 | // if there is a subscription, then we can surmise 743 | // the next handling is asynchronous. Any errors thrown 744 | // need to be rejected explicitly and unsubscribe must be 745 | // called manually 746 | try { 747 | next(value); 748 | } catch (err) { 749 | reject(err); 750 | subscription.unsubscribe(); 751 | } 752 | } else { 753 | // if there is NO subscription, then we're getting a nexted 754 | // value synchronously during subscription. We can just call it. 755 | // If it errors, Observable's `subscribe` will ensure the 756 | // unsubscription logic is called, then synchronously rethrow the error. 757 | // After that, Promise will trap the error and send it 758 | // down the rejection path. 759 | next(value); 760 | } 761 | }, reject, resolve); 762 | }); 763 | }; 764 | Observable.prototype._subscribe = function (subscriber) { 765 | return this.source.subscribe(subscriber); 766 | }; 767 | /** 768 | * An interop point defined by the es7-observable spec https://github.com/zenparsing/es-observable 769 | * @method Symbol.observable 770 | * @return {Observable} this instance of the observable 771 | */ 772 | Observable.prototype[observable_1.$$observable] = function () { 773 | return this; 774 | }; 775 | // HACK: Since TypeScript inherits static properties too, we have to 776 | // fight against TypeScript here so Subject can have a different static create signature 777 | /** 778 | * Creates a new cold Observable by calling the Observable constructor 779 | * @static true 780 | * @owner Observable 781 | * @method create 782 | * @param {Function} subscribe? the subscriber function to be passed to the Observable constructor 783 | * @return {Observable} a new cold observable 784 | */ 785 | Observable.create = function (subscribe) { 786 | return new Observable(subscribe); 787 | }; 788 | return Observable; 789 | }(); 790 | var Observable_2 = Observable; 791 | 792 | var Observable_1$1 = { 793 | Observable: Observable_2 794 | }; 795 | 796 | var __extends$4 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 797 | for (var p in b) { 798 | if (b.hasOwnProperty(p)) d[p] = b[p]; 799 | }function __() { 800 | this.constructor = d; 801 | } 802 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 803 | }; 804 | /** 805 | * An error thrown when an action is invalid because the object has been 806 | * unsubscribed. 807 | * 808 | * @see {@link Subject} 809 | * @see {@link BehaviorSubject} 810 | * 811 | * @class ObjectUnsubscribedError 812 | */ 813 | var ObjectUnsubscribedError = function (_super) { 814 | __extends$4(ObjectUnsubscribedError, _super); 815 | function ObjectUnsubscribedError() { 816 | var err = _super.call(this, 'object unsubscribed'); 817 | this.name = err.name = 'ObjectUnsubscribedError'; 818 | this.stack = err.stack; 819 | this.message = err.message; 820 | } 821 | return ObjectUnsubscribedError; 822 | }(Error); 823 | var ObjectUnsubscribedError_2 = ObjectUnsubscribedError; 824 | 825 | var ObjectUnsubscribedError_1$2 = { 826 | ObjectUnsubscribedError: ObjectUnsubscribedError_2 827 | }; 828 | 829 | var __extends$5 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 830 | for (var p in b) { 831 | if (b.hasOwnProperty(p)) d[p] = b[p]; 832 | }function __() { 833 | this.constructor = d; 834 | } 835 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 836 | }; 837 | var Subscription_1$4 = Subscription_1$2; 838 | /** 839 | * We need this JSDoc comment for affecting ESDoc. 840 | * @ignore 841 | * @extends {Ignored} 842 | */ 843 | var SubjectSubscription = function (_super) { 844 | __extends$5(SubjectSubscription, _super); 845 | function SubjectSubscription(subject, subscriber) { 846 | _super.call(this); 847 | this.subject = subject; 848 | this.subscriber = subscriber; 849 | this.closed = false; 850 | } 851 | SubjectSubscription.prototype.unsubscribe = function () { 852 | if (this.closed) { 853 | return; 854 | } 855 | this.closed = true; 856 | var subject = this.subject; 857 | var observers = subject.observers; 858 | this.subject = null; 859 | if (!observers || observers.length === 0 || subject.isStopped || subject.closed) { 860 | return; 861 | } 862 | var subscriberIndex = observers.indexOf(this.subscriber); 863 | if (subscriberIndex !== -1) { 864 | observers.splice(subscriberIndex, 1); 865 | } 866 | }; 867 | return SubjectSubscription; 868 | }(Subscription_1$4.Subscription); 869 | var SubjectSubscription_2 = SubjectSubscription; 870 | 871 | var SubjectSubscription_1$1 = { 872 | SubjectSubscription: SubjectSubscription_2 873 | }; 874 | 875 | var __extends$1 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 876 | for (var p in b) { 877 | if (b.hasOwnProperty(p)) d[p] = b[p]; 878 | }function __() { 879 | this.constructor = d; 880 | } 881 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 882 | }; 883 | var Observable_1 = Observable_1$1; 884 | var Subscriber_1 = Subscriber_1$2; 885 | var Subscription_1 = Subscription_1$2; 886 | var ObjectUnsubscribedError_1$1 = ObjectUnsubscribedError_1$2; 887 | var SubjectSubscription_1 = SubjectSubscription_1$1; 888 | var rxSubscriber_1 = rxSubscriber; 889 | /** 890 | * @class SubjectSubscriber 891 | */ 892 | var SubjectSubscriber = function (_super) { 893 | __extends$1(SubjectSubscriber, _super); 894 | function SubjectSubscriber(destination) { 895 | _super.call(this, destination); 896 | this.destination = destination; 897 | } 898 | return SubjectSubscriber; 899 | }(Subscriber_1.Subscriber); 900 | var SubjectSubscriber_1 = SubjectSubscriber; 901 | /** 902 | * @class Subject 903 | */ 904 | var Subject = function (_super) { 905 | __extends$1(Subject, _super); 906 | function Subject() { 907 | _super.call(this); 908 | this.observers = []; 909 | this.closed = false; 910 | this.isStopped = false; 911 | this.hasError = false; 912 | this.thrownError = null; 913 | } 914 | Subject.prototype[rxSubscriber_1.$$rxSubscriber] = function () { 915 | return new SubjectSubscriber(this); 916 | }; 917 | Subject.prototype.lift = function (operator) { 918 | var subject = new AnonymousSubject(this, this); 919 | subject.operator = operator; 920 | return subject; 921 | }; 922 | Subject.prototype.next = function (value) { 923 | if (this.closed) { 924 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 925 | } 926 | if (!this.isStopped) { 927 | var observers = this.observers; 928 | var len = observers.length; 929 | var copy = observers.slice(); 930 | for (var i = 0; i < len; i++) { 931 | copy[i].next(value); 932 | } 933 | } 934 | }; 935 | Subject.prototype.error = function (err) { 936 | if (this.closed) { 937 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 938 | } 939 | this.hasError = true; 940 | this.thrownError = err; 941 | this.isStopped = true; 942 | var observers = this.observers; 943 | var len = observers.length; 944 | var copy = observers.slice(); 945 | for (var i = 0; i < len; i++) { 946 | copy[i].error(err); 947 | } 948 | this.observers.length = 0; 949 | }; 950 | Subject.prototype.complete = function () { 951 | if (this.closed) { 952 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 953 | } 954 | this.isStopped = true; 955 | var observers = this.observers; 956 | var len = observers.length; 957 | var copy = observers.slice(); 958 | for (var i = 0; i < len; i++) { 959 | copy[i].complete(); 960 | } 961 | this.observers.length = 0; 962 | }; 963 | Subject.prototype.unsubscribe = function () { 964 | this.isStopped = true; 965 | this.closed = true; 966 | this.observers = null; 967 | }; 968 | Subject.prototype._subscribe = function (subscriber) { 969 | if (this.closed) { 970 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 971 | } else if (this.hasError) { 972 | subscriber.error(this.thrownError); 973 | return Subscription_1.Subscription.EMPTY; 974 | } else if (this.isStopped) { 975 | subscriber.complete(); 976 | return Subscription_1.Subscription.EMPTY; 977 | } else { 978 | this.observers.push(subscriber); 979 | return new SubjectSubscription_1.SubjectSubscription(this, subscriber); 980 | } 981 | }; 982 | Subject.prototype.asObservable = function () { 983 | var observable = new Observable_1.Observable(); 984 | observable.source = this; 985 | return observable; 986 | }; 987 | Subject.create = function (destination, source) { 988 | return new AnonymousSubject(destination, source); 989 | }; 990 | return Subject; 991 | }(Observable_1.Observable); 992 | var Subject_2 = Subject; 993 | /** 994 | * @class AnonymousSubject 995 | */ 996 | var AnonymousSubject = function (_super) { 997 | __extends$1(AnonymousSubject, _super); 998 | function AnonymousSubject(destination, source) { 999 | _super.call(this); 1000 | this.destination = destination; 1001 | this.source = source; 1002 | } 1003 | AnonymousSubject.prototype.next = function (value) { 1004 | var destination = this.destination; 1005 | if (destination && destination.next) { 1006 | destination.next(value); 1007 | } 1008 | }; 1009 | AnonymousSubject.prototype.error = function (err) { 1010 | var destination = this.destination; 1011 | if (destination && destination.error) { 1012 | this.destination.error(err); 1013 | } 1014 | }; 1015 | AnonymousSubject.prototype.complete = function () { 1016 | var destination = this.destination; 1017 | if (destination && destination.complete) { 1018 | this.destination.complete(); 1019 | } 1020 | }; 1021 | AnonymousSubject.prototype._subscribe = function (subscriber) { 1022 | var source = this.source; 1023 | if (source) { 1024 | return this.source.subscribe(subscriber); 1025 | } else { 1026 | return Subscription_1.Subscription.EMPTY; 1027 | } 1028 | }; 1029 | return AnonymousSubject; 1030 | }(Subject); 1031 | var AnonymousSubject_1 = AnonymousSubject; 1032 | 1033 | var Subject_1$1 = { 1034 | SubjectSubscriber: SubjectSubscriber_1, 1035 | Subject: Subject_2, 1036 | AnonymousSubject: AnonymousSubject_1 1037 | }; 1038 | 1039 | var __extends = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 1040 | for (var p in b) { 1041 | if (b.hasOwnProperty(p)) d[p] = b[p]; 1042 | }function __() { 1043 | this.constructor = d; 1044 | } 1045 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 1046 | }; 1047 | var Subject_1 = Subject_1$1; 1048 | var ObjectUnsubscribedError_1 = ObjectUnsubscribedError_1$2; 1049 | /** 1050 | * @class BehaviorSubject 1051 | */ 1052 | var BehaviorSubject = function (_super) { 1053 | __extends(BehaviorSubject, _super); 1054 | function BehaviorSubject(_value) { 1055 | _super.call(this); 1056 | this._value = _value; 1057 | } 1058 | Object.defineProperty(BehaviorSubject.prototype, "value", { 1059 | get: function get() { 1060 | return this.getValue(); 1061 | }, 1062 | enumerable: true, 1063 | configurable: true 1064 | }); 1065 | BehaviorSubject.prototype._subscribe = function (subscriber) { 1066 | var subscription = _super.prototype._subscribe.call(this, subscriber); 1067 | if (subscription && !subscription.closed) { 1068 | subscriber.next(this._value); 1069 | } 1070 | return subscription; 1071 | }; 1072 | BehaviorSubject.prototype.getValue = function () { 1073 | if (this.hasError) { 1074 | throw this.thrownError; 1075 | } else if (this.closed) { 1076 | throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError(); 1077 | } else { 1078 | return this._value; 1079 | } 1080 | }; 1081 | BehaviorSubject.prototype.next = function (value) { 1082 | _super.prototype.next.call(this, this._value = value); 1083 | }; 1084 | return BehaviorSubject; 1085 | }(Subject_1.Subject); 1086 | var BehaviorSubject_2 = BehaviorSubject; 1087 | 1088 | function createRxStore(reducer, initialState) { 1089 | var state$ = new BehaviorSubject_2(initialState); 1090 | return { 1091 | state$: state$, 1092 | dispatch: function dispatch(action) { 1093 | return state$.next(reducer(state$.value, action)); 1094 | }, 1095 | subscribe: function subscribe() { 1096 | return state$.subscribe.apply(state$, arguments); 1097 | } 1098 | }; 1099 | } 1100 | 1101 | exports.createRxStore = createRxStore; 1102 | 1103 | }((this.RxStore = this.RxStore || {}))); 1104 | -------------------------------------------------------------------------------- /dist/rx-store.browser.min.js: -------------------------------------------------------------------------------- 1 | !function(t){"use strict";function r(t,r){return r={exports:{}},t(r,r.exports),r.exports}function e(t){return"function"==typeof t}function o(t){return null!=t&&"object"===("undefined"==typeof t?"undefined":p(t))}function n(){try{return b.apply(this,arguments)}catch(t){return E.errorObject.e=t,E.errorObject}}function i(t){return b=t,n}function s(t,r,e){if(t){if(t instanceof X.Subscriber)return t;if(t[Z.$$rxSubscriber])return t[Z.$$rxSubscriber]()}return t||r||e?new X.Subscriber(t,r,e):new X.Subscriber(tt.empty)}function c(t){var r,e=t.Symbol;return"function"==typeof e?e.observable?r=e.observable:(r=e("observable"),e.observable=r):r="@@observable",r}function u(t,r){var e=new Jt(r);return{state$:e,dispatch:function(r){return e.next(t(e.value,r))},subscribe:function(){return e.subscribe.apply(e,arguments)}}}var b,h="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},f=r(function(t,r){if(r.root="object"==("undefined"==typeof window?"undefined":p(window))&&window.window===window&&window||"object"==("undefined"==typeof self?"undefined":p(self))&&self.self===self&&self||"object"==p(h)&&h.global===h&&h,!r.root)throw new Error("RxJS could not find any global context (window, self, global)")}),a=e,l={isFunction:a},y=Array.isArray||function(t){return t&&"number"==typeof t.length},d={isArray:y},w=o,_={isObject:w},v={e:{}},S={errorObject:v},E=S,x=i,m={tryCatch:x},O=h&&h.__extends||function(t,r){function e(){this.constructor=t}for(var o in r)r.hasOwnProperty(o)&&(t[o]=r[o]);t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)},j=function(t){function r(r){t.call(this),this.errors=r;var e=Error.call(this,r?r.length+" errors occurred during unsubscription:\n "+r.map(function(t,r){return r+1+") "+t.toString()}).join("\n "):"");this.name=e.name="UnsubscriptionError",this.stack=e.stack,this.message=e.message}return O(r,t),r}(Error),g=j,T={UnsubscriptionError:g},P=d,U=_,$=l,k=m,R=S,M=T,Y=function(){function t(t){this.closed=!1,t&&(this._unsubscribe=t)}return t.prototype.unsubscribe=function(){var t,r=!1;if(!this.closed){this.closed=!0;var e=this,o=e._unsubscribe,n=e._subscriptions;if(this._subscriptions=null,$.isFunction(o)){var i=k.tryCatch(o).call(this);i===R.errorObject&&(r=!0,(t=t||[]).push(R.errorObject.e))}if(P.isArray(n))for(var s=-1,c=n.length;++s 396 | */ 397 | var Subscriber = function (_super) { 398 | __extends$2(Subscriber, _super); 399 | /** 400 | * @param {Observer|function(value: T): void} [destinationOrNext] A partially 401 | * defined Observer or a `next` callback function. 402 | * @param {function(e: ?any): void} [error] The `error` callback of an 403 | * Observer. 404 | * @param {function(): void} [complete] The `complete` callback of an 405 | * Observer. 406 | */ 407 | function Subscriber(destinationOrNext, error, complete) { 408 | _super.call(this); 409 | this.syncErrorValue = null; 410 | this.syncErrorThrown = false; 411 | this.syncErrorThrowable = false; 412 | this.isStopped = false; 413 | switch (arguments.length) { 414 | case 0: 415 | this.destination = Observer_1$1.empty; 416 | break; 417 | case 1: 418 | if (!destinationOrNext) { 419 | this.destination = Observer_1$1.empty; 420 | break; 421 | } 422 | if ((typeof destinationOrNext === 'undefined' ? 'undefined' : _typeof(destinationOrNext)) === 'object') { 423 | if (destinationOrNext instanceof Subscriber) { 424 | this.destination = destinationOrNext; 425 | this.destination.add(this); 426 | } else { 427 | this.syncErrorThrowable = true; 428 | this.destination = new SafeSubscriber(this, destinationOrNext); 429 | } 430 | break; 431 | } 432 | default: 433 | this.syncErrorThrowable = true; 434 | this.destination = new SafeSubscriber(this, destinationOrNext, error, complete); 435 | break; 436 | } 437 | } 438 | Subscriber.prototype[rxSubscriber_1$2.$$rxSubscriber] = function () { 439 | return this; 440 | }; 441 | /** 442 | * A static factory for a Subscriber, given a (potentially partial) definition 443 | * of an Observer. 444 | * @param {function(x: ?T): void} [next] The `next` callback of an Observer. 445 | * @param {function(e: ?any): void} [error] The `error` callback of an 446 | * Observer. 447 | * @param {function(): void} [complete] The `complete` callback of an 448 | * Observer. 449 | * @return {Subscriber} A Subscriber wrapping the (partially defined) 450 | * Observer represented by the given arguments. 451 | */ 452 | Subscriber.create = function (next, error, complete) { 453 | var subscriber = new Subscriber(next, error, complete); 454 | subscriber.syncErrorThrowable = false; 455 | return subscriber; 456 | }; 457 | /** 458 | * The {@link Observer} callback to receive notifications of type `next` from 459 | * the Observable, with a value. The Observable may call this method 0 or more 460 | * times. 461 | * @param {T} [value] The `next` value. 462 | * @return {void} 463 | */ 464 | Subscriber.prototype.next = function (value) { 465 | if (!this.isStopped) { 466 | this._next(value); 467 | } 468 | }; 469 | /** 470 | * The {@link Observer} callback to receive notifications of type `error` from 471 | * the Observable, with an attached {@link Error}. Notifies the Observer that 472 | * the Observable has experienced an error condition. 473 | * @param {any} [err] The `error` exception. 474 | * @return {void} 475 | */ 476 | Subscriber.prototype.error = function (err) { 477 | if (!this.isStopped) { 478 | this.isStopped = true; 479 | this._error(err); 480 | } 481 | }; 482 | /** 483 | * The {@link Observer} callback to receive a valueless notification of type 484 | * `complete` from the Observable. Notifies the Observer that the Observable 485 | * has finished sending push-based notifications. 486 | * @return {void} 487 | */ 488 | Subscriber.prototype.complete = function () { 489 | if (!this.isStopped) { 490 | this.isStopped = true; 491 | this._complete(); 492 | } 493 | }; 494 | Subscriber.prototype.unsubscribe = function () { 495 | if (this.closed) { 496 | return; 497 | } 498 | this.isStopped = true; 499 | _super.prototype.unsubscribe.call(this); 500 | }; 501 | Subscriber.prototype._next = function (value) { 502 | this.destination.next(value); 503 | }; 504 | Subscriber.prototype._error = function (err) { 505 | this.destination.error(err); 506 | this.unsubscribe(); 507 | }; 508 | Subscriber.prototype._complete = function () { 509 | this.destination.complete(); 510 | this.unsubscribe(); 511 | }; 512 | return Subscriber; 513 | }(Subscription_1$1.Subscription); 514 | var Subscriber_2 = Subscriber; 515 | /** 516 | * We need this JSDoc comment for affecting ESDoc. 517 | * @ignore 518 | * @extends {Ignored} 519 | */ 520 | var SafeSubscriber = function (_super) { 521 | __extends$2(SafeSubscriber, _super); 522 | function SafeSubscriber(_parent, observerOrNext, error, complete) { 523 | _super.call(this); 524 | this._parent = _parent; 525 | var next; 526 | var context = this; 527 | if (isFunction_1.isFunction(observerOrNext)) { 528 | next = observerOrNext; 529 | } else if (observerOrNext) { 530 | context = observerOrNext; 531 | next = observerOrNext.next; 532 | error = observerOrNext.error; 533 | complete = observerOrNext.complete; 534 | if (isFunction_1.isFunction(context.unsubscribe)) { 535 | this.add(context.unsubscribe.bind(context)); 536 | } 537 | context.unsubscribe = this.unsubscribe.bind(this); 538 | } 539 | this._context = context; 540 | this._next = next; 541 | this._error = error; 542 | this._complete = complete; 543 | } 544 | SafeSubscriber.prototype.next = function (value) { 545 | if (!this.isStopped && this._next) { 546 | var _parent = this._parent; 547 | if (!_parent.syncErrorThrowable) { 548 | this.__tryOrUnsub(this._next, value); 549 | } else if (this.__tryOrSetError(_parent, this._next, value)) { 550 | this.unsubscribe(); 551 | } 552 | } 553 | }; 554 | SafeSubscriber.prototype.error = function (err) { 555 | if (!this.isStopped) { 556 | var _parent = this._parent; 557 | if (this._error) { 558 | if (!_parent.syncErrorThrowable) { 559 | this.__tryOrUnsub(this._error, err); 560 | this.unsubscribe(); 561 | } else { 562 | this.__tryOrSetError(_parent, this._error, err); 563 | this.unsubscribe(); 564 | } 565 | } else if (!_parent.syncErrorThrowable) { 566 | this.unsubscribe(); 567 | throw err; 568 | } else { 569 | _parent.syncErrorValue = err; 570 | _parent.syncErrorThrown = true; 571 | this.unsubscribe(); 572 | } 573 | } 574 | }; 575 | SafeSubscriber.prototype.complete = function () { 576 | if (!this.isStopped) { 577 | var _parent = this._parent; 578 | if (this._complete) { 579 | if (!_parent.syncErrorThrowable) { 580 | this.__tryOrUnsub(this._complete); 581 | this.unsubscribe(); 582 | } else { 583 | this.__tryOrSetError(_parent, this._complete); 584 | this.unsubscribe(); 585 | } 586 | } else { 587 | this.unsubscribe(); 588 | } 589 | } 590 | }; 591 | SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) { 592 | try { 593 | fn.call(this._context, value); 594 | } catch (err) { 595 | this.unsubscribe(); 596 | throw err; 597 | } 598 | }; 599 | SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) { 600 | try { 601 | fn.call(this._context, value); 602 | } catch (err) { 603 | parent.syncErrorValue = err; 604 | parent.syncErrorThrown = true; 605 | return true; 606 | } 607 | return false; 608 | }; 609 | SafeSubscriber.prototype._unsubscribe = function () { 610 | var _parent = this._parent; 611 | this._context = null; 612 | this._parent = null; 613 | _parent.unsubscribe(); 614 | }; 615 | return SafeSubscriber; 616 | }(Subscriber); 617 | 618 | var Subscriber_1$2 = { 619 | Subscriber: Subscriber_2 620 | }; 621 | 622 | var Subscriber_1$1 = Subscriber_1$2; 623 | var rxSubscriber_1$1 = rxSubscriber; 624 | var Observer_1 = Observer; 625 | function toSubscriber(nextOrObserver, error, complete) { 626 | if (nextOrObserver) { 627 | if (nextOrObserver instanceof Subscriber_1$1.Subscriber) { 628 | return nextOrObserver; 629 | } 630 | if (nextOrObserver[rxSubscriber_1$1.$$rxSubscriber]) { 631 | return nextOrObserver[rxSubscriber_1$1.$$rxSubscriber](); 632 | } 633 | } 634 | if (!nextOrObserver && !error && !complete) { 635 | return new Subscriber_1$1.Subscriber(Observer_1.empty); 636 | } 637 | return new Subscriber_1$1.Subscriber(nextOrObserver, error, complete); 638 | } 639 | var toSubscriber_2 = toSubscriber; 640 | 641 | var toSubscriber_1$1 = { 642 | toSubscriber: toSubscriber_2 643 | }; 644 | 645 | var root_1$3 = root; 646 | function getSymbolObservable(context) { 647 | var $$observable; 648 | var _Symbol = context.Symbol; 649 | if (typeof _Symbol === 'function') { 650 | if (_Symbol.observable) { 651 | $$observable = _Symbol.observable; 652 | } else { 653 | $$observable = _Symbol('observable'); 654 | _Symbol.observable = $$observable; 655 | } 656 | } else { 657 | $$observable = '@@observable'; 658 | } 659 | return $$observable; 660 | } 661 | var getSymbolObservable_1 = getSymbolObservable; 662 | var $$observable = getSymbolObservable(root_1$3.root); 663 | 664 | var observable = { 665 | getSymbolObservable: getSymbolObservable_1, 666 | $$observable: $$observable 667 | }; 668 | 669 | var root_1 = root; 670 | var toSubscriber_1 = toSubscriber_1$1; 671 | var observable_1 = observable; 672 | /** 673 | * A representation of any set of values over any amount of time. This the most basic building block 674 | * of RxJS. 675 | * 676 | * @class Observable 677 | */ 678 | var Observable = function () { 679 | /** 680 | * @constructor 681 | * @param {Function} subscribe the function that is called when the Observable is 682 | * initially subscribed to. This function is given a Subscriber, to which new values 683 | * can be `next`ed, or an `error` method can be called to raise an error, or 684 | * `complete` can be called to notify of a successful completion. 685 | */ 686 | function Observable(subscribe) { 687 | this._isScalar = false; 688 | if (subscribe) { 689 | this._subscribe = subscribe; 690 | } 691 | } 692 | /** 693 | * Creates a new Observable, with this Observable as the source, and the passed 694 | * operator defined as the new observable's operator. 695 | * @method lift 696 | * @param {Operator} operator the operator defining the operation to take on the observable 697 | * @return {Observable} a new observable with the Operator applied 698 | */ 699 | Observable.prototype.lift = function (operator) { 700 | var observable$$1 = new Observable(); 701 | observable$$1.source = this; 702 | observable$$1.operator = operator; 703 | return observable$$1; 704 | }; 705 | Observable.prototype.subscribe = function (observerOrNext, error, complete) { 706 | var operator = this.operator; 707 | var sink = toSubscriber_1.toSubscriber(observerOrNext, error, complete); 708 | if (operator) { 709 | operator.call(sink, this); 710 | } else { 711 | sink.add(this._subscribe(sink)); 712 | } 713 | if (sink.syncErrorThrowable) { 714 | sink.syncErrorThrowable = false; 715 | if (sink.syncErrorThrown) { 716 | throw sink.syncErrorValue; 717 | } 718 | } 719 | return sink; 720 | }; 721 | /** 722 | * @method forEach 723 | * @param {Function} next a handler for each value emitted by the observable 724 | * @param {PromiseConstructor} [PromiseCtor] a constructor function used to instantiate the Promise 725 | * @return {Promise} a promise that either resolves on observable completion or 726 | * rejects with the handled error 727 | */ 728 | Observable.prototype.forEach = function (next, PromiseCtor) { 729 | var _this = this; 730 | if (!PromiseCtor) { 731 | if (root_1.root.Rx && root_1.root.Rx.config && root_1.root.Rx.config.Promise) { 732 | PromiseCtor = root_1.root.Rx.config.Promise; 733 | } else if (root_1.root.Promise) { 734 | PromiseCtor = root_1.root.Promise; 735 | } 736 | } 737 | if (!PromiseCtor) { 738 | throw new Error('no Promise impl found'); 739 | } 740 | return new PromiseCtor(function (resolve, reject) { 741 | var subscription = _this.subscribe(function (value) { 742 | if (subscription) { 743 | // if there is a subscription, then we can surmise 744 | // the next handling is asynchronous. Any errors thrown 745 | // need to be rejected explicitly and unsubscribe must be 746 | // called manually 747 | try { 748 | next(value); 749 | } catch (err) { 750 | reject(err); 751 | subscription.unsubscribe(); 752 | } 753 | } else { 754 | // if there is NO subscription, then we're getting a nexted 755 | // value synchronously during subscription. We can just call it. 756 | // If it errors, Observable's `subscribe` will ensure the 757 | // unsubscription logic is called, then synchronously rethrow the error. 758 | // After that, Promise will trap the error and send it 759 | // down the rejection path. 760 | next(value); 761 | } 762 | }, reject, resolve); 763 | }); 764 | }; 765 | Observable.prototype._subscribe = function (subscriber) { 766 | return this.source.subscribe(subscriber); 767 | }; 768 | /** 769 | * An interop point defined by the es7-observable spec https://github.com/zenparsing/es-observable 770 | * @method Symbol.observable 771 | * @return {Observable} this instance of the observable 772 | */ 773 | Observable.prototype[observable_1.$$observable] = function () { 774 | return this; 775 | }; 776 | // HACK: Since TypeScript inherits static properties too, we have to 777 | // fight against TypeScript here so Subject can have a different static create signature 778 | /** 779 | * Creates a new cold Observable by calling the Observable constructor 780 | * @static true 781 | * @owner Observable 782 | * @method create 783 | * @param {Function} subscribe? the subscriber function to be passed to the Observable constructor 784 | * @return {Observable} a new cold observable 785 | */ 786 | Observable.create = function (subscribe) { 787 | return new Observable(subscribe); 788 | }; 789 | return Observable; 790 | }(); 791 | var Observable_2 = Observable; 792 | 793 | var Observable_1$1 = { 794 | Observable: Observable_2 795 | }; 796 | 797 | var __extends$4 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 798 | for (var p in b) { 799 | if (b.hasOwnProperty(p)) d[p] = b[p]; 800 | }function __() { 801 | this.constructor = d; 802 | } 803 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 804 | }; 805 | /** 806 | * An error thrown when an action is invalid because the object has been 807 | * unsubscribed. 808 | * 809 | * @see {@link Subject} 810 | * @see {@link BehaviorSubject} 811 | * 812 | * @class ObjectUnsubscribedError 813 | */ 814 | var ObjectUnsubscribedError = function (_super) { 815 | __extends$4(ObjectUnsubscribedError, _super); 816 | function ObjectUnsubscribedError() { 817 | var err = _super.call(this, 'object unsubscribed'); 818 | this.name = err.name = 'ObjectUnsubscribedError'; 819 | this.stack = err.stack; 820 | this.message = err.message; 821 | } 822 | return ObjectUnsubscribedError; 823 | }(Error); 824 | var ObjectUnsubscribedError_2 = ObjectUnsubscribedError; 825 | 826 | var ObjectUnsubscribedError_1$2 = { 827 | ObjectUnsubscribedError: ObjectUnsubscribedError_2 828 | }; 829 | 830 | var __extends$5 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 831 | for (var p in b) { 832 | if (b.hasOwnProperty(p)) d[p] = b[p]; 833 | }function __() { 834 | this.constructor = d; 835 | } 836 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 837 | }; 838 | var Subscription_1$4 = Subscription_1$2; 839 | /** 840 | * We need this JSDoc comment for affecting ESDoc. 841 | * @ignore 842 | * @extends {Ignored} 843 | */ 844 | var SubjectSubscription = function (_super) { 845 | __extends$5(SubjectSubscription, _super); 846 | function SubjectSubscription(subject, subscriber) { 847 | _super.call(this); 848 | this.subject = subject; 849 | this.subscriber = subscriber; 850 | this.closed = false; 851 | } 852 | SubjectSubscription.prototype.unsubscribe = function () { 853 | if (this.closed) { 854 | return; 855 | } 856 | this.closed = true; 857 | var subject = this.subject; 858 | var observers = subject.observers; 859 | this.subject = null; 860 | if (!observers || observers.length === 0 || subject.isStopped || subject.closed) { 861 | return; 862 | } 863 | var subscriberIndex = observers.indexOf(this.subscriber); 864 | if (subscriberIndex !== -1) { 865 | observers.splice(subscriberIndex, 1); 866 | } 867 | }; 868 | return SubjectSubscription; 869 | }(Subscription_1$4.Subscription); 870 | var SubjectSubscription_2 = SubjectSubscription; 871 | 872 | var SubjectSubscription_1$1 = { 873 | SubjectSubscription: SubjectSubscription_2 874 | }; 875 | 876 | var __extends$1 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 877 | for (var p in b) { 878 | if (b.hasOwnProperty(p)) d[p] = b[p]; 879 | }function __() { 880 | this.constructor = d; 881 | } 882 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 883 | }; 884 | var Observable_1 = Observable_1$1; 885 | var Subscriber_1 = Subscriber_1$2; 886 | var Subscription_1 = Subscription_1$2; 887 | var ObjectUnsubscribedError_1$1 = ObjectUnsubscribedError_1$2; 888 | var SubjectSubscription_1 = SubjectSubscription_1$1; 889 | var rxSubscriber_1 = rxSubscriber; 890 | /** 891 | * @class SubjectSubscriber 892 | */ 893 | var SubjectSubscriber = function (_super) { 894 | __extends$1(SubjectSubscriber, _super); 895 | function SubjectSubscriber(destination) { 896 | _super.call(this, destination); 897 | this.destination = destination; 898 | } 899 | return SubjectSubscriber; 900 | }(Subscriber_1.Subscriber); 901 | var SubjectSubscriber_1 = SubjectSubscriber; 902 | /** 903 | * @class Subject 904 | */ 905 | var Subject = function (_super) { 906 | __extends$1(Subject, _super); 907 | function Subject() { 908 | _super.call(this); 909 | this.observers = []; 910 | this.closed = false; 911 | this.isStopped = false; 912 | this.hasError = false; 913 | this.thrownError = null; 914 | } 915 | Subject.prototype[rxSubscriber_1.$$rxSubscriber] = function () { 916 | return new SubjectSubscriber(this); 917 | }; 918 | Subject.prototype.lift = function (operator) { 919 | var subject = new AnonymousSubject(this, this); 920 | subject.operator = operator; 921 | return subject; 922 | }; 923 | Subject.prototype.next = function (value) { 924 | if (this.closed) { 925 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 926 | } 927 | if (!this.isStopped) { 928 | var observers = this.observers; 929 | var len = observers.length; 930 | var copy = observers.slice(); 931 | for (var i = 0; i < len; i++) { 932 | copy[i].next(value); 933 | } 934 | } 935 | }; 936 | Subject.prototype.error = function (err) { 937 | if (this.closed) { 938 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 939 | } 940 | this.hasError = true; 941 | this.thrownError = err; 942 | this.isStopped = true; 943 | var observers = this.observers; 944 | var len = observers.length; 945 | var copy = observers.slice(); 946 | for (var i = 0; i < len; i++) { 947 | copy[i].error(err); 948 | } 949 | this.observers.length = 0; 950 | }; 951 | Subject.prototype.complete = function () { 952 | if (this.closed) { 953 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 954 | } 955 | this.isStopped = true; 956 | var observers = this.observers; 957 | var len = observers.length; 958 | var copy = observers.slice(); 959 | for (var i = 0; i < len; i++) { 960 | copy[i].complete(); 961 | } 962 | this.observers.length = 0; 963 | }; 964 | Subject.prototype.unsubscribe = function () { 965 | this.isStopped = true; 966 | this.closed = true; 967 | this.observers = null; 968 | }; 969 | Subject.prototype._subscribe = function (subscriber) { 970 | if (this.closed) { 971 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 972 | } else if (this.hasError) { 973 | subscriber.error(this.thrownError); 974 | return Subscription_1.Subscription.EMPTY; 975 | } else if (this.isStopped) { 976 | subscriber.complete(); 977 | return Subscription_1.Subscription.EMPTY; 978 | } else { 979 | this.observers.push(subscriber); 980 | return new SubjectSubscription_1.SubjectSubscription(this, subscriber); 981 | } 982 | }; 983 | Subject.prototype.asObservable = function () { 984 | var observable = new Observable_1.Observable(); 985 | observable.source = this; 986 | return observable; 987 | }; 988 | Subject.create = function (destination, source) { 989 | return new AnonymousSubject(destination, source); 990 | }; 991 | return Subject; 992 | }(Observable_1.Observable); 993 | var Subject_2 = Subject; 994 | /** 995 | * @class AnonymousSubject 996 | */ 997 | var AnonymousSubject = function (_super) { 998 | __extends$1(AnonymousSubject, _super); 999 | function AnonymousSubject(destination, source) { 1000 | _super.call(this); 1001 | this.destination = destination; 1002 | this.source = source; 1003 | } 1004 | AnonymousSubject.prototype.next = function (value) { 1005 | var destination = this.destination; 1006 | if (destination && destination.next) { 1007 | destination.next(value); 1008 | } 1009 | }; 1010 | AnonymousSubject.prototype.error = function (err) { 1011 | var destination = this.destination; 1012 | if (destination && destination.error) { 1013 | this.destination.error(err); 1014 | } 1015 | }; 1016 | AnonymousSubject.prototype.complete = function () { 1017 | var destination = this.destination; 1018 | if (destination && destination.complete) { 1019 | this.destination.complete(); 1020 | } 1021 | }; 1022 | AnonymousSubject.prototype._subscribe = function (subscriber) { 1023 | var source = this.source; 1024 | if (source) { 1025 | return this.source.subscribe(subscriber); 1026 | } else { 1027 | return Subscription_1.Subscription.EMPTY; 1028 | } 1029 | }; 1030 | return AnonymousSubject; 1031 | }(Subject); 1032 | var AnonymousSubject_1 = AnonymousSubject; 1033 | 1034 | var Subject_1$1 = { 1035 | SubjectSubscriber: SubjectSubscriber_1, 1036 | Subject: Subject_2, 1037 | AnonymousSubject: AnonymousSubject_1 1038 | }; 1039 | 1040 | var __extends = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 1041 | for (var p in b) { 1042 | if (b.hasOwnProperty(p)) d[p] = b[p]; 1043 | }function __() { 1044 | this.constructor = d; 1045 | } 1046 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 1047 | }; 1048 | var Subject_1 = Subject_1$1; 1049 | var ObjectUnsubscribedError_1 = ObjectUnsubscribedError_1$2; 1050 | /** 1051 | * @class BehaviorSubject 1052 | */ 1053 | var BehaviorSubject = function (_super) { 1054 | __extends(BehaviorSubject, _super); 1055 | function BehaviorSubject(_value) { 1056 | _super.call(this); 1057 | this._value = _value; 1058 | } 1059 | Object.defineProperty(BehaviorSubject.prototype, "value", { 1060 | get: function get() { 1061 | return this.getValue(); 1062 | }, 1063 | enumerable: true, 1064 | configurable: true 1065 | }); 1066 | BehaviorSubject.prototype._subscribe = function (subscriber) { 1067 | var subscription = _super.prototype._subscribe.call(this, subscriber); 1068 | if (subscription && !subscription.closed) { 1069 | subscriber.next(this._value); 1070 | } 1071 | return subscription; 1072 | }; 1073 | BehaviorSubject.prototype.getValue = function () { 1074 | if (this.hasError) { 1075 | throw this.thrownError; 1076 | } else if (this.closed) { 1077 | throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError(); 1078 | } else { 1079 | return this._value; 1080 | } 1081 | }; 1082 | BehaviorSubject.prototype.next = function (value) { 1083 | _super.prototype.next.call(this, this._value = value); 1084 | }; 1085 | return BehaviorSubject; 1086 | }(Subject_1.Subject); 1087 | var BehaviorSubject_2 = BehaviorSubject; 1088 | 1089 | function createRxStore(reducer, initialState) { 1090 | var state$ = new BehaviorSubject_2(initialState); 1091 | return { 1092 | state$: state$, 1093 | dispatch: function dispatch(action) { 1094 | return state$.next(reducer(state$.value, action)); 1095 | }, 1096 | subscribe: function subscribe() { 1097 | return state$.subscribe.apply(state$, arguments); 1098 | } 1099 | }; 1100 | } 1101 | 1102 | exports.createRxStore = createRxStore; 1103 | -------------------------------------------------------------------------------- /dist/rx-store.es-modules.js: -------------------------------------------------------------------------------- 1 | var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; 2 | 3 | 4 | 5 | 6 | 7 | function createCommonjsModule(fn, module) { 8 | return module = { exports: {} }, fn(module, module.exports), module.exports; 9 | } 10 | 11 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { 12 | return typeof obj; 13 | } : function (obj) { 14 | return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; 15 | }; 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | var get$1 = function get$1(object, property, receiver) { 32 | if (object === null) object = Function.prototype; 33 | var desc = Object.getOwnPropertyDescriptor(object, property); 34 | 35 | if (desc === undefined) { 36 | var parent = Object.getPrototypeOf(object); 37 | 38 | if (parent === null) { 39 | return undefined; 40 | } else { 41 | return get$1(parent, property, receiver); 42 | } 43 | } else if ("value" in desc) { 44 | return desc.value; 45 | } else { 46 | var getter = desc.get; 47 | 48 | if (getter === undefined) { 49 | return undefined; 50 | } 51 | 52 | return getter.call(receiver); 53 | } 54 | }; 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | var set = function set(object, property, value, receiver) { 73 | var desc = Object.getOwnPropertyDescriptor(object, property); 74 | 75 | if (desc === undefined) { 76 | var parent = Object.getPrototypeOf(object); 77 | 78 | if (parent !== null) { 79 | set(parent, property, value, receiver); 80 | } 81 | } else if ("value" in desc && desc.writable) { 82 | desc.value = value; 83 | } else { 84 | var setter = desc.set; 85 | 86 | if (setter !== undefined) { 87 | setter.call(receiver, value); 88 | } 89 | } 90 | 91 | return value; 92 | }; 93 | 94 | var root = createCommonjsModule(function (module, exports) { 95 | "use strict"; 96 | /** 97 | * window: browser in DOM main thread 98 | * self: browser in WebWorker 99 | * global: Node.js/other 100 | */ 101 | 102 | exports.root = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) == 'object' && window.window === window && window || (typeof self === 'undefined' ? 'undefined' : _typeof(self)) == 'object' && self.self === self && self || _typeof(commonjsGlobal) == 'object' && commonjsGlobal.global === commonjsGlobal && commonjsGlobal; 103 | if (!exports.root) { 104 | throw new Error('RxJS could not find any global context (window, self, global)'); 105 | } 106 | }); 107 | 108 | function isFunction(x) { 109 | return typeof x === 'function'; 110 | } 111 | var isFunction_2 = isFunction; 112 | 113 | var isFunction_1$1 = { 114 | isFunction: isFunction_2 115 | }; 116 | 117 | var isArray_1$1 = Array.isArray || function (x) { 118 | return x && typeof x.length === 'number'; 119 | }; 120 | 121 | var isArray = { 122 | isArray: isArray_1$1 123 | }; 124 | 125 | function isObject(x) { 126 | return x != null && (typeof x === "undefined" ? "undefined" : _typeof(x)) === 'object'; 127 | } 128 | var isObject_2 = isObject; 129 | 130 | var isObject_1$1 = { 131 | isObject: isObject_2 132 | }; 133 | 134 | // typeof any so that it we don't have to cast when comparing a result to the error object 135 | 136 | var errorObject_1$2 = { e: {} }; 137 | 138 | var errorObject = { 139 | errorObject: errorObject_1$2 140 | }; 141 | 142 | var errorObject_1$1 = errorObject; 143 | var tryCatchTarget; 144 | function tryCatcher() { 145 | try { 146 | return tryCatchTarget.apply(this, arguments); 147 | } catch (e) { 148 | errorObject_1$1.errorObject.e = e; 149 | return errorObject_1$1.errorObject; 150 | } 151 | } 152 | function tryCatch(fn) { 153 | tryCatchTarget = fn; 154 | return tryCatcher; 155 | } 156 | var tryCatch_2 = tryCatch; 157 | 158 | 159 | var tryCatch_1$1 = { 160 | tryCatch: tryCatch_2 161 | }; 162 | 163 | var __extends$3 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 164 | for (var p in b) { 165 | if (b.hasOwnProperty(p)) d[p] = b[p]; 166 | }function __() { 167 | this.constructor = d; 168 | } 169 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 170 | }; 171 | /** 172 | * An error thrown when one or more errors have occurred during the 173 | * `unsubscribe` of a {@link Subscription}. 174 | */ 175 | var UnsubscriptionError = function (_super) { 176 | __extends$3(UnsubscriptionError, _super); 177 | function UnsubscriptionError(errors) { 178 | _super.call(this); 179 | this.errors = errors; 180 | var err = Error.call(this, errors ? errors.length + " errors occurred during unsubscription:\n " + errors.map(function (err, i) { 181 | return i + 1 + ") " + err.toString(); 182 | }).join('\n ') : ''); 183 | this.name = err.name = 'UnsubscriptionError'; 184 | this.stack = err.stack; 185 | this.message = err.message; 186 | } 187 | return UnsubscriptionError; 188 | }(Error); 189 | var UnsubscriptionError_2 = UnsubscriptionError; 190 | 191 | var UnsubscriptionError_1$1 = { 192 | UnsubscriptionError: UnsubscriptionError_2 193 | }; 194 | 195 | var isArray_1 = isArray; 196 | var isObject_1 = isObject_1$1; 197 | var isFunction_1$3 = isFunction_1$1; 198 | var tryCatch_1 = tryCatch_1$1; 199 | var errorObject_1 = errorObject; 200 | var UnsubscriptionError_1 = UnsubscriptionError_1$1; 201 | /** 202 | * Represents a disposable resource, such as the execution of an Observable. A 203 | * Subscription has one important method, `unsubscribe`, that takes no argument 204 | * and just disposes the resource held by the subscription. 205 | * 206 | * Additionally, subscriptions may be grouped together through the `add()` 207 | * method, which will attach a child Subscription to the current Subscription. 208 | * When a Subscription is unsubscribed, all its children (and its grandchildren) 209 | * will be unsubscribed as well. 210 | * 211 | * @class Subscription 212 | */ 213 | var Subscription = function () { 214 | /** 215 | * @param {function(): void} [unsubscribe] A function describing how to 216 | * perform the disposal of resources when the `unsubscribe` method is called. 217 | */ 218 | function Subscription(unsubscribe) { 219 | /** 220 | * A flag to indicate whether this Subscription has already been unsubscribed. 221 | * @type {boolean} 222 | */ 223 | this.closed = false; 224 | if (unsubscribe) { 225 | this._unsubscribe = unsubscribe; 226 | } 227 | } 228 | /** 229 | * Disposes the resources held by the subscription. May, for instance, cancel 230 | * an ongoing Observable execution or cancel any other type of work that 231 | * started when the Subscription was created. 232 | * @return {void} 233 | */ 234 | Subscription.prototype.unsubscribe = function () { 235 | var hasErrors = false; 236 | var errors; 237 | if (this.closed) { 238 | return; 239 | } 240 | this.closed = true; 241 | var _a = this, 242 | _unsubscribe = _a._unsubscribe, 243 | _subscriptions = _a._subscriptions; 244 | this._subscriptions = null; 245 | if (isFunction_1$3.isFunction(_unsubscribe)) { 246 | var trial = tryCatch_1.tryCatch(_unsubscribe).call(this); 247 | if (trial === errorObject_1.errorObject) { 248 | hasErrors = true; 249 | (errors = errors || []).push(errorObject_1.errorObject.e); 250 | } 251 | } 252 | if (isArray_1.isArray(_subscriptions)) { 253 | var index = -1; 254 | var len = _subscriptions.length; 255 | while (++index < len) { 256 | var sub = _subscriptions[index]; 257 | if (isObject_1.isObject(sub)) { 258 | var trial = tryCatch_1.tryCatch(sub.unsubscribe).call(sub); 259 | if (trial === errorObject_1.errorObject) { 260 | hasErrors = true; 261 | errors = errors || []; 262 | var err = errorObject_1.errorObject.e; 263 | if (err instanceof UnsubscriptionError_1.UnsubscriptionError) { 264 | errors = errors.concat(err.errors); 265 | } else { 266 | errors.push(err); 267 | } 268 | } 269 | } 270 | } 271 | } 272 | if (hasErrors) { 273 | throw new UnsubscriptionError_1.UnsubscriptionError(errors); 274 | } 275 | }; 276 | /** 277 | * Adds a tear down to be called during the unsubscribe() of this 278 | * Subscription. 279 | * 280 | * If the tear down being added is a subscription that is already 281 | * unsubscribed, is the same reference `add` is being called on, or is 282 | * `Subscription.EMPTY`, it will not be added. 283 | * 284 | * If this subscription is already in an `closed` state, the passed 285 | * tear down logic will be executed immediately. 286 | * 287 | * @param {TeardownLogic} teardown The additional logic to execute on 288 | * teardown. 289 | * @return {Subscription} Returns the Subscription used or created to be 290 | * added to the inner subscriptions list. This Subscription can be used with 291 | * `remove()` to remove the passed teardown logic from the inner subscriptions 292 | * list. 293 | */ 294 | Subscription.prototype.add = function (teardown) { 295 | if (!teardown || teardown === Subscription.EMPTY) { 296 | return Subscription.EMPTY; 297 | } 298 | if (teardown === this) { 299 | return this; 300 | } 301 | var sub = teardown; 302 | switch (typeof teardown === 'undefined' ? 'undefined' : _typeof(teardown)) { 303 | case 'function': 304 | sub = new Subscription(teardown); 305 | case 'object': 306 | if (sub.closed || typeof sub.unsubscribe !== 'function') { 307 | break; 308 | } else if (this.closed) { 309 | sub.unsubscribe(); 310 | } else { 311 | (this._subscriptions || (this._subscriptions = [])).push(sub); 312 | } 313 | break; 314 | default: 315 | throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.'); 316 | } 317 | return sub; 318 | }; 319 | /** 320 | * Removes a Subscription from the internal list of subscriptions that will 321 | * unsubscribe during the unsubscribe process of this Subscription. 322 | * @param {Subscription} subscription The subscription to remove. 323 | * @return {void} 324 | */ 325 | Subscription.prototype.remove = function (subscription) { 326 | // HACK: This might be redundant because of the logic in `add()` 327 | if (subscription == null || subscription === this || subscription === Subscription.EMPTY) { 328 | return; 329 | } 330 | var subscriptions = this._subscriptions; 331 | if (subscriptions) { 332 | var subscriptionIndex = subscriptions.indexOf(subscription); 333 | if (subscriptionIndex !== -1) { 334 | subscriptions.splice(subscriptionIndex, 1); 335 | } 336 | } 337 | }; 338 | Subscription.EMPTY = function (empty) { 339 | empty.closed = true; 340 | return empty; 341 | }(new Subscription()); 342 | return Subscription; 343 | }(); 344 | var Subscription_2 = Subscription; 345 | 346 | var Subscription_1$2 = { 347 | Subscription: Subscription_2 348 | }; 349 | 350 | var empty = { 351 | closed: true, 352 | next: function next(value) {}, 353 | error: function error(err) { 354 | throw err; 355 | }, 356 | complete: function complete() {} 357 | }; 358 | 359 | var Observer = { 360 | empty: empty 361 | }; 362 | 363 | var root_1$2 = root; 364 | var _Symbol = root_1$2.root.Symbol; 365 | var $$rxSubscriber = typeof _Symbol === 'function' && typeof _Symbol.for === 'function' ? _Symbol.for('rxSubscriber') : '@@rxSubscriber'; 366 | 367 | var rxSubscriber = { 368 | $$rxSubscriber: $$rxSubscriber 369 | }; 370 | 371 | var __extends$2 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 372 | for (var p in b) { 373 | if (b.hasOwnProperty(p)) d[p] = b[p]; 374 | }function __() { 375 | this.constructor = d; 376 | } 377 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 378 | }; 379 | var isFunction_1 = isFunction_1$1; 380 | var Subscription_1$1 = Subscription_1$2; 381 | var Observer_1$1 = Observer; 382 | var rxSubscriber_1$2 = rxSubscriber; 383 | /** 384 | * Implements the {@link Observer} interface and extends the 385 | * {@link Subscription} class. While the {@link Observer} is the public API for 386 | * consuming the values of an {@link Observable}, all Observers get converted to 387 | * a Subscriber, in order to provide Subscription-like capabilities such as 388 | * `unsubscribe`. Subscriber is a common type in RxJS, and crucial for 389 | * implementing operators, but it is rarely used as a public API. 390 | * 391 | * @class Subscriber 392 | */ 393 | var Subscriber = function (_super) { 394 | __extends$2(Subscriber, _super); 395 | /** 396 | * @param {Observer|function(value: T): void} [destinationOrNext] A partially 397 | * defined Observer or a `next` callback function. 398 | * @param {function(e: ?any): void} [error] The `error` callback of an 399 | * Observer. 400 | * @param {function(): void} [complete] The `complete` callback of an 401 | * Observer. 402 | */ 403 | function Subscriber(destinationOrNext, error, complete) { 404 | _super.call(this); 405 | this.syncErrorValue = null; 406 | this.syncErrorThrown = false; 407 | this.syncErrorThrowable = false; 408 | this.isStopped = false; 409 | switch (arguments.length) { 410 | case 0: 411 | this.destination = Observer_1$1.empty; 412 | break; 413 | case 1: 414 | if (!destinationOrNext) { 415 | this.destination = Observer_1$1.empty; 416 | break; 417 | } 418 | if ((typeof destinationOrNext === 'undefined' ? 'undefined' : _typeof(destinationOrNext)) === 'object') { 419 | if (destinationOrNext instanceof Subscriber) { 420 | this.destination = destinationOrNext; 421 | this.destination.add(this); 422 | } else { 423 | this.syncErrorThrowable = true; 424 | this.destination = new SafeSubscriber(this, destinationOrNext); 425 | } 426 | break; 427 | } 428 | default: 429 | this.syncErrorThrowable = true; 430 | this.destination = new SafeSubscriber(this, destinationOrNext, error, complete); 431 | break; 432 | } 433 | } 434 | Subscriber.prototype[rxSubscriber_1$2.$$rxSubscriber] = function () { 435 | return this; 436 | }; 437 | /** 438 | * A static factory for a Subscriber, given a (potentially partial) definition 439 | * of an Observer. 440 | * @param {function(x: ?T): void} [next] The `next` callback of an Observer. 441 | * @param {function(e: ?any): void} [error] The `error` callback of an 442 | * Observer. 443 | * @param {function(): void} [complete] The `complete` callback of an 444 | * Observer. 445 | * @return {Subscriber} A Subscriber wrapping the (partially defined) 446 | * Observer represented by the given arguments. 447 | */ 448 | Subscriber.create = function (next, error, complete) { 449 | var subscriber = new Subscriber(next, error, complete); 450 | subscriber.syncErrorThrowable = false; 451 | return subscriber; 452 | }; 453 | /** 454 | * The {@link Observer} callback to receive notifications of type `next` from 455 | * the Observable, with a value. The Observable may call this method 0 or more 456 | * times. 457 | * @param {T} [value] The `next` value. 458 | * @return {void} 459 | */ 460 | Subscriber.prototype.next = function (value) { 461 | if (!this.isStopped) { 462 | this._next(value); 463 | } 464 | }; 465 | /** 466 | * The {@link Observer} callback to receive notifications of type `error` from 467 | * the Observable, with an attached {@link Error}. Notifies the Observer that 468 | * the Observable has experienced an error condition. 469 | * @param {any} [err] The `error` exception. 470 | * @return {void} 471 | */ 472 | Subscriber.prototype.error = function (err) { 473 | if (!this.isStopped) { 474 | this.isStopped = true; 475 | this._error(err); 476 | } 477 | }; 478 | /** 479 | * The {@link Observer} callback to receive a valueless notification of type 480 | * `complete` from the Observable. Notifies the Observer that the Observable 481 | * has finished sending push-based notifications. 482 | * @return {void} 483 | */ 484 | Subscriber.prototype.complete = function () { 485 | if (!this.isStopped) { 486 | this.isStopped = true; 487 | this._complete(); 488 | } 489 | }; 490 | Subscriber.prototype.unsubscribe = function () { 491 | if (this.closed) { 492 | return; 493 | } 494 | this.isStopped = true; 495 | _super.prototype.unsubscribe.call(this); 496 | }; 497 | Subscriber.prototype._next = function (value) { 498 | this.destination.next(value); 499 | }; 500 | Subscriber.prototype._error = function (err) { 501 | this.destination.error(err); 502 | this.unsubscribe(); 503 | }; 504 | Subscriber.prototype._complete = function () { 505 | this.destination.complete(); 506 | this.unsubscribe(); 507 | }; 508 | return Subscriber; 509 | }(Subscription_1$1.Subscription); 510 | var Subscriber_2 = Subscriber; 511 | /** 512 | * We need this JSDoc comment for affecting ESDoc. 513 | * @ignore 514 | * @extends {Ignored} 515 | */ 516 | var SafeSubscriber = function (_super) { 517 | __extends$2(SafeSubscriber, _super); 518 | function SafeSubscriber(_parent, observerOrNext, error, complete) { 519 | _super.call(this); 520 | this._parent = _parent; 521 | var next; 522 | var context = this; 523 | if (isFunction_1.isFunction(observerOrNext)) { 524 | next = observerOrNext; 525 | } else if (observerOrNext) { 526 | context = observerOrNext; 527 | next = observerOrNext.next; 528 | error = observerOrNext.error; 529 | complete = observerOrNext.complete; 530 | if (isFunction_1.isFunction(context.unsubscribe)) { 531 | this.add(context.unsubscribe.bind(context)); 532 | } 533 | context.unsubscribe = this.unsubscribe.bind(this); 534 | } 535 | this._context = context; 536 | this._next = next; 537 | this._error = error; 538 | this._complete = complete; 539 | } 540 | SafeSubscriber.prototype.next = function (value) { 541 | if (!this.isStopped && this._next) { 542 | var _parent = this._parent; 543 | if (!_parent.syncErrorThrowable) { 544 | this.__tryOrUnsub(this._next, value); 545 | } else if (this.__tryOrSetError(_parent, this._next, value)) { 546 | this.unsubscribe(); 547 | } 548 | } 549 | }; 550 | SafeSubscriber.prototype.error = function (err) { 551 | if (!this.isStopped) { 552 | var _parent = this._parent; 553 | if (this._error) { 554 | if (!_parent.syncErrorThrowable) { 555 | this.__tryOrUnsub(this._error, err); 556 | this.unsubscribe(); 557 | } else { 558 | this.__tryOrSetError(_parent, this._error, err); 559 | this.unsubscribe(); 560 | } 561 | } else if (!_parent.syncErrorThrowable) { 562 | this.unsubscribe(); 563 | throw err; 564 | } else { 565 | _parent.syncErrorValue = err; 566 | _parent.syncErrorThrown = true; 567 | this.unsubscribe(); 568 | } 569 | } 570 | }; 571 | SafeSubscriber.prototype.complete = function () { 572 | if (!this.isStopped) { 573 | var _parent = this._parent; 574 | if (this._complete) { 575 | if (!_parent.syncErrorThrowable) { 576 | this.__tryOrUnsub(this._complete); 577 | this.unsubscribe(); 578 | } else { 579 | this.__tryOrSetError(_parent, this._complete); 580 | this.unsubscribe(); 581 | } 582 | } else { 583 | this.unsubscribe(); 584 | } 585 | } 586 | }; 587 | SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) { 588 | try { 589 | fn.call(this._context, value); 590 | } catch (err) { 591 | this.unsubscribe(); 592 | throw err; 593 | } 594 | }; 595 | SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) { 596 | try { 597 | fn.call(this._context, value); 598 | } catch (err) { 599 | parent.syncErrorValue = err; 600 | parent.syncErrorThrown = true; 601 | return true; 602 | } 603 | return false; 604 | }; 605 | SafeSubscriber.prototype._unsubscribe = function () { 606 | var _parent = this._parent; 607 | this._context = null; 608 | this._parent = null; 609 | _parent.unsubscribe(); 610 | }; 611 | return SafeSubscriber; 612 | }(Subscriber); 613 | 614 | var Subscriber_1$2 = { 615 | Subscriber: Subscriber_2 616 | }; 617 | 618 | var Subscriber_1$1 = Subscriber_1$2; 619 | var rxSubscriber_1$1 = rxSubscriber; 620 | var Observer_1 = Observer; 621 | function toSubscriber(nextOrObserver, error, complete) { 622 | if (nextOrObserver) { 623 | if (nextOrObserver instanceof Subscriber_1$1.Subscriber) { 624 | return nextOrObserver; 625 | } 626 | if (nextOrObserver[rxSubscriber_1$1.$$rxSubscriber]) { 627 | return nextOrObserver[rxSubscriber_1$1.$$rxSubscriber](); 628 | } 629 | } 630 | if (!nextOrObserver && !error && !complete) { 631 | return new Subscriber_1$1.Subscriber(Observer_1.empty); 632 | } 633 | return new Subscriber_1$1.Subscriber(nextOrObserver, error, complete); 634 | } 635 | var toSubscriber_2 = toSubscriber; 636 | 637 | var toSubscriber_1$1 = { 638 | toSubscriber: toSubscriber_2 639 | }; 640 | 641 | var root_1$3 = root; 642 | function getSymbolObservable(context) { 643 | var $$observable; 644 | var _Symbol = context.Symbol; 645 | if (typeof _Symbol === 'function') { 646 | if (_Symbol.observable) { 647 | $$observable = _Symbol.observable; 648 | } else { 649 | $$observable = _Symbol('observable'); 650 | _Symbol.observable = $$observable; 651 | } 652 | } else { 653 | $$observable = '@@observable'; 654 | } 655 | return $$observable; 656 | } 657 | var getSymbolObservable_1 = getSymbolObservable; 658 | var $$observable = getSymbolObservable(root_1$3.root); 659 | 660 | var observable = { 661 | getSymbolObservable: getSymbolObservable_1, 662 | $$observable: $$observable 663 | }; 664 | 665 | var root_1 = root; 666 | var toSubscriber_1 = toSubscriber_1$1; 667 | var observable_1 = observable; 668 | /** 669 | * A representation of any set of values over any amount of time. This the most basic building block 670 | * of RxJS. 671 | * 672 | * @class Observable 673 | */ 674 | var Observable = function () { 675 | /** 676 | * @constructor 677 | * @param {Function} subscribe the function that is called when the Observable is 678 | * initially subscribed to. This function is given a Subscriber, to which new values 679 | * can be `next`ed, or an `error` method can be called to raise an error, or 680 | * `complete` can be called to notify of a successful completion. 681 | */ 682 | function Observable(subscribe) { 683 | this._isScalar = false; 684 | if (subscribe) { 685 | this._subscribe = subscribe; 686 | } 687 | } 688 | /** 689 | * Creates a new Observable, with this Observable as the source, and the passed 690 | * operator defined as the new observable's operator. 691 | * @method lift 692 | * @param {Operator} operator the operator defining the operation to take on the observable 693 | * @return {Observable} a new observable with the Operator applied 694 | */ 695 | Observable.prototype.lift = function (operator) { 696 | var observable$$1 = new Observable(); 697 | observable$$1.source = this; 698 | observable$$1.operator = operator; 699 | return observable$$1; 700 | }; 701 | Observable.prototype.subscribe = function (observerOrNext, error, complete) { 702 | var operator = this.operator; 703 | var sink = toSubscriber_1.toSubscriber(observerOrNext, error, complete); 704 | if (operator) { 705 | operator.call(sink, this); 706 | } else { 707 | sink.add(this._subscribe(sink)); 708 | } 709 | if (sink.syncErrorThrowable) { 710 | sink.syncErrorThrowable = false; 711 | if (sink.syncErrorThrown) { 712 | throw sink.syncErrorValue; 713 | } 714 | } 715 | return sink; 716 | }; 717 | /** 718 | * @method forEach 719 | * @param {Function} next a handler for each value emitted by the observable 720 | * @param {PromiseConstructor} [PromiseCtor] a constructor function used to instantiate the Promise 721 | * @return {Promise} a promise that either resolves on observable completion or 722 | * rejects with the handled error 723 | */ 724 | Observable.prototype.forEach = function (next, PromiseCtor) { 725 | var _this = this; 726 | if (!PromiseCtor) { 727 | if (root_1.root.Rx && root_1.root.Rx.config && root_1.root.Rx.config.Promise) { 728 | PromiseCtor = root_1.root.Rx.config.Promise; 729 | } else if (root_1.root.Promise) { 730 | PromiseCtor = root_1.root.Promise; 731 | } 732 | } 733 | if (!PromiseCtor) { 734 | throw new Error('no Promise impl found'); 735 | } 736 | return new PromiseCtor(function (resolve, reject) { 737 | var subscription = _this.subscribe(function (value) { 738 | if (subscription) { 739 | // if there is a subscription, then we can surmise 740 | // the next handling is asynchronous. Any errors thrown 741 | // need to be rejected explicitly and unsubscribe must be 742 | // called manually 743 | try { 744 | next(value); 745 | } catch (err) { 746 | reject(err); 747 | subscription.unsubscribe(); 748 | } 749 | } else { 750 | // if there is NO subscription, then we're getting a nexted 751 | // value synchronously during subscription. We can just call it. 752 | // If it errors, Observable's `subscribe` will ensure the 753 | // unsubscription logic is called, then synchronously rethrow the error. 754 | // After that, Promise will trap the error and send it 755 | // down the rejection path. 756 | next(value); 757 | } 758 | }, reject, resolve); 759 | }); 760 | }; 761 | Observable.prototype._subscribe = function (subscriber) { 762 | return this.source.subscribe(subscriber); 763 | }; 764 | /** 765 | * An interop point defined by the es7-observable spec https://github.com/zenparsing/es-observable 766 | * @method Symbol.observable 767 | * @return {Observable} this instance of the observable 768 | */ 769 | Observable.prototype[observable_1.$$observable] = function () { 770 | return this; 771 | }; 772 | // HACK: Since TypeScript inherits static properties too, we have to 773 | // fight against TypeScript here so Subject can have a different static create signature 774 | /** 775 | * Creates a new cold Observable by calling the Observable constructor 776 | * @static true 777 | * @owner Observable 778 | * @method create 779 | * @param {Function} subscribe? the subscriber function to be passed to the Observable constructor 780 | * @return {Observable} a new cold observable 781 | */ 782 | Observable.create = function (subscribe) { 783 | return new Observable(subscribe); 784 | }; 785 | return Observable; 786 | }(); 787 | var Observable_2 = Observable; 788 | 789 | var Observable_1$1 = { 790 | Observable: Observable_2 791 | }; 792 | 793 | var __extends$4 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 794 | for (var p in b) { 795 | if (b.hasOwnProperty(p)) d[p] = b[p]; 796 | }function __() { 797 | this.constructor = d; 798 | } 799 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 800 | }; 801 | /** 802 | * An error thrown when an action is invalid because the object has been 803 | * unsubscribed. 804 | * 805 | * @see {@link Subject} 806 | * @see {@link BehaviorSubject} 807 | * 808 | * @class ObjectUnsubscribedError 809 | */ 810 | var ObjectUnsubscribedError = function (_super) { 811 | __extends$4(ObjectUnsubscribedError, _super); 812 | function ObjectUnsubscribedError() { 813 | var err = _super.call(this, 'object unsubscribed'); 814 | this.name = err.name = 'ObjectUnsubscribedError'; 815 | this.stack = err.stack; 816 | this.message = err.message; 817 | } 818 | return ObjectUnsubscribedError; 819 | }(Error); 820 | var ObjectUnsubscribedError_2 = ObjectUnsubscribedError; 821 | 822 | var ObjectUnsubscribedError_1$2 = { 823 | ObjectUnsubscribedError: ObjectUnsubscribedError_2 824 | }; 825 | 826 | var __extends$5 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 827 | for (var p in b) { 828 | if (b.hasOwnProperty(p)) d[p] = b[p]; 829 | }function __() { 830 | this.constructor = d; 831 | } 832 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 833 | }; 834 | var Subscription_1$4 = Subscription_1$2; 835 | /** 836 | * We need this JSDoc comment for affecting ESDoc. 837 | * @ignore 838 | * @extends {Ignored} 839 | */ 840 | var SubjectSubscription = function (_super) { 841 | __extends$5(SubjectSubscription, _super); 842 | function SubjectSubscription(subject, subscriber) { 843 | _super.call(this); 844 | this.subject = subject; 845 | this.subscriber = subscriber; 846 | this.closed = false; 847 | } 848 | SubjectSubscription.prototype.unsubscribe = function () { 849 | if (this.closed) { 850 | return; 851 | } 852 | this.closed = true; 853 | var subject = this.subject; 854 | var observers = subject.observers; 855 | this.subject = null; 856 | if (!observers || observers.length === 0 || subject.isStopped || subject.closed) { 857 | return; 858 | } 859 | var subscriberIndex = observers.indexOf(this.subscriber); 860 | if (subscriberIndex !== -1) { 861 | observers.splice(subscriberIndex, 1); 862 | } 863 | }; 864 | return SubjectSubscription; 865 | }(Subscription_1$4.Subscription); 866 | var SubjectSubscription_2 = SubjectSubscription; 867 | 868 | var SubjectSubscription_1$1 = { 869 | SubjectSubscription: SubjectSubscription_2 870 | }; 871 | 872 | var __extends$1 = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 873 | for (var p in b) { 874 | if (b.hasOwnProperty(p)) d[p] = b[p]; 875 | }function __() { 876 | this.constructor = d; 877 | } 878 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 879 | }; 880 | var Observable_1 = Observable_1$1; 881 | var Subscriber_1 = Subscriber_1$2; 882 | var Subscription_1 = Subscription_1$2; 883 | var ObjectUnsubscribedError_1$1 = ObjectUnsubscribedError_1$2; 884 | var SubjectSubscription_1 = SubjectSubscription_1$1; 885 | var rxSubscriber_1 = rxSubscriber; 886 | /** 887 | * @class SubjectSubscriber 888 | */ 889 | var SubjectSubscriber = function (_super) { 890 | __extends$1(SubjectSubscriber, _super); 891 | function SubjectSubscriber(destination) { 892 | _super.call(this, destination); 893 | this.destination = destination; 894 | } 895 | return SubjectSubscriber; 896 | }(Subscriber_1.Subscriber); 897 | var SubjectSubscriber_1 = SubjectSubscriber; 898 | /** 899 | * @class Subject 900 | */ 901 | var Subject = function (_super) { 902 | __extends$1(Subject, _super); 903 | function Subject() { 904 | _super.call(this); 905 | this.observers = []; 906 | this.closed = false; 907 | this.isStopped = false; 908 | this.hasError = false; 909 | this.thrownError = null; 910 | } 911 | Subject.prototype[rxSubscriber_1.$$rxSubscriber] = function () { 912 | return new SubjectSubscriber(this); 913 | }; 914 | Subject.prototype.lift = function (operator) { 915 | var subject = new AnonymousSubject(this, this); 916 | subject.operator = operator; 917 | return subject; 918 | }; 919 | Subject.prototype.next = function (value) { 920 | if (this.closed) { 921 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 922 | } 923 | if (!this.isStopped) { 924 | var observers = this.observers; 925 | var len = observers.length; 926 | var copy = observers.slice(); 927 | for (var i = 0; i < len; i++) { 928 | copy[i].next(value); 929 | } 930 | } 931 | }; 932 | Subject.prototype.error = function (err) { 933 | if (this.closed) { 934 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 935 | } 936 | this.hasError = true; 937 | this.thrownError = err; 938 | this.isStopped = true; 939 | var observers = this.observers; 940 | var len = observers.length; 941 | var copy = observers.slice(); 942 | for (var i = 0; i < len; i++) { 943 | copy[i].error(err); 944 | } 945 | this.observers.length = 0; 946 | }; 947 | Subject.prototype.complete = function () { 948 | if (this.closed) { 949 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 950 | } 951 | this.isStopped = true; 952 | var observers = this.observers; 953 | var len = observers.length; 954 | var copy = observers.slice(); 955 | for (var i = 0; i < len; i++) { 956 | copy[i].complete(); 957 | } 958 | this.observers.length = 0; 959 | }; 960 | Subject.prototype.unsubscribe = function () { 961 | this.isStopped = true; 962 | this.closed = true; 963 | this.observers = null; 964 | }; 965 | Subject.prototype._subscribe = function (subscriber) { 966 | if (this.closed) { 967 | throw new ObjectUnsubscribedError_1$1.ObjectUnsubscribedError(); 968 | } else if (this.hasError) { 969 | subscriber.error(this.thrownError); 970 | return Subscription_1.Subscription.EMPTY; 971 | } else if (this.isStopped) { 972 | subscriber.complete(); 973 | return Subscription_1.Subscription.EMPTY; 974 | } else { 975 | this.observers.push(subscriber); 976 | return new SubjectSubscription_1.SubjectSubscription(this, subscriber); 977 | } 978 | }; 979 | Subject.prototype.asObservable = function () { 980 | var observable = new Observable_1.Observable(); 981 | observable.source = this; 982 | return observable; 983 | }; 984 | Subject.create = function (destination, source) { 985 | return new AnonymousSubject(destination, source); 986 | }; 987 | return Subject; 988 | }(Observable_1.Observable); 989 | var Subject_2 = Subject; 990 | /** 991 | * @class AnonymousSubject 992 | */ 993 | var AnonymousSubject = function (_super) { 994 | __extends$1(AnonymousSubject, _super); 995 | function AnonymousSubject(destination, source) { 996 | _super.call(this); 997 | this.destination = destination; 998 | this.source = source; 999 | } 1000 | AnonymousSubject.prototype.next = function (value) { 1001 | var destination = this.destination; 1002 | if (destination && destination.next) { 1003 | destination.next(value); 1004 | } 1005 | }; 1006 | AnonymousSubject.prototype.error = function (err) { 1007 | var destination = this.destination; 1008 | if (destination && destination.error) { 1009 | this.destination.error(err); 1010 | } 1011 | }; 1012 | AnonymousSubject.prototype.complete = function () { 1013 | var destination = this.destination; 1014 | if (destination && destination.complete) { 1015 | this.destination.complete(); 1016 | } 1017 | }; 1018 | AnonymousSubject.prototype._subscribe = function (subscriber) { 1019 | var source = this.source; 1020 | if (source) { 1021 | return this.source.subscribe(subscriber); 1022 | } else { 1023 | return Subscription_1.Subscription.EMPTY; 1024 | } 1025 | }; 1026 | return AnonymousSubject; 1027 | }(Subject); 1028 | var AnonymousSubject_1 = AnonymousSubject; 1029 | 1030 | var Subject_1$1 = { 1031 | SubjectSubscriber: SubjectSubscriber_1, 1032 | Subject: Subject_2, 1033 | AnonymousSubject: AnonymousSubject_1 1034 | }; 1035 | 1036 | var __extends = commonjsGlobal && commonjsGlobal.__extends || function (d, b) { 1037 | for (var p in b) { 1038 | if (b.hasOwnProperty(p)) d[p] = b[p]; 1039 | }function __() { 1040 | this.constructor = d; 1041 | } 1042 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 1043 | }; 1044 | var Subject_1 = Subject_1$1; 1045 | var ObjectUnsubscribedError_1 = ObjectUnsubscribedError_1$2; 1046 | /** 1047 | * @class BehaviorSubject 1048 | */ 1049 | var BehaviorSubject = function (_super) { 1050 | __extends(BehaviorSubject, _super); 1051 | function BehaviorSubject(_value) { 1052 | _super.call(this); 1053 | this._value = _value; 1054 | } 1055 | Object.defineProperty(BehaviorSubject.prototype, "value", { 1056 | get: function get() { 1057 | return this.getValue(); 1058 | }, 1059 | enumerable: true, 1060 | configurable: true 1061 | }); 1062 | BehaviorSubject.prototype._subscribe = function (subscriber) { 1063 | var subscription = _super.prototype._subscribe.call(this, subscriber); 1064 | if (subscription && !subscription.closed) { 1065 | subscriber.next(this._value); 1066 | } 1067 | return subscription; 1068 | }; 1069 | BehaviorSubject.prototype.getValue = function () { 1070 | if (this.hasError) { 1071 | throw this.thrownError; 1072 | } else if (this.closed) { 1073 | throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError(); 1074 | } else { 1075 | return this._value; 1076 | } 1077 | }; 1078 | BehaviorSubject.prototype.next = function (value) { 1079 | _super.prototype.next.call(this, this._value = value); 1080 | }; 1081 | return BehaviorSubject; 1082 | }(Subject_1.Subject); 1083 | var BehaviorSubject_2 = BehaviorSubject; 1084 | 1085 | function createRxStore(reducer, initialState) { 1086 | var state$ = new BehaviorSubject_2(initialState); 1087 | return { 1088 | state$: state$, 1089 | dispatch: function dispatch(action) { 1090 | return state$.next(reducer(state$.value, action)); 1091 | }, 1092 | subscribe: function subscribe() { 1093 | return state$.subscribe.apply(state$, arguments); 1094 | } 1095 | }; 1096 | } 1097 | 1098 | export { createRxStore }; 1099 | -------------------------------------------------------------------------------- /dist/rx-store.umd.min.js: -------------------------------------------------------------------------------- 1 | !function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define("rx-store",["exports"],r):r(t.RxStore=t.RxStore||{})}(this,function(t){"use strict";function r(t,r){return r={exports:{}},t(r,r.exports),r.exports}function e(t){return"function"==typeof t}function o(t){return null!=t&&"object"===("undefined"==typeof t?"undefined":h(t))}function n(){try{return b.apply(this,arguments)}catch(t){return x.errorObject.e=t,x.errorObject}}function i(t){return b=t,n}function s(t,r,e){if(t){if(t instanceof X.Subscriber)return t;if(t[Z.$$rxSubscriber])return t[Z.$$rxSubscriber]()}return t||r||e?new X.Subscriber(t,r,e):new X.Subscriber(tt.empty)}function c(t){var r,e=t.Symbol;return"function"==typeof e?e.observable?r=e.observable:(r=e("observable"),e.observable=r):r="@@observable",r}function u(t,r){var e=new Jt(r);return{state$:e,dispatch:function(r){return e.next(t(e.value,r))},subscribe:function(){return e.subscribe.apply(e,arguments)}}}var b,p="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},f=r(function(t,r){if(r.root="object"==("undefined"==typeof window?"undefined":h(window))&&window.window===window&&window||"object"==("undefined"==typeof self?"undefined":h(self))&&self.self===self&&self||"object"==h(p)&&p.global===p&&p,!r.root)throw new Error("RxJS could not find any global context (window, self, global)")}),a=e,l={isFunction:a},y=Array.isArray||function(t){return t&&"number"==typeof t.length},d={isArray:y},w=o,_={isObject:w},v={e:{}},S={errorObject:v},x=S,E=i,m={tryCatch:E},O=p&&p.__extends||function(t,r){function e(){this.constructor=t}for(var o in r)r.hasOwnProperty(o)&&(t[o]=r[o]);t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)},j=function(t){function r(r){t.call(this),this.errors=r;var e=Error.call(this,r?r.length+" errors occurred during unsubscription:\n "+r.map(function(t,r){return r+1+") "+t.toString()}).join("\n "):"");this.name=e.name="UnsubscriptionError",this.stack=e.stack,this.message=e.message}return O(r,t),r}(Error),g=j,T={UnsubscriptionError:g},P=d,U=_,$=l,k=m,M=S,R=T,Y=function(){function t(t){this.closed=!1,t&&(this._unsubscribe=t)}return t.prototype.unsubscribe=function(){var t,r=!1;if(!this.closed){this.closed=!0;var e=this,o=e._unsubscribe,n=e._subscriptions;if(this._subscriptions=null,$.isFunction(o)){var i=k.tryCatch(o).call(this);i===M.errorObject&&(r=!0,(t=t||[]).push(M.errorObject.e))}if(P.isArray(n))for(var s=-1,c=n.length;++s dist/rx-store.browser.min.js", 13 | "minify-umd": "$(npm bin)/uglifyjs dist/rx-store.umd.js -cm > dist/rx-store.umd.min.js", 14 | "lint": "$(npm bin)/eslint src/**/*.js test/**/*.js", 15 | "test": "$(npm bin)/mocha" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/jdlehman/rx-store.git" 20 | }, 21 | "bugs": { 22 | "url": "https://github.com/jdlehman/rx-store/issues" 23 | }, 24 | "keywords": [ 25 | "state", 26 | "reactive", 27 | "observable", 28 | "rx", 29 | "rxjs", 30 | "flux" 31 | ], 32 | "files": [ 33 | "dist" 34 | ], 35 | "author": "Jonathan Lehman ", 36 | "license": "MIT", 37 | "devDependencies": { 38 | "babel-eslint": "^7.1.1", 39 | "babel-preset-es2015": "^6.18.0", 40 | "babel-preset-es2015-rollup": "^1.2.0", 41 | "babel-preset-stage-2": "^6.18.0", 42 | "babel-register": "^6.18.0", 43 | "chai": "^3.5.0", 44 | "eslint": "^3.11.1", 45 | "mocha": "^3.2.0", 46 | "rollup": "^0.36.4", 47 | "rollup-plugin-babel": "^2.6.1", 48 | "rollup-plugin-commonjs": "^5.0.5", 49 | "rollup-plugin-node-resolve": "^2.0.0", 50 | "rollup-plugin-replace": "^1.1.1", 51 | "sinon": "^1.17.6", 52 | "uglify-js": "^2.7.5" 53 | }, 54 | "dependencies": { 55 | "rxjs": "^5.0.0-rc.5" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from 'rollup-plugin-babel'; 2 | import nodeResolve from 'rollup-plugin-node-resolve'; 3 | import replace from 'rollup-plugin-replace'; 4 | import commonjs from 'rollup-plugin-commonjs'; 5 | 6 | export default { 7 | entry: 'src/index.js', 8 | plugins: [ 9 | nodeResolve(), 10 | commonjs({ 11 | include: 'node_modules/**' 12 | }), 13 | babel({ 14 | babelrc: false, 15 | presets: ['es2015-rollup', 'stage-2'] 16 | }), 17 | replace({ 18 | 'process.env.NODE_DEBUG': false, 19 | 'process.env.NODE_ENV': 'production' 20 | }) 21 | ], 22 | moduleName: 'RxStore', 23 | moduleId: 'rx-store', 24 | targets: [ 25 | {format: 'umd', dest: 'dist/rx-store.umd.js'}, 26 | {format: 'iife', dest: 'dist/rx-store.browser.js'}, 27 | {format: 'amd', dest: 'dist/rx-store.amd.js'}, 28 | {format: 'cjs', dest: 'dist/rx-store.cjs.js'}, 29 | {format: 'es', dest: 'dist/rx-store.es-modules.js'} 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import {BehaviorSubject} from 'rxjs/BehaviorSubject'; 2 | 3 | export function createRxStore(reducer, initialState) { 4 | const state$ = new BehaviorSubject(initialState); 5 | return { 6 | state$, 7 | dispatch: action => state$.next(reducer(state$.value, action)), 8 | subscribe: (...args) => state$.subscribe(...args) 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /test/index_test.js: -------------------------------------------------------------------------------- 1 | import {assert} from 'chai'; 2 | import sinon from 'sinon'; 3 | import {createRxStore} from '../src'; 4 | 5 | function reducer(state, action) { 6 | switch (action.type) { 7 | case 'ADD': 8 | return state + action.payload; 9 | case 'DECREMENT': 10 | return state - action.payload; 11 | default: 12 | return state; 13 | } 14 | } 15 | 16 | const initialState = 0; 17 | 18 | describe('createRxStore', () => { 19 | describe('state$', () => { 20 | it('returns raw state stream', () => { 21 | const {state$} = createRxStore(reducer, initialState); 22 | assert.equal(typeof state$.subscribe, 'function'); 23 | }); 24 | }); 25 | 26 | describe('subscribe', () => { 27 | it('starts with the initialState', () => { 28 | const store = createRxStore(reducer, initialState); 29 | const states = []; 30 | const subscription = store.subscribe(data => states.push(data)); 31 | assert.equal(states.join(''), '0'); 32 | subscription.unsubscribe(); 33 | }); 34 | 35 | it('keeps state in sync between subscribers', () => { 36 | const store = createRxStore(reducer, initialState); 37 | const states = []; 38 | const states2 = []; 39 | const subscription = store.subscribe(data => states.push(data)); 40 | const subscription2 = store.subscribe(data => states2.push(data)); 41 | store.dispatch({type: 'ADD', payload: 2}); 42 | store.dispatch({type: 'DECREMENT', payload: 4}); 43 | store.dispatch({type: 'ADD', payload: 10}); 44 | 45 | assert.equal(states.join(' '), '0 2 -2 8'); 46 | assert.equal(states2.join(' '), '0 2 -2 8'); 47 | 48 | subscription.unsubscribe(); 49 | subscription2.unsubscribe(); 50 | }); 51 | 52 | it('subscriptions can be unsubscribed', () => { 53 | const store = createRxStore(reducer, initialState); 54 | const states = []; 55 | const subscription = store.subscribe(data => states.push(data)); 56 | store.dispatch({type: 'ADD', payload: 2}); 57 | store.dispatch({type: 'ADD', payload: 10}); 58 | subscription.unsubscribe(); 59 | store.dispatch({type: 'ADD', payload: 2}); 60 | 61 | assert.equal(states.join(' '), '0 2 12'); 62 | }); 63 | 64 | it('new subscribers start with the current state', () => { 65 | const store = createRxStore(reducer, initialState); 66 | const states = []; 67 | const states2 = []; 68 | const states3 = []; 69 | const subscription = store.subscribe(data => states.push(data)); 70 | store.dispatch({type: 'ADD', payload: 2}); 71 | store.dispatch({type: 'DECREMENT', payload: 4}); 72 | const subscription2 = store.subscribe(data => states2.push(data)); 73 | store.dispatch({type: 'ADD', payload: 10}); 74 | const subscription3 = store.subscribe(data => states3.push(data)); 75 | store.dispatch({type: 'DECREMENT', payload: 2}); 76 | 77 | assert.equal(states.join(' '), '0 2 -2 8 6'); 78 | assert.equal(states2.join(' '), '-2 8 6'); 79 | assert.equal(states3.join(' '), '8 6'); 80 | 81 | subscription.unsubscribe(); 82 | subscription2.unsubscribe(); 83 | subscription3.unsubscribe(); 84 | }); 85 | }); 86 | 87 | describe('dispatch', () => { 88 | it('calls reducer once on each action dispatched', () => { 89 | const reducer = (state, action) => state; 90 | const spy = sinon.spy(reducer); 91 | const store = createRxStore(spy, initialState); 92 | const subscription = store.subscribe(); 93 | 94 | store.dispatch(); 95 | assert.equal(spy.callCount, 1); 96 | store.dispatch({data: 'test'}); 97 | assert.equal(spy.callCount, 2); 98 | 99 | subscription.unsubscribe(); 100 | }); 101 | 102 | it('only hits the reducer once per dispatch despite number of subscribers', () => { 103 | const reducer = (state, action) => state; 104 | const spy = sinon.spy(reducer); 105 | const store = createRxStore(spy, initialState); 106 | const subscription = store.subscribe(); 107 | const subscription2 = store.subscribe(); 108 | 109 | store.dispatch(); 110 | assert.equal(spy.callCount, 1); 111 | store.dispatch({data: 'test'}); 112 | assert.equal(spy.callCount, 2); 113 | store.dispatch({data: 'test'}); 114 | assert.equal(spy.callCount, 3); 115 | const subscription3 = store.subscribe(); 116 | const subscription4 = store.subscribe(); 117 | store.dispatch({data: 'test'}); 118 | assert.equal(spy.callCount, 4); 119 | store.dispatch({data: 'test'}); 120 | assert.equal(spy.callCount, 5); 121 | 122 | subscription.unsubscribe(); 123 | subscription2.unsubscribe(); 124 | subscription3.unsubscribe(); 125 | subscription4.unsubscribe(); 126 | }); 127 | 128 | it('passes current state and action through each dispatch', () => { 129 | const reducer = (state, action) => state; 130 | const spy = sinon.spy(reducer); 131 | const store = createRxStore(spy, initialState); 132 | const subscription = store.subscribe(); 133 | 134 | store.dispatch(); 135 | sinon.assert.calledWith(spy, initialState, undefined); 136 | store.dispatch({data: 'test'}); 137 | sinon.assert.calledWith(spy, initialState, {data: 'test'}); 138 | 139 | subscription.unsubscribe(); 140 | }); 141 | }); 142 | }); 143 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers js:babel-register --recursive 2 | --------------------------------------------------------------------------------