1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var rxjs_1 = require("rxjs");
|
4 | var operators_1 = require("rxjs/operators");
|
5 | var shallowEqual_1 = require("./shallowEqual");
|
6 | var isPlainObject = require("lodash.isplainobject");
|
7 | var isObject = require("lodash.isobject");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function createState(stateMutators, initialState) {
|
15 | var state = stateMutators.pipe(operators_1.scan(function (state, reducer) { return reducer(state); }, initialState),
|
16 |
|
17 |
|
18 | operators_1.publishReplay(1), operators_1.refCount());
|
19 | return state;
|
20 | }
|
21 | exports.createState = createState;
|
22 | var Store = (function () {
|
23 | function Store(state, stateMutators, forwardProjections, backwardProjections, onDestroy, notifyRootStateChangedSubject) {
|
24 | |
25 |
|
26 |
|
27 | this._destroyed = new rxjs_1.Subject();
|
28 | |
29 |
|
30 |
|
31 | this.actionDispatch = new rxjs_1.Subject();
|
32 | this.state = state;
|
33 | this.stateMutators = stateMutators;
|
34 | this.forwardProjections = forwardProjections;
|
35 | this.backwardProjections = backwardProjections;
|
36 | this._destroyed.subscribe(undefined, undefined, onDestroy);
|
37 | this.destroyed = this._destroyed.asObservable();
|
38 | this.stateChangeNotificationSubject = notifyRootStateChangedSubject;
|
39 | this.stateChangedNotification = this.stateChangeNotificationSubject.asObservable().pipe(operators_1.takeUntil(this.destroyed));
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | Store.create = function (initialState) {
|
45 | if (initialState === undefined)
|
46 | initialState = {};
|
47 | else {
|
48 | if (isObject(initialState) && !Array.isArray(initialState) && !isPlainObject(initialState))
|
49 | throw new Error("initialState must be a plain object, an array, or a primitive type");
|
50 | }
|
51 | var stateMutators = new rxjs_1.Subject();
|
52 | var state = createState(stateMutators, initialState);
|
53 |
|
54 | var stateSubscription = state.subscribe();
|
55 | var onDestroy = function () { stateSubscription.unsubscribe(); };
|
56 | var store = new Store(state, stateMutators, [], [], onDestroy, new rxjs_1.Subject());
|
57 |
|
58 | stateMutators.next(function (s) { return s; });
|
59 | return store;
|
60 | };
|
61 | |
62 |
|
63 |
|
64 |
|
65 | Store.prototype.createSlice = function (key, initialState, cleanupState) {
|
66 | if (isObject(initialState) && !Array.isArray(initialState) && !isPlainObject(initialState))
|
67 | throw new Error("initialState must be a plain object, an array, or a primitive type");
|
68 | if (isObject(cleanupState) && !Array.isArray(cleanupState) && !isPlainObject(cleanupState))
|
69 | throw new Error("cleanupState must be a plain object, an array, or a primitive type");
|
70 | var forward = function (state) { return state[key]; };
|
71 | var backward = function (state, parentState) {
|
72 | parentState[key] = state;
|
73 | return parentState;
|
74 | };
|
75 | var initial = initialState === undefined ? undefined : function () { return initialState; };
|
76 |
|
77 | var cleanup = cleanupState === undefined ? undefined : function (state, parentState) {
|
78 | if (cleanupState === "undefined") {
|
79 | parentState[key] = undefined;
|
80 | }
|
81 | else if (cleanupState === "delete")
|
82 | delete parentState[key];
|
83 | else {
|
84 | parentState[key] = cleanupState;
|
85 | }
|
86 | return parentState;
|
87 | };
|
88 | return this.createProjection(forward, backward, initial, cleanup);
|
89 | };
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | Store.prototype.clone = function () {
|
98 | return this.createProjection(function (s) { return s; }, function (s, p) { return s; });
|
99 | };
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | Store.prototype.createProjection = function (forwardProjection, backwardProjection, initial, cleanup) {
|
110 | var _this = this;
|
111 | var state = this.state.pipe(operators_1.map(function (state) { return forwardProjection(state); }));
|
112 | var forwardProjections = this.forwardProjections.concat([forwardProjection]);
|
113 | var backwardProjections = [backwardProjection].concat(this.backwardProjections);
|
114 | if (initial !== undefined) {
|
115 | this.stateMutators.next(function (s) {
|
116 | var initialReducer = function () { return initial(s); };
|
117 | return mutateRootState(s, forwardProjections, backwardProjections, initialReducer);
|
118 | });
|
119 | }
|
120 | var onDestroy = function () {
|
121 | if (cleanup !== undefined) {
|
122 | _this.stateMutators.next(function (s) {
|
123 | var backward = [cleanup].concat(_this.backwardProjections);
|
124 | return mutateRootState(s, forwardProjections, backward, function (s) { return s; });
|
125 | });
|
126 | }
|
127 | };
|
128 | var sliceStore = new Store(state, this.stateMutators, forwardProjections, backwardProjections, onDestroy, this.stateChangeNotificationSubject);
|
129 |
|
130 | this._destroyed.subscribe(undefined, undefined, function () {
|
131 | sliceStore.destroy();
|
132 | });
|
133 | return sliceStore;
|
134 | };
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | Store.prototype.addReducer = function (action, reducer, actionName) {
|
143 | var _this = this;
|
144 | if (typeof action === "string" && typeof actionName !== "undefined")
|
145 | throw new Error("cannot specify a string-action AND a string alias at the same time");
|
146 | if (!rxjs_1.isObservable(action) && typeof action !== "string")
|
147 | throw new Error("first argument must be an observable or a string");
|
148 | if (typeof reducer !== "function")
|
149 | throw new Error("reducer argument must be a function");
|
150 | if ((typeof actionName === "string" && actionName.length === 0) ||
|
151 | (typeof action === "string" && action.length === 0))
|
152 | throw new Error("action/actionName must have non-zero length");
|
153 | var name = typeof action === "string" ? action : actionName;
|
154 | var actionFromStringBasedDispatch = this.actionDispatch.pipe(operators_1.filter(function (s) { return s.actionName === name; }), operators_1.map(function (s) { return s.actionPayload; }), operators_1.merge(rxjs_1.isObservable(action) ? action : rxjs_1.EMPTY), operators_1.takeUntil(this.destroyed));
|
155 | var rootReducer = function (payload) { return function (rootState) {
|
156 |
|
157 | var sliceReducer = function (slice) { return reducer(slice, payload); };
|
158 | rootState = mutateRootState(rootState, _this.forwardProjections, _this.backwardProjections, sliceReducer);
|
159 |
|
160 | var changeNotification = {
|
161 | actionName: name,
|
162 | actionPayload: payload,
|
163 | newState: rootState
|
164 | };
|
165 | _this.stateChangeNotificationSubject.next(changeNotification);
|
166 | return rootState;
|
167 | }; };
|
168 | return actionFromStringBasedDispatch.pipe(operators_1.map(function (payload) { return rootReducer(payload); })).subscribe(function (rootStateMutation) {
|
169 | _this.stateMutators.next(rootStateMutation);
|
170 | });
|
171 | };
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | Store.prototype.watch = function (selectorFn) {
|
188 | return this.select(selectorFn).pipe(operators_1.distinctUntilChanged(function (a, b) { return shallowEqual_1.shallowEqual(a, b); }));
|
189 | };
|
190 | |
191 |
|
192 |
|
193 |
|
194 | Store.prototype.select = function (selectorFn) {
|
195 | if (!selectorFn)
|
196 | selectorFn = function (state) { return state; };
|
197 | var mapped = this.state.pipe(operators_1.takeUntil(this._destroyed), operators_1.map(selectorFn));
|
198 | return mapped;
|
199 | };
|
200 | |
201 |
|
202 |
|
203 | Store.prototype.destroy = function () {
|
204 | this._destroyed.next();
|
205 | this._destroyed.complete();
|
206 | };
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | Store.prototype.dispatch = function (actionName, actionPayload) {
|
218 | this.actionDispatch.next({ actionName: actionName, actionPayload: actionPayload });
|
219 | };
|
220 | return Store;
|
221 | }());
|
222 | exports.Store = Store;
|
223 | function mutateRootState(rootState, forwardProjections, backwardProjections, sliceReducer) {
|
224 |
|
225 | var forwardState = rootState;
|
226 | var intermediaryState = [rootState];
|
227 | forwardProjections.map(function (fp) {
|
228 | forwardState = fp.call(undefined, forwardState);
|
229 | intermediaryState.push(forwardState);
|
230 | });
|
231 |
|
232 | var reducedState = sliceReducer(forwardState);
|
233 |
|
234 | var backwardState = reducedState;
|
235 | backwardProjections.slice().map(function (bp, index) {
|
236 | var intermediaryIndex = intermediaryState.length - index - 2;
|
237 | backwardState = bp.call(undefined, backwardState, intermediaryState[intermediaryIndex]);
|
238 | });
|
239 | return backwardState;
|
240 | }
|
241 |
|
\ | No newline at end of file |