UNPKG

@sigi/core

Version:
118 lines 4.14 kB
import { BehaviorSubject, ReplaySubject, Subject, Subscription, identity, NEVER } from 'rxjs'; import { last, share, switchMap, takeUntil } from 'rxjs/operators'; import { logStoreAction } from './logger'; import { INIT_ACTION_TYPE_SYMBOL, TERMINATE_ACTION_TYPE_SYMBOL, NOOP_ACTION_TYPE_SYMBOL } from './symbols'; var Store = (function () { function Store(name, reducer, epic) { if (reducer === void 0) { reducer = identity; } if (epic === void 0) { epic = function () { return NEVER; }; } this.state$ = new ReplaySubject(1); this.action$ = new Subject(); this.isReady = false; this.actionSub = new Subscription(); this.initAction = { type: INIT_ACTION_TYPE_SYMBOL, payload: null, store: this, }; this.name = name; this.reducer = reducer; this.epic$ = new BehaviorSubject(epic); } Object.defineProperty(Store.prototype, "state", { get: function () { return this.internalState; }, enumerable: false, configurable: true }); Object.defineProperty(Store.prototype, "ready", { get: function () { return this.isReady; }, enumerable: false, configurable: true }); Store.prototype.setup = function (defaultState) { this.internalState = defaultState; this.state$.next(defaultState); this.subscribeAction(); this.log(this.initAction); this.isReady = true; }; Store.prototype.addEpic = function (combineEpic) { var _this = this; var epic$ = this.epic$; var prevEpic = epic$.getValue(); epic$.next(combineEpic(function (action$) { var output$; if (action$ instanceof Subject) { output$ = prevEpic(action$); } else { output$ = prevEpic(action$.pipe(share())); } return output$.pipe(takeUntil(_this.action$.pipe(last(null, null)))); })); return function () { _this.epic$.next(prevEpic); }; }; Store.prototype.dispatch = function (action) { if (action.type === NOOP_ACTION_TYPE_SYMBOL) { return; } if (action.store !== this) { action.store.dispatch(action); return; } var prevState = this.internalState; var newState = this.reducer(prevState, action); if (newState !== prevState) { if (process.env.NODE_ENV !== 'production' && newState === undefined) { console.warn("".concat(action.type, " produced an undefined state, you may forget to return new State in @Reducer")); } this.internalState = newState; this.state$.next(newState); } this.log(action); this.action$.next(action); }; Store.prototype.log = function (action) { if (action.type !== TERMINATE_ACTION_TYPE_SYMBOL) { logStoreAction(action); } }; Store.prototype.dispose = function () { this.actionSub.unsubscribe(); this.action$.complete(); this.state$.complete(); this.epic$.complete(); }; Store.prototype.subscribeAction = function () { var _this = this; this.actionSub = this.epic$ .pipe(switchMap(function (epic) { return epic(_this.action$).pipe(takeUntil(_this.action$.pipe(last(null, null)))); })) .subscribe({ next: function (action) { try { _this.dispatch(action); } catch (e) { if (process.env.NODE_ENV === 'development') { console.error(e); } _this.action$.error(e); } }, error: function (e) { if (!_this.action$.closed) { _this.action$.error(e); } }, }); }; return Store; }()); export { Store }; //# sourceMappingURL=store.js.map