@microphi/store 1.1.0 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -9
- package/package.json +25 -23
- package/esm2020/lib/effect/effect.mjs +0 -75
- package/esm2020/lib/list/list.mjs +0 -75
- package/esm2020/lib/reduce/reduce.mjs +0 -26
- package/esm2020/lib/store/store.mjs +0 -101
- package/esm2020/lib/utilities/scan-instance.mjs +0 -19
- package/esm2020/microphi-store.mjs +0 -5
- package/esm2020/public-api.mjs +0 -8
- package/fesm2015/microphi-store.mjs +0 -305
- package/fesm2015/microphi-store.mjs.map +0 -1
- package/fesm2020/microphi-store.mjs +0 -303
- package/fesm2020/microphi-store.mjs.map +0 -1
- package/index.d.ts +0 -6
- package/lib/effect/effect.d.ts +0 -62
- package/lib/effect/effect.d.ts.map +0 -1
- package/lib/list/list.d.ts +0 -19
- package/lib/list/list.d.ts.map +0 -1
- package/lib/reduce/reduce.d.ts +0 -12
- package/lib/reduce/reduce.d.ts.map +0 -1
- package/lib/store/store.d.ts +0 -39
- package/lib/store/store.d.ts.map +0 -1
- package/lib/utilities/scan-instance.d.ts +0 -3
- package/lib/utilities/scan-instance.d.ts.map +0 -1
- package/microphi-store.d.ts.map +0 -1
- package/public-api.d.ts +0 -5
- package/public-api.d.ts.map +0 -1
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import { Subject, BehaviorSubject, of, EMPTY } from 'rxjs';
|
|
2
|
-
import 'reflect-metadata';
|
|
3
|
-
import { withLatestFrom, tap, map, catchError, filter, mergeMap, concatMap, switchMap } from 'rxjs/operators';
|
|
4
|
-
|
|
5
|
-
const builtInMethods = Object.keys(Object.getOwnPropertyDescriptors(Object.getPrototypeOf({})));
|
|
6
|
-
function scanInstance(instance, cb) {
|
|
7
|
-
let proto = Object.getPrototypeOf(instance);
|
|
8
|
-
while (proto) {
|
|
9
|
-
const descriptors = Object.getOwnPropertyDescriptors(proto);
|
|
10
|
-
Object.keys(descriptors)
|
|
11
|
-
.filter((k) => {
|
|
12
|
-
return !builtInMethods.includes(k);
|
|
13
|
-
})
|
|
14
|
-
.forEach((key) => cb(proto, key));
|
|
15
|
-
try {
|
|
16
|
-
proto = Object.getPrototypeOf(proto);
|
|
17
|
-
}
|
|
18
|
-
catch (e) {
|
|
19
|
-
proto = null;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const ReduceSymbol = Symbol('@Reduce');
|
|
25
|
-
function Reduce() {
|
|
26
|
-
return (target, propertyKey) => {
|
|
27
|
-
// by convention a reducer have name such as onFindAll
|
|
28
|
-
// where findAll is the associated action
|
|
29
|
-
// with the following we extract the action's name
|
|
30
|
-
const action = propertyKey.slice(2, 3).toLowerCase() + propertyKey.slice(3);
|
|
31
|
-
return Reflect.defineMetadata(ReduceSymbol, action, target, propertyKey);
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function getReduceMetadata(klass, key) {
|
|
35
|
-
return Reflect.getMetadata(ReduceSymbol, klass, key);
|
|
36
|
-
}
|
|
37
|
-
function getReducers(instance) {
|
|
38
|
-
const reducers = [];
|
|
39
|
-
scanInstance(instance, (proto, key) => {
|
|
40
|
-
const metadata = getReduceMetadata(proto, key);
|
|
41
|
-
if (metadata) {
|
|
42
|
-
reducers.push({ action: metadata });
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
return reducers;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const EffectSymbol = Symbol.for('@Effect');
|
|
49
|
-
/**
|
|
50
|
-
* Tags a method as an effect <br>
|
|
51
|
-
*
|
|
52
|
-
* Strategy is one of the following rxjs pipe-able operators:
|
|
53
|
-
* - `mergeMap`
|
|
54
|
-
* - `concatMap`
|
|
55
|
-
* - `switchMap`
|
|
56
|
-
*
|
|
57
|
-
* As an example consider the following:
|
|
58
|
-
* suppose there's an effect that takes 10 ms to return and then a reducer is called.<br>
|
|
59
|
-
* Suppose we dispatch its event, the following diagram demonstrate what happens<br>
|
|
60
|
-
* <code>
|
|
61
|
-
* `D -> E -> (10) -> R`<br>
|
|
62
|
-
* </code>
|
|
63
|
-
* Event is dispatched `D`, event is executed `E`, takes 10ms and finally reducer is called with result from `E`
|
|
64
|
-
*
|
|
65
|
-
* Now let's suppose we dispatch again the same while `E` is still pending (executing) and suppose we call it with a different argument.
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* `D(a) -> E -> D(b)`
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* what happens next depends on the strategy used.<br>
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* If `switchMap` is used (default)
|
|
75
|
-
*
|
|
76
|
-
* `D(a) -> E(a) -> D(b) -> E(b) -> R(b)`
|
|
77
|
-
*
|
|
78
|
-
* the second dispatch cancels the first and so only `R(b)` is executed
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* If `mergeMap` is used
|
|
82
|
-
*
|
|
83
|
-
* `D(a) -> E(a) -> D(b) -> E(b) -> R(a) -> R(b)
|
|
84
|
-
*
|
|
85
|
-
* effects are executed in parallel and the first to come is the first served
|
|
86
|
-
*
|
|
87
|
-
* i.e.: lets say E(a) takes longer to respond then
|
|
88
|
-
*
|
|
89
|
-
* `D(a) -> D(b) -> E(b) -> R(b) -> E(a) -> -> R(a)`
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* If `concatMap` is used then the order of calls is respected.
|
|
93
|
-
*
|
|
94
|
-
* in the case above we'll have whatever the timings are on the effects
|
|
95
|
-
*
|
|
96
|
-
* `D(a) -> D(b) -> E(a) -> R(a) -> E(b) -> -> R(b)`
|
|
97
|
-
*
|
|
98
|
-
* @param strategy
|
|
99
|
-
* @constructor
|
|
100
|
-
*/
|
|
101
|
-
function Effect(strategy = 'switchMap') {
|
|
102
|
-
// return Reflect.metadata(EffectSymbol, strategy);
|
|
103
|
-
return (target, propertyKey) => {
|
|
104
|
-
// return Reflect.metadata(EffectSymbol, strategy)(target, propertyKey);
|
|
105
|
-
return Reflect.defineMetadata(EffectSymbol, strategy, target, propertyKey);
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function getEffectMetadata(klass, key) {
|
|
109
|
-
return Reflect.getMetadata(EffectSymbol, klass, key);
|
|
110
|
-
}
|
|
111
|
-
function getEffects(instance) {
|
|
112
|
-
const effects = [];
|
|
113
|
-
scanInstance(instance, (proto, key) => {
|
|
114
|
-
const strategy = getEffectMetadata(proto, key);
|
|
115
|
-
if (strategy) {
|
|
116
|
-
effects.push({ action: key, strategy, });
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
return effects;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
class Store {
|
|
123
|
-
constructor(initialState) {
|
|
124
|
-
this.initialState = initialState;
|
|
125
|
-
this.actions = new Map();
|
|
126
|
-
this._loading$ = new Subject();
|
|
127
|
-
this._store$ = new BehaviorSubject(
|
|
128
|
-
// we need this in order to decouple initial state between instances
|
|
129
|
-
JSON.parse(JSON.stringify(this.initialState)));
|
|
130
|
-
this.effects = getEffects(this);
|
|
131
|
-
this.reducers = getReducers(this);
|
|
132
|
-
const actions = [].concat(this.effects, this.reducers);
|
|
133
|
-
const actionsSet = new Set();
|
|
134
|
-
actions.forEach(({ action }) => actionsSet.add(action));
|
|
135
|
-
actionsSet.forEach((key) => {
|
|
136
|
-
var _a;
|
|
137
|
-
this.actions.set(key, new Subject());
|
|
138
|
-
// @ts-ignore
|
|
139
|
-
const operator = Store.getOperator((_a = this.effects[key]) === null || _a === void 0 ? void 0 : _a.strategy);
|
|
140
|
-
// @ts-ignore
|
|
141
|
-
this.actions.get(key).pipe(withLatestFrom(this._store$), tap(([{ payload }]) => {
|
|
142
|
-
this._loading$.next({
|
|
143
|
-
payload: payload,
|
|
144
|
-
status: true,
|
|
145
|
-
code: key,
|
|
146
|
-
});
|
|
147
|
-
}), operator(([{ name, payload }]) => {
|
|
148
|
-
// @ts-ignore
|
|
149
|
-
return this[name](...payload).pipe(tap((response) => {
|
|
150
|
-
this._loading$.next({
|
|
151
|
-
payload,
|
|
152
|
-
response,
|
|
153
|
-
status: false,
|
|
154
|
-
// here the action is done. The action is the only async call
|
|
155
|
-
// we can assume the loading is done as the effect is not async
|
|
156
|
-
code: key,
|
|
157
|
-
});
|
|
158
|
-
}),
|
|
159
|
-
// the above is
|
|
160
|
-
map((response) => {
|
|
161
|
-
// by convention reducer name must be
|
|
162
|
-
// on + Action
|
|
163
|
-
const reducerName = `on${(name[0]).toUpperCase()}${name.slice(1)}`;
|
|
164
|
-
// @ts-ignore
|
|
165
|
-
return this[reducerName](this._store$.getValue(), response);
|
|
166
|
-
}),
|
|
167
|
-
// error need to be swallowed here too otherwise the observable chain will be broken
|
|
168
|
-
// meaning that once an action goes into an error it cannot be dispatched again
|
|
169
|
-
catchError((error) => this.swallowError(key, error, payload)));
|
|
170
|
-
})).subscribe(this._store$);
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
get loading$() {
|
|
174
|
-
return this._loading$.asObservable();
|
|
175
|
-
}
|
|
176
|
-
get state$() {
|
|
177
|
-
return this._store$.asObservable();
|
|
178
|
-
}
|
|
179
|
-
select(projection) {
|
|
180
|
-
return this.state$.pipe(map(projection));
|
|
181
|
-
}
|
|
182
|
-
getLoadingFor(action) {
|
|
183
|
-
return this.loading$.pipe(filter(({ code }) => code === action), map(({ status }) => status));
|
|
184
|
-
}
|
|
185
|
-
dispatch(action, ...payload) {
|
|
186
|
-
var _a;
|
|
187
|
-
if (!this.actions.has(action)) {
|
|
188
|
-
throw new Error(`Cannot find action ${String(action)}`);
|
|
189
|
-
}
|
|
190
|
-
const name = (typeof this[action] === 'function' ? action : 'noopEffect');
|
|
191
|
-
(_a = this.actions.get(action)) === null || _a === void 0 ? void 0 : _a.next({ name, payload });
|
|
192
|
-
}
|
|
193
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
194
|
-
static noopEffect(payload) {
|
|
195
|
-
return of(payload);
|
|
196
|
-
}
|
|
197
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
198
|
-
static getOperator(strategyFn) {
|
|
199
|
-
if (strategyFn === 'mergeMap') {
|
|
200
|
-
return mergeMap;
|
|
201
|
-
}
|
|
202
|
-
else if (strategyFn === 'concatMap') {
|
|
203
|
-
return concatMap;
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
return switchMap;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
swallowError(key, error, payload) {
|
|
210
|
-
console.log(`Error handling action "${key}"`, error);
|
|
211
|
-
this._loading$.next({
|
|
212
|
-
payload,
|
|
213
|
-
error: error,
|
|
214
|
-
status: false,
|
|
215
|
-
code: key,
|
|
216
|
-
});
|
|
217
|
-
return EMPTY;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
class List {
|
|
222
|
-
constructor(IDField, entities = []) {
|
|
223
|
-
this.IDField = IDField;
|
|
224
|
-
this.entities = new Map();
|
|
225
|
-
/**
|
|
226
|
-
* maps entities position from the original array to their ids
|
|
227
|
-
*/
|
|
228
|
-
this.ids = [];
|
|
229
|
-
this.ids = entities.map((e) => this.getId(e));
|
|
230
|
-
entities
|
|
231
|
-
.forEach((item, idx) => {
|
|
232
|
-
const id = item[IDField];
|
|
233
|
-
this.entities.set(id, item);
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
get size() {
|
|
237
|
-
if (this.ids.length !== this.entities.size) {
|
|
238
|
-
throw new Error(`Size Mismatch ${this.ids.length} !== ${this.entities.size}`);
|
|
239
|
-
}
|
|
240
|
-
return this.entities.size;
|
|
241
|
-
}
|
|
242
|
-
get(id) {
|
|
243
|
-
return this.entities.get(id);
|
|
244
|
-
}
|
|
245
|
-
getId(entity) {
|
|
246
|
-
return entity[this.IDField];
|
|
247
|
-
}
|
|
248
|
-
*[Symbol.iterator]() {
|
|
249
|
-
// tslint:disable-next-line:forin
|
|
250
|
-
for (const idx in this.ids) {
|
|
251
|
-
const id = this.ids[idx];
|
|
252
|
-
yield this.entities.get(id);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
upsert(...entities) {
|
|
256
|
-
entities.forEach((e) => {
|
|
257
|
-
const id = this.getId(e);
|
|
258
|
-
if (!this.entities.has(id)) {
|
|
259
|
-
this.ids.push(id);
|
|
260
|
-
}
|
|
261
|
-
this.entities.set(id, e);
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
prepend(...entities) {
|
|
265
|
-
entities.reverse().forEach((e) => {
|
|
266
|
-
const id = this.getId(e);
|
|
267
|
-
if (this.entities.has(id)) {
|
|
268
|
-
this.delete(e);
|
|
269
|
-
}
|
|
270
|
-
this.ids.unshift(id);
|
|
271
|
-
this.entities.set(id, e);
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
delete(...entities) {
|
|
275
|
-
entities.forEach((e) => {
|
|
276
|
-
const id = this.getId(e);
|
|
277
|
-
const indexOfId = this.ids.findIndex((_id) => _id === id);
|
|
278
|
-
if (indexOfId >= 0) {
|
|
279
|
-
this.ids.splice(indexOfId, 1);
|
|
280
|
-
this.entities.delete(id);
|
|
281
|
-
}
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
append(...entities) {
|
|
285
|
-
entities.forEach((e) => {
|
|
286
|
-
const id = this.getId(e);
|
|
287
|
-
if (this.entities.has(id)) {
|
|
288
|
-
this.delete(e);
|
|
289
|
-
}
|
|
290
|
-
this.ids.push(id);
|
|
291
|
-
this.entities.set(id, e);
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/*
|
|
297
|
-
* Public API Surface of store
|
|
298
|
-
*/
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Generated bundle index. Do not edit.
|
|
302
|
-
*/
|
|
303
|
-
|
|
304
|
-
export { Effect, EffectSymbol, List, Reduce, ReduceSymbol, Store, getEffectMetadata, getEffects, getReduceMetadata, getReducers };
|
|
305
|
-
//# sourceMappingURL=microphi-store.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"microphi-store.mjs","sources":["../../../projects/store/src/lib/utilities/scan-instance.ts","../../../projects/store/src/lib/reduce/reduce.ts","../../../projects/store/src/lib/effect/effect.ts","../../../projects/store/src/lib/store/store.ts","../../../projects/store/src/lib/list/list.ts","../../../projects/store/src/public-api.ts","../../../projects/store/src/microphi-store.ts"],"sourcesContent":["import { Class } from 'utility-types';\n\nconst builtInMethods = Object.keys(Object.getOwnPropertyDescriptors(Object.getPrototypeOf({})));\n\nexport function scanInstance<T extends object>(instance: T, cb: (proto: Class<T>, key: string) => void) {\n\n let proto = Object.getPrototypeOf(instance);\n\n while (proto) {\n\n const descriptors = Object.getOwnPropertyDescriptors(proto);\n\n Object.keys(descriptors)\n .filter((k) => {\n return !builtInMethods.includes(k);\n })\n .forEach((key) => cb(proto, key));\n\n try {\n proto = Object.getPrototypeOf(proto);\n } catch (e) {\n proto = null;\n }\n }\n\n}\n","import 'reflect-metadata';\nimport { scanInstance } from '../utilities/scan-instance';\nimport { Class } from 'utility-types';\n\nexport const ReduceSymbol = Symbol('@Reduce');\n\nexport interface Reducer {\n action: string;\n}\n\nexport function Reduce(): MethodDecorator {\n return (target, propertyKey) => {\n // by convention a reducer have name such as onFindAll\n // where findAll is the associated action\n // with the following we extract the action's name\n const action = (propertyKey as string).slice(2, 3).toLowerCase() + (propertyKey as string).slice(3);\n\n return Reflect.defineMetadata(ReduceSymbol, action, target, propertyKey);\n };\n}\n\nexport function getReduceMetadata(klass: Class<any>, key: string) {\n return Reflect.getMetadata(ReduceSymbol, klass, key);\n}\n\nexport function getReducers(instance: object): {action: string;}[] {\n\n const reducers: {action: string;}[] = [];\n\n scanInstance(instance, (proto, key) => {\n\n const metadata = getReduceMetadata(proto, key);\n if (metadata) {\n reducers.push({action: metadata});\n }\n\n });\n\n return reducers;\n}\n","import { Store } from '../store/store';\nimport { $Keys } from 'utility-types';\nimport { scanInstance } from '../utilities/scan-instance';\n\nexport type EffectStrategy = 'switchMap' | 'mergeMap' | 'concatMap';\n\nexport interface Effect {\n action: string;\n strategy: EffectStrategy;\n}\n\nexport const EffectSymbol = Symbol.for('@Effect');\n\n/**\n * Tags a method as an effect <br>\n *\n * Strategy is one of the following rxjs pipe-able operators:\n * - `mergeMap`\n * - `concatMap`\n * - `switchMap`\n *\n * As an example consider the following:\n * suppose there's an effect that takes 10 ms to return and then a reducer is called.<br>\n * Suppose we dispatch its event, the following diagram demonstrate what happens<br>\n * <code>\n * `D -> E -> (10) -> R`<br>\n * </code>\n * Event is dispatched `D`, event is executed `E`, takes 10ms and finally reducer is called with result from `E`\n *\n * Now let's suppose we dispatch again the same while `E` is still pending (executing) and suppose we call it with a different argument.\n *\n *\n * `D(a) -> E -> D(b)`\n *\n *\n * what happens next depends on the strategy used.<br>\n *\n *\n * If `switchMap` is used (default)\n *\n * `D(a) -> E(a) -> D(b) -> E(b) -> R(b)`\n *\n * the second dispatch cancels the first and so only `R(b)` is executed\n *\n *\n * If `mergeMap` is used\n *\n * `D(a) -> E(a) -> D(b) -> E(b) -> R(a) -> R(b)\n *\n * effects are executed in parallel and the first to come is the first served\n *\n * i.e.: lets say E(a) takes longer to respond then\n *\n * `D(a) -> D(b) -> E(b) -> R(b) -> E(a) -> -> R(a)`\n *\n *\n * If `concatMap` is used then the order of calls is respected.\n *\n * in the case above we'll have whatever the timings are on the effects\n *\n * `D(a) -> D(b) -> E(a) -> R(a) -> E(b) -> -> R(b)`\n *\n * @param strategy\n * @constructor\n */\nexport function Effect(strategy: EffectStrategy = 'switchMap'): MethodDecorator {\n // return Reflect.metadata(EffectSymbol, strategy);\n return (target, propertyKey) => {\n // return Reflect.metadata(EffectSymbol, strategy)(target, propertyKey);\n return Reflect.defineMetadata(EffectSymbol, strategy, target, propertyKey);\n };\n}\n\nexport function getEffectMetadata(klass: object, key: string) {\n return Reflect.getMetadata(EffectSymbol, klass, key);\n}\n\nexport function getEffects<T extends object>(instance: T): Effect[] {\n\n const effects: Effect[] = [];\n\n scanInstance(instance, (proto, key) => {\n\n const strategy = getEffectMetadata(proto, key);\n if (strategy) {\n effects.push({action: key, strategy,});\n }\n\n });\n\n return effects;\n}\n","import { BehaviorSubject, EMPTY, Observable, of, Subject } from 'rxjs';\nimport { getReducers, Reducer } from '../reduce/reduce';\nimport { catchError, concatMap, filter, map, mergeMap, switchMap, tap, withLatestFrom } from 'rxjs/operators';\nimport { Effect, EffectStrategy, getEffects } from '../effect/effect';\nimport { Brand, Primitive } from 'utility-types';\n\nexport type getPayloadFromActionType<A, C extends keyof A> =\n A[C] extends Function ? A[C] extends () => any\n ? never[] : A[C] extends (...args: infer T) => any\n ? T : never[] : A[C][];\n\nexport type PureReducer<T extends Primitive> = Brand<T, 'reducer'>;\n\nexport type makeEffects<Actions> = {\n // Effects\n [k in keyof Actions]: Actions[k] extends PureReducer<Primitive>\n ? never\n : Actions[k]\n}\n\nexport type makeStore<State, Actions> =\n {\n [k in keyof makeEffects<Actions>]: makeEffects<Actions>[k]\n }\n // &\n // Actions\n &\n {\n // Reducers\n [k in keyof Actions as `on${Capitalize<string & k>}`]: Actions[k] extends (...args: any) => Observable<infer O>\n ? (state: State, payload: O) => State\n : Actions[k] extends PureReducer<infer O>\n ? (state?: State, payload?: O) => State\n : (state?: State) => State\n\n };\n\nexport interface LoadingState<A> {\n code: keyof A;\n payload?: any;\n response?: any;\n error?: any;\n status: boolean;\n}\n\nexport abstract class Store<State, A> {\n\n private readonly actions: Map<string, Subject<{ name: string, payload?: any }>> = new Map();\n\n readonly effects: Effect[];\n\n readonly reducers: Reducer[];\n\n\n private _loading$ = new Subject<LoadingState<A>>();\n\n public get loading$(): Observable<LoadingState<A>> {\n return this._loading$.asObservable();\n }\n\n protected readonly _store$: BehaviorSubject<State> = new BehaviorSubject<State>(\n // we need this in order to decouple initial state between instances\n JSON.parse(JSON.stringify(this.initialState))\n );\n\n public get state$(): Observable<State> {\n return this._store$.asObservable();\n }\n\n constructor(private initialState: State) {\n\n this.effects = getEffects(this);\n this.reducers = getReducers(this);\n\n const actions = [].concat(this.effects, this.reducers);\n\n const actionsSet = new Set<string>();\n\n actions.forEach(({action}) => actionsSet.add(action));\n\n actionsSet.forEach((key) => {\n\n this.actions.set(key, new Subject());\n\n // @ts-ignore\n const operator = Store.getOperator(this.effects[key]?.strategy);\n\n // @ts-ignore\n this.actions.get(key).pipe(\n withLatestFrom(this._store$),\n tap(([{payload}]) => {\n this._loading$.next({\n payload: payload,\n status: true,\n code: key as keyof A,\n });\n }),\n operator(([{name, payload}]) => {\n // @ts-ignore\n return this[name](...payload).pipe(\n tap((response) => {\n this._loading$.next({\n payload,\n response,\n status: false,\n // here the action is done. The action is the only async call\n // we can assume the loading is done as the effect is not async\n code: key as keyof A,\n });\n }),\n // the above is\n map((response) => {\n // by convention reducer name must be\n // on + Action\n const reducerName = `on${(name[0]).toUpperCase()}${(name as string).slice(1)}`;\n\n // @ts-ignore\n return this[reducerName](this._store$.getValue(), response);\n }),\n // error need to be swallowed here too otherwise the observable chain will be broken\n // meaning that once an action goes into an error it cannot be dispatched again\n catchError((error) => this.swallowError(key, error, payload)),\n );\n }),\n // @ts-ignore\n ).subscribe(this._store$);\n });\n }\n\n select<R>(projection: (s: State) => R) {\n return this.state$.pipe(\n map(projection)\n );\n }\n\n getLoadingFor<C extends keyof A>(action: C): Observable<boolean> {\n return this.loading$.pipe(\n filter(({code}) => code === action),\n map(({status}) => status),\n );\n }\n\n dispatch<C extends keyof A>(\n action: C,\n ...payload: getPayloadFromActionType<A, C>\n ) {\n\n if (!this.actions.has(action as string)) {\n throw new Error(`Cannot find action ${String(action)}`);\n }\n\n const name: string = (typeof (this as unknown as A)[action] === 'function' ? action : 'noopEffect') as string;\n\n\n this.actions.get(action as string)?.next({name, payload});\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n private static noopEffect(payload: any) {\n return of(payload);\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n private static getOperator(strategyFn: EffectStrategy) {\n if (strategyFn === 'mergeMap') {\n return mergeMap;\n } else if (strategyFn === 'concatMap') {\n return concatMap;\n } else {\n return switchMap;\n }\n }\n\n private swallowError(key: string, error: Error, payload: any) {\n console.log(`Error handling action \"${key}\"`, error);\n\n this._loading$.next({\n payload,\n error: error,\n status: false,\n code: key as keyof A,\n });\n\n return EMPTY;\n }\n\n}\n","import { $Keys } from 'utility-types';\n\nexport class List<EntityType extends {}> implements Iterable<EntityType> {\n\n public readonly entities = new Map<any, EntityType>();\n\n /**\n * maps entities position from the original array to their ids\n */\n private readonly ids: any[] = [];\n\n public get size() {\n if (this.ids.length !== this.entities.size) {\n\n throw new Error(`Size Mismatch ${this.ids.length} !== ${this.entities.size}`);\n }\n\n return this.entities.size;\n }\n\n public get(id) {\n return this.entities.get(id);\n }\n private getId(entity: EntityType) {\n return entity[this.IDField];\n }\n\n constructor(private IDField: $Keys<EntityType>, entities: EntityType[] = []) {\n\n this.ids = entities.map((e) => this.getId(e));\n\n entities\n .forEach((item, idx) => {\n const id = item[IDField];\n this.entities.set(id, item);\n });\n }\n\n *[Symbol.iterator](): IterableIterator<EntityType> {\n // tslint:disable-next-line:forin\n for (const idx in this.ids) {\n const id = this.ids[idx];\n yield this.entities.get(id);\n }\n }\n\n public upsert(...entities: EntityType[]) {\n entities.forEach((e) => {\n const id = this.getId(e);\n\n if (!this.entities.has(id)) {\n this.ids.push(id);\n }\n\n this.entities.set(id, e);\n });\n }\n\n public prepend(...entities: EntityType[]) {\n entities.reverse().forEach((e) => {\n const id = this.getId(e);\n\n if (this.entities.has(id)) {\n this.delete(e);\n }\n\n this.ids.unshift(id);\n\n this.entities.set(id, e);\n\n });\n }\n\n public delete(...entities: EntityType[]) {\n entities.forEach((e) => {\n const id = this.getId(e);\n const indexOfId = this.ids.findIndex((_id) => _id === id);\n\n if (indexOfId >= 0) {\n this.ids.splice(indexOfId, 1);\n this.entities.delete(id);\n }\n });\n }\n\n public append(...entities: EntityType[]) {\n entities.forEach((e) => {\n const id = this.getId(e);\n\n if (this.entities.has(id)) {\n this.delete(e);\n }\n\n this.ids.push(id);\n this.entities.set(id, e);\n });\n }\n}\n","/*\n * Public API Surface of store\n */\n\nexport * from './lib/store/store';\nexport * from './lib/effect/effect';\nexport * from './lib/reduce/reduce';\nexport * from './lib/list/list';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAEA,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,yBAAyB,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAEhF,SAAA,YAAY,CAAmB,QAAW,EAAE,EAA0C,EAAA;IAEpG,IAAI,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;AAE5C,IAAA,OAAO,KAAK,EAAE;QAEZ,MAAM,WAAW,GAAG,MAAM,CAAC,yBAAyB,CAAC,KAAK,CAAC,CAAC;AAE5D,QAAA,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;AACrB,aAAA,MAAM,CAAC,CAAC,CAAC,KAAI;AACZ,YAAA,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AACrC,SAAC,CAAC;AACD,aAAA,OAAO,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;QAEpC,IAAI;AACF,YAAA,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;AACtC,SAAA;AAAC,QAAA,OAAO,CAAC,EAAE;YACV,KAAK,GAAG,IAAI,CAAC;AACd,SAAA;AACF,KAAA;AAEH;;MCrBa,YAAY,GAAG,MAAM,CAAC,SAAS,EAAE;SAM9B,MAAM,GAAA;AACpB,IAAA,OAAO,CAAC,MAAM,EAAE,WAAW,KAAI;;;;QAI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1D,IAAI,SAAS,IAAI,CAAC,EAAE;gBAClB,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;AAC9B,gBAAA,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAC1B,aAAA;AACH,SAAC,CAAC,CAAC;KACJ;IAEM,MAAM,CAAC,GAAG,QAAsB,EAAA;AACrC,QAAA,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,KAAI;YACrB,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAEzB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;AACzB,gBAAA,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAChB,aAAA;AAED,YAAA,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAClB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3B,SAAC,CAAC,CAAC;KACJ;AACF;;ACjGD;;AAEG;;ACFH;;AAEG;;;;"}
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import { Subject, BehaviorSubject, of, EMPTY } from 'rxjs';
|
|
2
|
-
import 'reflect-metadata';
|
|
3
|
-
import { withLatestFrom, tap, map, catchError, filter, mergeMap, concatMap, switchMap } from 'rxjs/operators';
|
|
4
|
-
|
|
5
|
-
const builtInMethods = Object.keys(Object.getOwnPropertyDescriptors(Object.getPrototypeOf({})));
|
|
6
|
-
function scanInstance(instance, cb) {
|
|
7
|
-
let proto = Object.getPrototypeOf(instance);
|
|
8
|
-
while (proto) {
|
|
9
|
-
const descriptors = Object.getOwnPropertyDescriptors(proto);
|
|
10
|
-
Object.keys(descriptors)
|
|
11
|
-
.filter((k) => {
|
|
12
|
-
return !builtInMethods.includes(k);
|
|
13
|
-
})
|
|
14
|
-
.forEach((key) => cb(proto, key));
|
|
15
|
-
try {
|
|
16
|
-
proto = Object.getPrototypeOf(proto);
|
|
17
|
-
}
|
|
18
|
-
catch (e) {
|
|
19
|
-
proto = null;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const ReduceSymbol = Symbol('@Reduce');
|
|
25
|
-
function Reduce() {
|
|
26
|
-
return (target, propertyKey) => {
|
|
27
|
-
// by convention a reducer have name such as onFindAll
|
|
28
|
-
// where findAll is the associated action
|
|
29
|
-
// with the following we extract the action's name
|
|
30
|
-
const action = propertyKey.slice(2, 3).toLowerCase() + propertyKey.slice(3);
|
|
31
|
-
return Reflect.defineMetadata(ReduceSymbol, action, target, propertyKey);
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function getReduceMetadata(klass, key) {
|
|
35
|
-
return Reflect.getMetadata(ReduceSymbol, klass, key);
|
|
36
|
-
}
|
|
37
|
-
function getReducers(instance) {
|
|
38
|
-
const reducers = [];
|
|
39
|
-
scanInstance(instance, (proto, key) => {
|
|
40
|
-
const metadata = getReduceMetadata(proto, key);
|
|
41
|
-
if (metadata) {
|
|
42
|
-
reducers.push({ action: metadata });
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
return reducers;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const EffectSymbol = Symbol.for('@Effect');
|
|
49
|
-
/**
|
|
50
|
-
* Tags a method as an effect <br>
|
|
51
|
-
*
|
|
52
|
-
* Strategy is one of the following rxjs pipe-able operators:
|
|
53
|
-
* - `mergeMap`
|
|
54
|
-
* - `concatMap`
|
|
55
|
-
* - `switchMap`
|
|
56
|
-
*
|
|
57
|
-
* As an example consider the following:
|
|
58
|
-
* suppose there's an effect that takes 10 ms to return and then a reducer is called.<br>
|
|
59
|
-
* Suppose we dispatch its event, the following diagram demonstrate what happens<br>
|
|
60
|
-
* <code>
|
|
61
|
-
* `D -> E -> (10) -> R`<br>
|
|
62
|
-
* </code>
|
|
63
|
-
* Event is dispatched `D`, event is executed `E`, takes 10ms and finally reducer is called with result from `E`
|
|
64
|
-
*
|
|
65
|
-
* Now let's suppose we dispatch again the same while `E` is still pending (executing) and suppose we call it with a different argument.
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* `D(a) -> E -> D(b)`
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* what happens next depends on the strategy used.<br>
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* If `switchMap` is used (default)
|
|
75
|
-
*
|
|
76
|
-
* `D(a) -> E(a) -> D(b) -> E(b) -> R(b)`
|
|
77
|
-
*
|
|
78
|
-
* the second dispatch cancels the first and so only `R(b)` is executed
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* If `mergeMap` is used
|
|
82
|
-
*
|
|
83
|
-
* `D(a) -> E(a) -> D(b) -> E(b) -> R(a) -> R(b)
|
|
84
|
-
*
|
|
85
|
-
* effects are executed in parallel and the first to come is the first served
|
|
86
|
-
*
|
|
87
|
-
* i.e.: lets say E(a) takes longer to respond then
|
|
88
|
-
*
|
|
89
|
-
* `D(a) -> D(b) -> E(b) -> R(b) -> E(a) -> -> R(a)`
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* If `concatMap` is used then the order of calls is respected.
|
|
93
|
-
*
|
|
94
|
-
* in the case above we'll have whatever the timings are on the effects
|
|
95
|
-
*
|
|
96
|
-
* `D(a) -> D(b) -> E(a) -> R(a) -> E(b) -> -> R(b)`
|
|
97
|
-
*
|
|
98
|
-
* @param strategy
|
|
99
|
-
* @constructor
|
|
100
|
-
*/
|
|
101
|
-
function Effect(strategy = 'switchMap') {
|
|
102
|
-
// return Reflect.metadata(EffectSymbol, strategy);
|
|
103
|
-
return (target, propertyKey) => {
|
|
104
|
-
// return Reflect.metadata(EffectSymbol, strategy)(target, propertyKey);
|
|
105
|
-
return Reflect.defineMetadata(EffectSymbol, strategy, target, propertyKey);
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function getEffectMetadata(klass, key) {
|
|
109
|
-
return Reflect.getMetadata(EffectSymbol, klass, key);
|
|
110
|
-
}
|
|
111
|
-
function getEffects(instance) {
|
|
112
|
-
const effects = [];
|
|
113
|
-
scanInstance(instance, (proto, key) => {
|
|
114
|
-
const strategy = getEffectMetadata(proto, key);
|
|
115
|
-
if (strategy) {
|
|
116
|
-
effects.push({ action: key, strategy, });
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
return effects;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
class Store {
|
|
123
|
-
constructor(initialState) {
|
|
124
|
-
this.initialState = initialState;
|
|
125
|
-
this.actions = new Map();
|
|
126
|
-
this._loading$ = new Subject();
|
|
127
|
-
this._store$ = new BehaviorSubject(
|
|
128
|
-
// we need this in order to decouple initial state between instances
|
|
129
|
-
JSON.parse(JSON.stringify(this.initialState)));
|
|
130
|
-
this.effects = getEffects(this);
|
|
131
|
-
this.reducers = getReducers(this);
|
|
132
|
-
const actions = [].concat(this.effects, this.reducers);
|
|
133
|
-
const actionsSet = new Set();
|
|
134
|
-
actions.forEach(({ action }) => actionsSet.add(action));
|
|
135
|
-
actionsSet.forEach((key) => {
|
|
136
|
-
this.actions.set(key, new Subject());
|
|
137
|
-
// @ts-ignore
|
|
138
|
-
const operator = Store.getOperator(this.effects[key]?.strategy);
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
this.actions.get(key).pipe(withLatestFrom(this._store$), tap(([{ payload }]) => {
|
|
141
|
-
this._loading$.next({
|
|
142
|
-
payload: payload,
|
|
143
|
-
status: true,
|
|
144
|
-
code: key,
|
|
145
|
-
});
|
|
146
|
-
}), operator(([{ name, payload }]) => {
|
|
147
|
-
// @ts-ignore
|
|
148
|
-
return this[name](...payload).pipe(tap((response) => {
|
|
149
|
-
this._loading$.next({
|
|
150
|
-
payload,
|
|
151
|
-
response,
|
|
152
|
-
status: false,
|
|
153
|
-
// here the action is done. The action is the only async call
|
|
154
|
-
// we can assume the loading is done as the effect is not async
|
|
155
|
-
code: key,
|
|
156
|
-
});
|
|
157
|
-
}),
|
|
158
|
-
// the above is
|
|
159
|
-
map((response) => {
|
|
160
|
-
// by convention reducer name must be
|
|
161
|
-
// on + Action
|
|
162
|
-
const reducerName = `on${(name[0]).toUpperCase()}${name.slice(1)}`;
|
|
163
|
-
// @ts-ignore
|
|
164
|
-
return this[reducerName](this._store$.getValue(), response);
|
|
165
|
-
}),
|
|
166
|
-
// error need to be swallowed here too otherwise the observable chain will be broken
|
|
167
|
-
// meaning that once an action goes into an error it cannot be dispatched again
|
|
168
|
-
catchError((error) => this.swallowError(key, error, payload)));
|
|
169
|
-
})).subscribe(this._store$);
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
get loading$() {
|
|
173
|
-
return this._loading$.asObservable();
|
|
174
|
-
}
|
|
175
|
-
get state$() {
|
|
176
|
-
return this._store$.asObservable();
|
|
177
|
-
}
|
|
178
|
-
select(projection) {
|
|
179
|
-
return this.state$.pipe(map(projection));
|
|
180
|
-
}
|
|
181
|
-
getLoadingFor(action) {
|
|
182
|
-
return this.loading$.pipe(filter(({ code }) => code === action), map(({ status }) => status));
|
|
183
|
-
}
|
|
184
|
-
dispatch(action, ...payload) {
|
|
185
|
-
if (!this.actions.has(action)) {
|
|
186
|
-
throw new Error(`Cannot find action ${String(action)}`);
|
|
187
|
-
}
|
|
188
|
-
const name = (typeof this[action] === 'function' ? action : 'noopEffect');
|
|
189
|
-
this.actions.get(action)?.next({ name, payload });
|
|
190
|
-
}
|
|
191
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
192
|
-
static noopEffect(payload) {
|
|
193
|
-
return of(payload);
|
|
194
|
-
}
|
|
195
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
196
|
-
static getOperator(strategyFn) {
|
|
197
|
-
if (strategyFn === 'mergeMap') {
|
|
198
|
-
return mergeMap;
|
|
199
|
-
}
|
|
200
|
-
else if (strategyFn === 'concatMap') {
|
|
201
|
-
return concatMap;
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
return switchMap;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
swallowError(key, error, payload) {
|
|
208
|
-
console.log(`Error handling action "${key}"`, error);
|
|
209
|
-
this._loading$.next({
|
|
210
|
-
payload,
|
|
211
|
-
error: error,
|
|
212
|
-
status: false,
|
|
213
|
-
code: key,
|
|
214
|
-
});
|
|
215
|
-
return EMPTY;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
class List {
|
|
220
|
-
constructor(IDField, entities = []) {
|
|
221
|
-
this.IDField = IDField;
|
|
222
|
-
this.entities = new Map();
|
|
223
|
-
/**
|
|
224
|
-
* maps entities position from the original array to their ids
|
|
225
|
-
*/
|
|
226
|
-
this.ids = [];
|
|
227
|
-
this.ids = entities.map((e) => this.getId(e));
|
|
228
|
-
entities
|
|
229
|
-
.forEach((item, idx) => {
|
|
230
|
-
const id = item[IDField];
|
|
231
|
-
this.entities.set(id, item);
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
get size() {
|
|
235
|
-
if (this.ids.length !== this.entities.size) {
|
|
236
|
-
throw new Error(`Size Mismatch ${this.ids.length} !== ${this.entities.size}`);
|
|
237
|
-
}
|
|
238
|
-
return this.entities.size;
|
|
239
|
-
}
|
|
240
|
-
get(id) {
|
|
241
|
-
return this.entities.get(id);
|
|
242
|
-
}
|
|
243
|
-
getId(entity) {
|
|
244
|
-
return entity[this.IDField];
|
|
245
|
-
}
|
|
246
|
-
*[Symbol.iterator]() {
|
|
247
|
-
// tslint:disable-next-line:forin
|
|
248
|
-
for (const idx in this.ids) {
|
|
249
|
-
const id = this.ids[idx];
|
|
250
|
-
yield this.entities.get(id);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
upsert(...entities) {
|
|
254
|
-
entities.forEach((e) => {
|
|
255
|
-
const id = this.getId(e);
|
|
256
|
-
if (!this.entities.has(id)) {
|
|
257
|
-
this.ids.push(id);
|
|
258
|
-
}
|
|
259
|
-
this.entities.set(id, e);
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
prepend(...entities) {
|
|
263
|
-
entities.reverse().forEach((e) => {
|
|
264
|
-
const id = this.getId(e);
|
|
265
|
-
if (this.entities.has(id)) {
|
|
266
|
-
this.delete(e);
|
|
267
|
-
}
|
|
268
|
-
this.ids.unshift(id);
|
|
269
|
-
this.entities.set(id, e);
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
delete(...entities) {
|
|
273
|
-
entities.forEach((e) => {
|
|
274
|
-
const id = this.getId(e);
|
|
275
|
-
const indexOfId = this.ids.findIndex((_id) => _id === id);
|
|
276
|
-
if (indexOfId >= 0) {
|
|
277
|
-
this.ids.splice(indexOfId, 1);
|
|
278
|
-
this.entities.delete(id);
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
append(...entities) {
|
|
283
|
-
entities.forEach((e) => {
|
|
284
|
-
const id = this.getId(e);
|
|
285
|
-
if (this.entities.has(id)) {
|
|
286
|
-
this.delete(e);
|
|
287
|
-
}
|
|
288
|
-
this.ids.push(id);
|
|
289
|
-
this.entities.set(id, e);
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/*
|
|
295
|
-
* Public API Surface of store
|
|
296
|
-
*/
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Generated bundle index. Do not edit.
|
|
300
|
-
*/
|
|
301
|
-
|
|
302
|
-
export { Effect, EffectSymbol, List, Reduce, ReduceSymbol, Store, getEffectMetadata, getEffects, getReduceMetadata, getReducers };
|
|
303
|
-
//# sourceMappingURL=microphi-store.mjs.map
|