UNPKG

@angular-redux/store

Version:
84 lines 3.95 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var redux_1 = require("redux"); var Observable_1 = require("rxjs/Observable"); var BehaviorSubject_1 = require("rxjs/BehaviorSubject"); require("rxjs/add/operator/map"); require("rxjs/add/operator/filter"); require("rxjs/add/operator/distinctUntilChanged"); require("rxjs/add/operator/switchMap"); var ng_redux_1 = require("./ng-redux"); var selectors_1 = require("./selectors"); var assert_1 = require("../utils/assert"); var sub_store_1 = require("./sub-store"); var fractal_reducer_map_1 = require("./fractal-reducer-map"); /** @hidden */ var RootStore = (function (_super) { __extends(RootStore, _super); function RootStore(ngZone) { var _this = _super.call(this) || this; _this.ngZone = ngZone; _this.configureStore = function (rootReducer, initState, middleware, enhancers) { if (middleware === void 0) { middleware = []; } if (enhancers === void 0) { enhancers = []; } assert_1.assert(!_this._store, 'Store already configured!'); // Variable-arity compose in typescript FTW. _this.setStore(redux_1.compose.apply(null, [redux_1.applyMiddleware.apply(void 0, middleware)].concat(enhancers))(redux_1.createStore)(fractal_reducer_map_1.enableFractalReducers(rootReducer), initState)); }; _this.provideStore = function (store) { assert_1.assert(!_this._store, 'Store already configured!'); _this.setStore(store); }; _this.getState = function () { return _this._store.getState(); }; _this.subscribe = function (listener) { return _this._store.subscribe(listener); }; _this.replaceReducer = function (nextReducer) { _this._store.replaceReducer(nextReducer); }; _this.dispatch = function (action) { assert_1.assert(!!_this._store, 'Dispatch failed: did you forget to configure your store? ' + 'https://github.com/angular-redux/@angular-redux/core/blob/master/' + 'README.md#quick-start'); return _this.ngZone.run(function () { return _this._store.dispatch(action); }); }; _this.select = function (selector, comparator) { return _this._store$ .distinctUntilChanged() .map(selectors_1.resolveToFunctionSelector(selector)) .distinctUntilChanged(comparator); }; _this.configureSubStore = function (basePath, localReducer) { return new sub_store_1.SubStore(_this, basePath, localReducer); }; _this.storeToObservable = function (store) { return new Observable_1.Observable(function (observer) { observer.next(store.getState()); store.subscribe(function () { return observer.next(store.getState()); }); }); }; ng_redux_1.NgRedux.instance = _this; _this._store$ = new BehaviorSubject_1.BehaviorSubject(undefined) .filter(function (n) { return n !== undefined; }) .switchMap(function (n) { return _this.storeToObservable(n); }); return _this; } RootStore.prototype.setStore = function (store) { this._store = store; this._store$.next(store); }; return RootStore; }(ng_redux_1.NgRedux)); exports.RootStore = RootStore; ; //# sourceMappingURL=root-store.js.map