UNPKG

rxact

Version:

an observable application management for Javascript apps

66 lines (52 loc) 1.36 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var disposedFn = function disposedFn() { console.warn('You are calling function on a disposed StateStream.'); }; function stateFactory(initialState) { var _this = this; var disposed = false; var state = initialState; this.observers = []; this.getState = function () { return state; }; this.next = function (updater) { if (disposed) { return; } if (typeof updater !== 'function') { throw new Error('Expected passing a function to emitState.'); } var nextState = updater(state); state = nextState; _this.observers.forEach(function (observer) { observer.next(nextState); }); }; var state$ = new this.Observable(function (observer) { _this.observers.push(observer); observer.next(state); return { unsubscribe: function unsubscribe() { _this.observers = _this.observers.filter(function (item) { return item !== observer; }); } }; }); this.subscriptions.push({ unsubscribe: function unsubscribe() { disposed = true; _this.next = disposedFn; _this.observers.forEach(function (observer) { observer.complete(); }); _this.observers = []; } }); return state$; } exports.default = stateFactory;