@microphi/store 0.2.10 → 1.1.3

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.
Files changed (63) hide show
  1. package/lib/index.d.ts +4 -0
  2. package/lib/index.js +8 -0
  3. package/lib/index.js.map +1 -0
  4. package/lib/lib/effect/effect.d.ts +61 -0
  5. package/lib/lib/effect/effect.js +75 -0
  6. package/lib/lib/effect/effect.js.map +1 -0
  7. package/lib/{list → lib/list}/list.d.ts +1 -1
  8. package/lib/lib/list/list.js +75 -0
  9. package/lib/lib/list/list.js.map +1 -0
  10. package/lib/lib/reduce/reduce.d.ts +11 -0
  11. package/lib/lib/reduce/reduce.js +26 -0
  12. package/lib/lib/reduce/reduce.js.map +1 -0
  13. package/lib/lib/store/store.d.ts +38 -0
  14. package/lib/lib/store/store.js +99 -0
  15. package/lib/lib/store/store.js.map +1 -0
  16. package/lib/lib/utilities/capitalise.d.ts +1 -0
  17. package/lib/lib/utilities/capitalise.js +7 -0
  18. package/lib/lib/utilities/capitalise.js.map +1 -0
  19. package/lib/lib/utilities/scan-instance.d.ts +2 -0
  20. package/lib/lib/utilities/scan-instance.js +19 -0
  21. package/lib/lib/utilities/scan-instance.js.map +1 -0
  22. package/package.json +18 -14
  23. package/bundles/microphi-store.umd.js +0 -996
  24. package/bundles/microphi-store.umd.js.map +0 -1
  25. package/bundles/microphi-store.umd.min.js +0 -16
  26. package/bundles/microphi-store.umd.min.js.map +0 -1
  27. package/esm2015/lib/actions.js +0 -105
  28. package/esm2015/lib/base-store.js +0 -156
  29. package/esm2015/lib/effect.js +0 -11
  30. package/esm2015/lib/http-status/http-status.interceptor.js +0 -31
  31. package/esm2015/lib/http-status/http-status.store.js +0 -63
  32. package/esm2015/lib/list/list.js +0 -80
  33. package/esm2015/lib/observable-list/observable-list.js +0 -119
  34. package/esm2015/lib/reduce.js +0 -15
  35. package/esm2015/lib/store.js +0 -13
  36. package/esm2015/microphi-store.js +0 -5
  37. package/esm2015/public-api.js +0 -13
  38. package/esm5/lib/actions.js +0 -110
  39. package/esm5/lib/base-store.js +0 -177
  40. package/esm5/lib/effect.js +0 -11
  41. package/esm5/lib/http-status/http-status.interceptor.js +0 -33
  42. package/esm5/lib/http-status/http-status.store.js +0 -66
  43. package/esm5/lib/list/list.js +0 -126
  44. package/esm5/lib/observable-list/observable-list.js +0 -250
  45. package/esm5/lib/reduce.js +0 -15
  46. package/esm5/lib/store.js +0 -13
  47. package/esm5/microphi-store.js +0 -5
  48. package/esm5/public-api.js +0 -13
  49. package/fesm2015/microphi-store.js +0 -586
  50. package/fesm2015/microphi-store.js.map +0 -1
  51. package/fesm5/microphi-store.js +0 -788
  52. package/fesm5/microphi-store.js.map +0 -1
  53. package/lib/actions.d.ts +0 -77
  54. package/lib/base-store.d.ts +0 -31
  55. package/lib/effect.d.ts +0 -2
  56. package/lib/http-status/http-status.interceptor.d.ts +0 -7
  57. package/lib/http-status/http-status.store.d.ts +0 -17
  58. package/lib/observable-list/observable-list.d.ts +0 -29
  59. package/lib/reduce.d.ts +0 -7
  60. package/lib/store.d.ts +0 -9
  61. package/microphi-store.d.ts +0 -4
  62. package/microphi-store.metadata.json +0 -1
  63. package/public-api.d.ts +0 -9
@@ -1,788 +0,0 @@
1
- import { getDebugger } from '@microgamma/loggator';
2
- import { Subject, BehaviorSubject, Observable, throwError } from 'rxjs';
3
- import { tap, takeUntil, catchError, map, delay } from 'rxjs/operators';
4
- import { __values, __generator, __extends, __decorate } from 'tslib';
5
- import { ɵɵdefineInjectable, Injectable } from '@angular/core';
6
-
7
- var REQUEST_SUFFIX = '_REQUEST';
8
- var RESPONSE_SUFFIX = '_RESPONSE';
9
- var ERROR_SUFFIX = '_ERROR';
10
- /**
11
- * Action are defined with an enum. Such as:
12
- *
13
- * emun {
14
- * GET_ALL,
15
- * GET_ONE,
16
- * DELETE,
17
- * }
18
- *
19
- * For each action defined a REQUEST, a RESPONSE and an ERROR are associated to it.
20
- *
21
- *
22
- */
23
- var Actions = /** @class */ (function () {
24
- function Actions(actions) {
25
- var _this = this;
26
- /**
27
- * Example:
28
- * {
29
- * 0: { request: 'ACTION_NAME_REQUEST', response: 'ACTION_NAME_RESPONSE', error: 'ACTION_NAME_ERROR' }
30
- * [....]
31
- * }
32
- */
33
- this.actions = {};
34
- /**
35
- * Example:
36
- * {
37
- * ACTION_NAME: 0
38
- * [....]
39
- * }
40
- *
41
- */
42
- this.names = {};
43
- /**
44
- * Example:
45
- * {
46
- * 0: 'ACTION_NAME',
47
- * [....]
48
- *
49
- * }
50
- */
51
- this.codes = {};
52
- /**
53
- * Example:
54
- * {
55
- * ACTION_NAME': { request: 'ACTION_NAME_REQUEST', response: 'ACTION_NAME_RESPONSE', error: 'ACTION_NAME_ERROR' }
56
- * [....]
57
- * }
58
- */
59
- this.restActionsByName = {};
60
- Object.keys(actions).forEach(function (key) {
61
- if (+key >= 0) {
62
- _this.codes[key] = actions[key];
63
- _this.actions[key] = Actions.createAction(actions[key]);
64
- }
65
- else {
66
- _this.names[key] = actions[key];
67
- _this.restActionsByName[key] = Actions.createAction(key);
68
- }
69
- });
70
- }
71
- Actions.createAction = function (key) {
72
- return {
73
- request: "" + key + REQUEST_SUFFIX,
74
- response: "" + key + RESPONSE_SUFFIX,
75
- error: "" + key + ERROR_SUFFIX,
76
- };
77
- };
78
- Actions.prototype.getByName = function (actionName) {
79
- return this.names[actionName];
80
- };
81
- Actions.prototype.getByCode = function (code) {
82
- return this.codes[code];
83
- };
84
- Actions.prototype.getActionsByCode = function (key) {
85
- return this.actions[key];
86
- };
87
- Actions.prototype.getActionsByName = function (actionName) {
88
- var code = this.getByName(actionName);
89
- return this.getActionsByCode(code);
90
- };
91
- Actions.prototype.getActionNameFromChild = function (childAction) {
92
- var _this = this;
93
- var actionName;
94
- Object.keys(this.restActionsByName).forEach(function (restAction) {
95
- var restActions = _this.restActionsByName[restAction];
96
- if (restActions.response === childAction || restActions.request === childAction || restActions.error === childAction) {
97
- actionName = restAction;
98
- }
99
- });
100
- return actionName;
101
- };
102
- Actions.prototype.getActionCodeFromChild = function (childAction) {
103
- var _this = this;
104
- var actionCode;
105
- Object.keys(this.actions).forEach(function (restAction) {
106
- var restActions = _this.actions[restAction];
107
- if (restActions.response === childAction || restActions.request === childAction || restActions.error === childAction) {
108
- actionCode = restAction;
109
- }
110
- });
111
- return Number(actionCode);
112
- };
113
- return Actions;
114
- }());
115
-
116
- var StoreMetadata = '@Store';
117
- function Store(options) {
118
- var d = getDebugger("microphi:@Store:" + options.name);
119
- return function (target) {
120
- d('running store decorator');
121
- Reflect.defineMetadata(StoreMetadata, options, target);
122
- };
123
- }
124
- function getStoreMetadata(instance) {
125
- return Reflect.getMetadata(StoreMetadata, instance.constructor);
126
- }
127
-
128
- var ReduceMetadata = '@Reduce';
129
- function Reduce(onAction) {
130
- return function (target, key) {
131
- var d = getDebugger("microphi:@Reduce:" + target.constructor.name);
132
- var reducer = Reflect.getMetadata(ReduceMetadata, target) || {};
133
- reducer[onAction] = key;
134
- d('stored reducers', reducer);
135
- return Reflect.defineMetadata(ReduceMetadata, reducer, target);
136
- };
137
- }
138
- function getReduceMetadata(instance) {
139
- return Reflect.getMetadata(ReduceMetadata, instance) || {};
140
- }
141
-
142
- var BaseStore = /** @class */ (function () {
143
- function BaseStore() {
144
- var _this = this;
145
- this.logger = getDebugger("microphi:BaseStore:" + this.constructor.name);
146
- this.actions$ = new Subject();
147
- this._loading$ = new Subject();
148
- this._error$ = new Subject();
149
- this.destroy$ = new Subject();
150
- this.storeMetadata = getStoreMetadata(this);
151
- this.logger('@Store', this.storeMetadata);
152
- this._store$ = new BehaviorSubject(this.storeMetadata.initialState);
153
- // dereference initial state so that is does not leak between instances
154
- this._state = Object.assign({}, this.storeMetadata.initialState);
155
- this.logger('InitialState', this.state);
156
- this.actionsMetadata = new Actions(this.storeMetadata.actions);
157
- var reducerMetadata = getReduceMetadata(this);
158
- this.logger('Reducers', reducerMetadata);
159
- var remappedReducers = this.parseReducers(reducerMetadata);
160
- this.logger('remapped reducers', remappedReducers);
161
- var effectsMetadata = Reflect.getMetadata('@Effect', this) || {};
162
- this.logger('Effects', effectsMetadata);
163
- var remappedEffects = this.remapEffects(effectsMetadata);
164
- this.logger('remapped effects', remappedEffects);
165
- this.actions$.pipe(tap(function (action) {
166
- _this._loading$.next({
167
- type: action.type,
168
- code: _this.actionsMetadata.getActionCodeFromChild(action.type),
169
- payload: action.payload,
170
- status: action.type.endsWith('_REQUEST')
171
- });
172
- }), takeUntil(this.destroy$)).subscribe(function (action) {
173
- _this.logger('got type', action);
174
- var type = action.type;
175
- // effects are associated with requests
176
- if (type.endsWith(REQUEST_SUFFIX)) {
177
- if (remappedEffects.hasOwnProperty(type)) {
178
- var effectName_1 = remappedEffects[type];
179
- _this.logger('should call', effectName_1);
180
- if (_this[effectName_1] instanceof Function) {
181
- var retValue = _this[effectName_1](action.payload);
182
- if (retValue instanceof Observable) {
183
- retValue.pipe(catchError(function (err) {
184
- console.error("@Effect: " + effectName_1 + " thrown the following error");
185
- console.error(err);
186
- _this._loading$.next({
187
- status: false,
188
- payload: action.payload,
189
- code: _this.actionsMetadata.getActionCodeFromChild(action.type),
190
- type: action.type,
191
- });
192
- _this._error$.next({ action: _this.actionsMetadata.getActionCodeFromChild(action.type), error: err });
193
- return throwError(err);
194
- }), takeUntil(_this.destroy$)).subscribe(function (value) {
195
- _this.logger('got data', value);
196
- _this.actions$.next({
197
- type: remappedEffects[effectName_1],
198
- payload: value
199
- });
200
- });
201
- }
202
- }
203
- }
204
- else {
205
- // if there is no effect associated with this action then we can proceed calling the reducer associated
206
- _this.actions$.next({
207
- type: type.replace(REQUEST_SUFFIX, RESPONSE_SUFFIX),
208
- payload: action.payload
209
- });
210
- }
211
- }
212
- else if (type.endsWith(RESPONSE_SUFFIX)) {
213
- var fn = remappedReducers[action.type];
214
- _this.logger('should call fn', fn);
215
- if (_this[remappedReducers[action.type]]) {
216
- try {
217
- _this.state = _this[remappedReducers[action.type]](_this.state, action.payload);
218
- }
219
- catch (e) {
220
- // we don't need to stop loading here at it has already been stopped
221
- console.error("@Reduce " + fn + " thrown an error", e);
222
- _this._error$.next({ action: _this.actionsMetadata.getActionCodeFromChild(action.type), error: e });
223
- }
224
- }
225
- }
226
- }, function (err) {
227
- _this.logger('got error', err);
228
- // TODO should handle the error or should we change loading$ to a more generic status$ so we can pass altogether
229
- // loadings and errors events?
230
- _this._error$.next(err);
231
- console.error(err);
232
- });
233
- }
234
- Object.defineProperty(BaseStore.prototype, "state", {
235
- get: function () {
236
- return this._state;
237
- },
238
- set: function (value) {
239
- this.logger('setting new state', value);
240
- this._state = value;
241
- this._store$.next(value);
242
- if (this.storeMetadata.useLocalStorage) {
243
- this.logger('saving on localStorage');
244
- localStorage.setItem(this.storeMetadata.name, JSON.stringify(value));
245
- }
246
- },
247
- enumerable: true,
248
- configurable: true
249
- });
250
- Object.defineProperty(BaseStore.prototype, "store$", {
251
- get: function () {
252
- return this._store$.asObservable();
253
- },
254
- enumerable: true,
255
- configurable: true
256
- });
257
- Object.defineProperty(BaseStore.prototype, "loading$", {
258
- get: function () {
259
- return this._loading$.asObservable();
260
- },
261
- enumerable: true,
262
- configurable: true
263
- });
264
- Object.defineProperty(BaseStore.prototype, "error$", {
265
- get: function () {
266
- return this._error$.asObservable();
267
- },
268
- enumerable: true,
269
- configurable: true
270
- });
271
- BaseStore.prototype.dispatch = function (type, payload) {
272
- var requestAction = this.actionsMetadata.getActionsByCode(type).request;
273
- this.logger('dispatching type', requestAction);
274
- this.actions$.next({ type: requestAction, payload: payload });
275
- };
276
- BaseStore.prototype.parseReducers = function (reducers) {
277
- var _this = this;
278
- var remappedReducers = {};
279
- Object.keys(reducers).forEach(function (action) {
280
- if (+action >= 0) {
281
- var _action = Number(action);
282
- // this.logger('remapping', type, 'to');
283
- var realAction = _this.actionsMetadata.getActionsByCode(_action).response;
284
- // this.logger('real type to map to', realAction);
285
- remappedReducers[realAction] = reducers[action];
286
- }
287
- });
288
- return remappedReducers;
289
- };
290
- BaseStore.prototype.remapEffects = function (effectsMetadata) {
291
- var _this = this;
292
- var remappedEffects = {};
293
- Object.keys(effectsMetadata).forEach(function (action) {
294
- // effects should react to REQUEST events
295
- if (+action >= 0) {
296
- var _action = Number(action);
297
- var realAction = _this.actionsMetadata.getActionsByCode(_action).request;
298
- remappedEffects[realAction] = effectsMetadata[action];
299
- // mapping the effect function so that it can return a response type
300
- remappedEffects[effectsMetadata[action]] = _this.actionsMetadata.getActionsByCode(_action).response;
301
- }
302
- });
303
- return remappedEffects;
304
- };
305
- BaseStore.prototype.ngOnDestroy = function () {
306
- this.destroy$.next();
307
- this.destroy$.complete();
308
- };
309
- return BaseStore;
310
- }());
311
-
312
- function Effect(onAction) {
313
- return function (target, key) {
314
- var d = getDebugger("microphi:@Effect:" + target.constructor.name);
315
- d('decorating', key, 'with type', onAction);
316
- var effects = Reflect.getMetadata('@Effect', target) || {};
317
- effects[onAction] = key;
318
- return Reflect.defineMetadata('@Effect', effects, target);
319
- };
320
- }
321
-
322
- var ObservableList = /** @class */ (function () {
323
- function ObservableList(items, idFieldName) {
324
- var e_1, _a;
325
- var _this = this;
326
- if (idFieldName === void 0) { idFieldName = 'id'; }
327
- this.idFieldName = idFieldName;
328
- this.ids = new Set();
329
- this.data = new Map();
330
- var ids = items.map(function (i) { return i[_this.idFieldName]; });
331
- this.ids = new Set(ids);
332
- try {
333
- for (var ids_1 = __values(ids), ids_1_1 = ids_1.next(); !ids_1_1.done; ids_1_1 = ids_1.next()) {
334
- var id = ids_1_1.value;
335
- this.data.set(id, new BehaviorSubject(items[id]));
336
- }
337
- }
338
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
339
- finally {
340
- try {
341
- if (ids_1_1 && !ids_1_1.done && (_a = ids_1.return)) _a.call(ids_1);
342
- }
343
- finally { if (e_1) throw e_1.error; }
344
- }
345
- }
346
- ObservableList.difference = function (setA, setB) {
347
- var e_2, _a;
348
- var _difference = new Set(setA);
349
- try {
350
- for (var setB_1 = __values(setB), setB_1_1 = setB_1.next(); !setB_1_1.done; setB_1_1 = setB_1.next()) {
351
- var elem = setB_1_1.value;
352
- _difference.delete(elem);
353
- }
354
- }
355
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
356
- finally {
357
- try {
358
- if (setB_1_1 && !setB_1_1.done && (_a = setB_1.return)) _a.call(setB_1);
359
- }
360
- finally { if (e_2) throw e_2.error; }
361
- }
362
- return _difference;
363
- };
364
- ObservableList.intersection = function (setA, setB) {
365
- var e_3, _a;
366
- var _intersection = new Set();
367
- try {
368
- for (var setB_2 = __values(setB), setB_2_1 = setB_2.next(); !setB_2_1.done; setB_2_1 = setB_2.next()) {
369
- var elem = setB_2_1.value;
370
- if (setA.has(elem)) {
371
- _intersection.add(elem);
372
- }
373
- }
374
- }
375
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
376
- finally {
377
- try {
378
- if (setB_2_1 && !setB_2_1.done && (_a = setB_2.return)) _a.call(setB_2);
379
- }
380
- finally { if (e_3) throw e_3.error; }
381
- }
382
- return _intersection;
383
- };
384
- ObservableList.symmetricDifference = function (setA, setB) {
385
- var e_4, _a;
386
- var _difference = new Set(setA);
387
- try {
388
- for (var setB_3 = __values(setB), setB_3_1 = setB_3.next(); !setB_3_1.done; setB_3_1 = setB_3.next()) {
389
- var elem = setB_3_1.value;
390
- if (_difference.has(elem)) {
391
- _difference.delete(elem);
392
- }
393
- else {
394
- _difference.add(elem);
395
- }
396
- }
397
- }
398
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
399
- finally {
400
- try {
401
- if (setB_3_1 && !setB_3_1.done && (_a = setB_3.return)) _a.call(setB_3);
402
- }
403
- finally { if (e_4) throw e_4.error; }
404
- }
405
- return _difference;
406
- };
407
- ObservableList.prototype[Symbol.iterator] = function () {
408
- var _a, _b, id, e_5_1;
409
- var e_5, _c;
410
- return __generator(this, function (_d) {
411
- switch (_d.label) {
412
- case 0:
413
- _d.trys.push([0, 5, 6, 7]);
414
- _a = __values(this.ids.values()), _b = _a.next();
415
- _d.label = 1;
416
- case 1:
417
- if (!!_b.done) return [3 /*break*/, 4];
418
- id = _b.value;
419
- return [4 /*yield*/, this.data.get(id)];
420
- case 2:
421
- _d.sent();
422
- _d.label = 3;
423
- case 3:
424
- _b = _a.next();
425
- return [3 /*break*/, 1];
426
- case 4: return [3 /*break*/, 7];
427
- case 5:
428
- e_5_1 = _d.sent();
429
- e_5 = { error: e_5_1 };
430
- return [3 /*break*/, 7];
431
- case 6:
432
- try {
433
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
434
- }
435
- finally { if (e_5) throw e_5.error; }
436
- return [7 /*endfinally*/];
437
- case 7: return [2 /*return*/];
438
- }
439
- });
440
- };
441
- ObservableList.prototype.push = function () {
442
- var e_6, _a;
443
- var items = [];
444
- for (var _i = 0; _i < arguments.length; _i++) {
445
- items[_i] = arguments[_i];
446
- }
447
- var id = this.idFieldName;
448
- var _loop_1 = function (item) {
449
- if (this_1.data.has(item[id])) {
450
- // check whether the item changed;
451
- var storedItem_1 = this_1.data.get(item[id]).getValue();
452
- var shouldUpdate = Object.keys(storedItem_1).some(function (key) {
453
- return storedItem_1[key] !== item[key];
454
- });
455
- if (shouldUpdate) {
456
- this_1.data.get(item[id]).next(item);
457
- }
458
- }
459
- else {
460
- this_1.ids.add(item[id]);
461
- this_1.data.set(item[id], new BehaviorSubject(item));
462
- }
463
- };
464
- var this_1 = this;
465
- try {
466
- for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
467
- var item = items_1_1.value;
468
- _loop_1(item);
469
- }
470
- }
471
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
472
- finally {
473
- try {
474
- if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
475
- }
476
- finally { if (e_6) throw e_6.error; }
477
- }
478
- };
479
- ObservableList.prototype.updateOne = function (item) {
480
- if (this.data.has(item[this.idFieldName])) {
481
- this.data.get(item[this.idFieldName]).next(item);
482
- }
483
- else {
484
- console.warn('item not found', item);
485
- }
486
- };
487
- ObservableList.prototype.toJSON = function () {
488
- var data = [];
489
- this.data.forEach(function (value) {
490
- data.push(value.getValue());
491
- });
492
- return data;
493
- };
494
- /**
495
- * Will update items that are already present only if they need to be updated and remove items that are not present.
496
- * @param items items of the list
497
- */
498
- ObservableList.prototype.set = function () {
499
- var e_7, _a, e_8, _b, e_9, _c;
500
- var _this = this;
501
- var items = [];
502
- for (var _i = 0; _i < arguments.length; _i++) {
503
- items[_i] = arguments[_i];
504
- }
505
- var newIds = items.map(function (i) { return i[_this.idFieldName]; });
506
- var intersection = ObservableList.intersection(this.ids, newIds);
507
- try {
508
- for (var intersection_1 = __values(intersection), intersection_1_1 = intersection_1.next(); !intersection_1_1.done; intersection_1_1 = intersection_1.next()) {
509
- var id = intersection_1_1.value;
510
- this.updateOne(items[id]);
511
- }
512
- }
513
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
514
- finally {
515
- try {
516
- if (intersection_1_1 && !intersection_1_1.done && (_a = intersection_1.return)) _a.call(intersection_1);
517
- }
518
- finally { if (e_7) throw e_7.error; }
519
- }
520
- var itemsToRemove = ObservableList.difference(this.ids, newIds);
521
- try {
522
- for (var itemsToRemove_1 = __values(itemsToRemove), itemsToRemove_1_1 = itemsToRemove_1.next(); !itemsToRemove_1_1.done; itemsToRemove_1_1 = itemsToRemove_1.next()) {
523
- var id = itemsToRemove_1_1.value;
524
- this.removeItem(id);
525
- }
526
- }
527
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
528
- finally {
529
- try {
530
- if (itemsToRemove_1_1 && !itemsToRemove_1_1.done && (_b = itemsToRemove_1.return)) _b.call(itemsToRemove_1);
531
- }
532
- finally { if (e_8) throw e_8.error; }
533
- }
534
- var itemsToAdd = ObservableList.difference(newIds, this.ids);
535
- try {
536
- for (var itemsToAdd_1 = __values(itemsToAdd), itemsToAdd_1_1 = itemsToAdd_1.next(); !itemsToAdd_1_1.done; itemsToAdd_1_1 = itemsToAdd_1.next()) {
537
- var id = itemsToAdd_1_1.value;
538
- this.addItem(items[id]);
539
- }
540
- }
541
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
542
- finally {
543
- try {
544
- if (itemsToAdd_1_1 && !itemsToAdd_1_1.done && (_c = itemsToAdd_1.return)) _c.call(itemsToAdd_1);
545
- }
546
- finally { if (e_9) throw e_9.error; }
547
- }
548
- };
549
- /**
550
- * Remove an item from the list given its id
551
- * @param id id of the item to remove
552
- */
553
- ObservableList.prototype.removeItem = function (id) {
554
- this.ids.delete(id);
555
- this.data.delete(id);
556
- };
557
- /**
558
- * Adds an new item to the list
559
- * @param item the item to be added
560
- */
561
- ObservableList.prototype.addItem = function (item) {
562
- var id = item[this.idFieldName];
563
- this.ids.add(id);
564
- this.data.set(id, new BehaviorSubject(item));
565
- };
566
- return ObservableList;
567
- }());
568
-
569
- var HttpActions;
570
- (function (HttpActions) {
571
- HttpActions[HttpActions["REQUEST"] = 0] = "REQUEST";
572
- HttpActions[HttpActions["RESPONSE"] = 1] = "RESPONSE";
573
- HttpActions[HttpActions["ERROR"] = 2] = "ERROR";
574
- })(HttpActions || (HttpActions = {}));
575
- var HttpStatusStore = /** @class */ (function (_super) {
576
- __extends(HttpStatusStore, _super);
577
- function HttpStatusStore() {
578
- var _this = _super !== null && _super.apply(this, arguments) || this;
579
- // TODO: add explicit constructor
580
- _this.isLoading$ = _this.store$.pipe(map(function (state) {
581
- return state.isLoading;
582
- }));
583
- return _this;
584
- }
585
- HttpStatusStore.prototype.onRequest = function () {
586
- return {
587
- isLoading: true
588
- };
589
- };
590
- HttpStatusStore.prototype.onResponse = function () {
591
- return {
592
- isLoading: false
593
- };
594
- };
595
- HttpStatusStore.prototype.onError = function (state, err) {
596
- return {
597
- isLoading: false,
598
- error: err
599
- };
600
- };
601
- HttpStatusStore.ɵprov = ɵɵdefineInjectable({ factory: function HttpStatusStore_Factory() { return new HttpStatusStore(); }, token: HttpStatusStore, providedIn: "root" });
602
- __decorate([
603
- Reduce(HttpActions.REQUEST)
604
- ], HttpStatusStore.prototype, "onRequest", null);
605
- __decorate([
606
- Reduce(HttpActions.RESPONSE)
607
- ], HttpStatusStore.prototype, "onResponse", null);
608
- __decorate([
609
- Reduce(HttpActions.ERROR)
610
- ], HttpStatusStore.prototype, "onError", null);
611
- HttpStatusStore = __decorate([
612
- Store({
613
- name: 'httpStatusStore',
614
- initialState: {
615
- isLoading: false,
616
- error: null
617
- },
618
- actions: HttpActions
619
- }),
620
- Injectable({
621
- providedIn: 'root'
622
- })
623
- ], HttpStatusStore);
624
- return HttpStatusStore;
625
- }(BaseStore));
626
-
627
- var HttpStatusInterceptor = /** @class */ (function () {
628
- function HttpStatusInterceptor(httpStatusStore) {
629
- this.httpStatusStore = httpStatusStore;
630
- }
631
- HttpStatusInterceptor.prototype.intercept = function (req, next) {
632
- var _this = this;
633
- this.httpStatusStore.dispatch(HttpActions.REQUEST);
634
- // extend server response observable with logging
635
- return next.handle(req)
636
- .pipe(delay(100), tap(
637
- // Succeeds when there is a response; ignore other events
638
- function () {
639
- _this.httpStatusStore.dispatch(HttpActions.RESPONSE);
640
- },
641
- // Operation failed; error is an HttpErrorResponse
642
- function (error) {
643
- _this.httpStatusStore.dispatch(HttpActions.ERROR, error);
644
- }));
645
- };
646
- HttpStatusInterceptor.ctorParameters = function () { return [
647
- { type: HttpStatusStore }
648
- ]; };
649
- HttpStatusInterceptor = __decorate([
650
- Injectable()
651
- ], HttpStatusInterceptor);
652
- return HttpStatusInterceptor;
653
- }());
654
-
655
- var List = /** @class */ (function () {
656
- function List(entities, IDField) {
657
- var _this = this;
658
- this.IDField = IDField;
659
- this.entities = new Map();
660
- /**
661
- * maps entities position from the original array to their ids
662
- */
663
- this.ids = [];
664
- this.ids = entities.map(function (e) { return _this.getId(e); });
665
- entities
666
- .forEach(function (item, idx) {
667
- var id = item[IDField];
668
- _this.entities.set(id, item);
669
- });
670
- }
671
- Object.defineProperty(List.prototype, "size", {
672
- get: function () {
673
- if (this.ids.length !== this.entities.size) {
674
- throw new Error("Size Mismatch " + this.ids.length + " !== " + this.entities.size);
675
- }
676
- return this.entities.size;
677
- },
678
- enumerable: true,
679
- configurable: true
680
- });
681
- List.prototype.get = function (id) {
682
- return this.entities.get(id);
683
- };
684
- List.prototype.getId = function (entity) {
685
- return entity[this.IDField];
686
- };
687
- List.prototype[Symbol.iterator] = function () {
688
- var _a, _b, _i, idx, id;
689
- return __generator(this, function (_c) {
690
- switch (_c.label) {
691
- case 0:
692
- _a = [];
693
- for (_b in this.ids)
694
- _a.push(_b);
695
- _i = 0;
696
- _c.label = 1;
697
- case 1:
698
- if (!(_i < _a.length)) return [3 /*break*/, 4];
699
- idx = _a[_i];
700
- id = this.ids[idx];
701
- return [4 /*yield*/, this.entities.get(id)];
702
- case 2:
703
- _c.sent();
704
- _c.label = 3;
705
- case 3:
706
- _i++;
707
- return [3 /*break*/, 1];
708
- case 4: return [2 /*return*/];
709
- }
710
- });
711
- };
712
- List.prototype.upsert = function () {
713
- var _this = this;
714
- var entities = [];
715
- for (var _i = 0; _i < arguments.length; _i++) {
716
- entities[_i] = arguments[_i];
717
- }
718
- entities.forEach(function (e) {
719
- var id = _this.getId(e);
720
- if (!_this.entities.has(id)) {
721
- _this.ids.push(id);
722
- }
723
- _this.entities.set(id, e);
724
- });
725
- };
726
- List.prototype.prepend = function () {
727
- var _this = this;
728
- var entities = [];
729
- for (var _i = 0; _i < arguments.length; _i++) {
730
- entities[_i] = arguments[_i];
731
- }
732
- entities.reverse().forEach(function (e) {
733
- var id = _this.getId(e);
734
- if (_this.entities.has(id)) {
735
- console.warn("Entity with id '" + id + "' already exists, moving to the top of the list. Entity will be updated");
736
- _this.delete(e);
737
- }
738
- _this.ids.unshift(id);
739
- _this.entities.set(id, e);
740
- });
741
- };
742
- List.prototype.delete = function () {
743
- var _this = this;
744
- var entities = [];
745
- for (var _i = 0; _i < arguments.length; _i++) {
746
- entities[_i] = arguments[_i];
747
- }
748
- entities.forEach(function (e) {
749
- var id = _this.getId(e);
750
- var indexOfId = _this.ids.findIndex(function (_id) { return _id === id; });
751
- if (indexOfId < 0) {
752
- console.warn("Cannot find id: " + id);
753
- }
754
- else {
755
- _this.ids.splice(indexOfId, 1);
756
- _this.entities.delete(id);
757
- }
758
- });
759
- };
760
- List.prototype.append = function () {
761
- var _this = this;
762
- var entities = [];
763
- for (var _i = 0; _i < arguments.length; _i++) {
764
- entities[_i] = arguments[_i];
765
- }
766
- entities.forEach(function (e) {
767
- var id = _this.getId(e);
768
- if (_this.entities.has(id)) {
769
- console.warn("Entity with id '" + id + "' already exists, moving to the end of the list. Entity will be updated.");
770
- _this.delete(e);
771
- }
772
- _this.ids.push(id);
773
- _this.entities.set(id, e);
774
- });
775
- };
776
- return List;
777
- }());
778
-
779
- /*
780
- * Public API Surface of store
781
- */
782
-
783
- /**
784
- * Generated bundle index. Do not edit.
785
- */
786
-
787
- export { Actions, BaseStore, ERROR_SUFFIX, Effect, HttpActions, HttpStatusInterceptor, HttpStatusStore, List, ObservableList, REQUEST_SUFFIX, RESPONSE_SUFFIX, Reduce, ReduceMetadata, Store, StoreMetadata, getReduceMetadata, getStoreMetadata };
788
- //# sourceMappingURL=microphi-store.js.map