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