@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,996 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@microgamma/loggator'), require('rxjs'), require('rxjs/operators'), require('@angular/core')) :
3
- typeof define === 'function' && define.amd ? define('@microphi/store', ['exports', '@microgamma/loggator', 'rxjs', 'rxjs/operators', '@angular/core'], factory) :
4
- (global = global || self, factory((global.microphi = global.microphi || {}, global.microphi.store = {}), global.loggator, global.rxjs, global.rxjs.operators, global.ng.core));
5
- }(this, (function (exports, loggator, rxjs, operators, core) { 'use strict';
6
-
7
- /*! *****************************************************************************
8
- Copyright (c) Microsoft Corporation. All rights reserved.
9
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
10
- this file except in compliance with the License. You may obtain a copy of the
11
- License at http://www.apache.org/licenses/LICENSE-2.0
12
-
13
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16
- MERCHANTABLITY OR NON-INFRINGEMENT.
17
-
18
- See the Apache Version 2.0 License for specific language governing permissions
19
- and limitations under the License.
20
- ***************************************************************************** */
21
- /* global Reflect, Promise */
22
-
23
- var extendStatics = function(d, b) {
24
- extendStatics = Object.setPrototypeOf ||
25
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
26
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
27
- return extendStatics(d, b);
28
- };
29
-
30
- function __extends(d, b) {
31
- extendStatics(d, b);
32
- function __() { this.constructor = d; }
33
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
34
- }
35
-
36
- var __assign = function() {
37
- __assign = Object.assign || function __assign(t) {
38
- for (var s, i = 1, n = arguments.length; i < n; i++) {
39
- s = arguments[i];
40
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
41
- }
42
- return t;
43
- };
44
- return __assign.apply(this, arguments);
45
- };
46
-
47
- function __rest(s, e) {
48
- var t = {};
49
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
50
- t[p] = s[p];
51
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
52
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
53
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
54
- t[p[i]] = s[p[i]];
55
- }
56
- return t;
57
- }
58
-
59
- function __decorate(decorators, target, key, desc) {
60
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
61
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
62
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
63
- return c > 3 && r && Object.defineProperty(target, key, r), r;
64
- }
65
-
66
- function __param(paramIndex, decorator) {
67
- return function (target, key) { decorator(target, key, paramIndex); }
68
- }
69
-
70
- function __metadata(metadataKey, metadataValue) {
71
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
72
- }
73
-
74
- function __awaiter(thisArg, _arguments, P, generator) {
75
- return new (P || (P = Promise))(function (resolve, reject) {
76
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
77
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
78
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
79
- step((generator = generator.apply(thisArg, _arguments || [])).next());
80
- });
81
- }
82
-
83
- function __generator(thisArg, body) {
84
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
85
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
86
- function verb(n) { return function (v) { return step([n, v]); }; }
87
- function step(op) {
88
- if (f) throw new TypeError("Generator is already executing.");
89
- while (_) try {
90
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
91
- if (y = 0, t) op = [op[0] & 2, t.value];
92
- switch (op[0]) {
93
- case 0: case 1: t = op; break;
94
- case 4: _.label++; return { value: op[1], done: false };
95
- case 5: _.label++; y = op[1]; op = [0]; continue;
96
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
97
- default:
98
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
99
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
100
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
101
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
102
- if (t[2]) _.ops.pop();
103
- _.trys.pop(); continue;
104
- }
105
- op = body.call(thisArg, _);
106
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
107
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
108
- }
109
- }
110
-
111
- function __exportStar(m, exports) {
112
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
113
- }
114
-
115
- function __values(o) {
116
- var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
117
- if (m) return m.call(o);
118
- return {
119
- next: function () {
120
- if (o && i >= o.length) o = void 0;
121
- return { value: o && o[i++], done: !o };
122
- }
123
- };
124
- }
125
-
126
- function __read(o, n) {
127
- var m = typeof Symbol === "function" && o[Symbol.iterator];
128
- if (!m) return o;
129
- var i = m.call(o), r, ar = [], e;
130
- try {
131
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
132
- }
133
- catch (error) { e = { error: error }; }
134
- finally {
135
- try {
136
- if (r && !r.done && (m = i["return"])) m.call(i);
137
- }
138
- finally { if (e) throw e.error; }
139
- }
140
- return ar;
141
- }
142
-
143
- function __spread() {
144
- for (var ar = [], i = 0; i < arguments.length; i++)
145
- ar = ar.concat(__read(arguments[i]));
146
- return ar;
147
- }
148
-
149
- function __spreadArrays() {
150
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
151
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
152
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
153
- r[k] = a[j];
154
- return r;
155
- };
156
-
157
- function __await(v) {
158
- return this instanceof __await ? (this.v = v, this) : new __await(v);
159
- }
160
-
161
- function __asyncGenerator(thisArg, _arguments, generator) {
162
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
163
- var g = generator.apply(thisArg, _arguments || []), i, q = [];
164
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
165
- function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
166
- function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
167
- function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
168
- function fulfill(value) { resume("next", value); }
169
- function reject(value) { resume("throw", value); }
170
- function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
171
- }
172
-
173
- function __asyncDelegator(o) {
174
- var i, p;
175
- return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
176
- function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
177
- }
178
-
179
- function __asyncValues(o) {
180
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
181
- var m = o[Symbol.asyncIterator], i;
182
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
183
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
184
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
185
- }
186
-
187
- function __makeTemplateObject(cooked, raw) {
188
- if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
189
- return cooked;
190
- };
191
-
192
- function __importStar(mod) {
193
- if (mod && mod.__esModule) return mod;
194
- var result = {};
195
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
196
- result.default = mod;
197
- return result;
198
- }
199
-
200
- function __importDefault(mod) {
201
- return (mod && mod.__esModule) ? mod : { default: mod };
202
- }
203
-
204
- var REQUEST_SUFFIX = '_REQUEST';
205
- var RESPONSE_SUFFIX = '_RESPONSE';
206
- var ERROR_SUFFIX = '_ERROR';
207
- /**
208
- * Action are defined with an enum. Such as:
209
- *
210
- * emun {
211
- * GET_ALL,
212
- * GET_ONE,
213
- * DELETE,
214
- * }
215
- *
216
- * For each action defined a REQUEST, a RESPONSE and an ERROR are associated to it.
217
- *
218
- *
219
- */
220
- var Actions = /** @class */ (function () {
221
- function Actions(actions) {
222
- var _this = this;
223
- /**
224
- * Example:
225
- * {
226
- * 0: { request: 'ACTION_NAME_REQUEST', response: 'ACTION_NAME_RESPONSE', error: 'ACTION_NAME_ERROR' }
227
- * [....]
228
- * }
229
- */
230
- this.actions = {};
231
- /**
232
- * Example:
233
- * {
234
- * ACTION_NAME: 0
235
- * [....]
236
- * }
237
- *
238
- */
239
- this.names = {};
240
- /**
241
- * Example:
242
- * {
243
- * 0: 'ACTION_NAME',
244
- * [....]
245
- *
246
- * }
247
- */
248
- this.codes = {};
249
- /**
250
- * Example:
251
- * {
252
- * ACTION_NAME': { request: 'ACTION_NAME_REQUEST', response: 'ACTION_NAME_RESPONSE', error: 'ACTION_NAME_ERROR' }
253
- * [....]
254
- * }
255
- */
256
- this.restActionsByName = {};
257
- Object.keys(actions).forEach(function (key) {
258
- if (+key >= 0) {
259
- _this.codes[key] = actions[key];
260
- _this.actions[key] = Actions.createAction(actions[key]);
261
- }
262
- else {
263
- _this.names[key] = actions[key];
264
- _this.restActionsByName[key] = Actions.createAction(key);
265
- }
266
- });
267
- }
268
- Actions.createAction = function (key) {
269
- return {
270
- request: "" + key + REQUEST_SUFFIX,
271
- response: "" + key + RESPONSE_SUFFIX,
272
- error: "" + key + ERROR_SUFFIX,
273
- };
274
- };
275
- Actions.prototype.getByName = function (actionName) {
276
- return this.names[actionName];
277
- };
278
- Actions.prototype.getByCode = function (code) {
279
- return this.codes[code];
280
- };
281
- Actions.prototype.getActionsByCode = function (key) {
282
- return this.actions[key];
283
- };
284
- Actions.prototype.getActionsByName = function (actionName) {
285
- var code = this.getByName(actionName);
286
- return this.getActionsByCode(code);
287
- };
288
- Actions.prototype.getActionNameFromChild = function (childAction) {
289
- var _this = this;
290
- var actionName;
291
- Object.keys(this.restActionsByName).forEach(function (restAction) {
292
- var restActions = _this.restActionsByName[restAction];
293
- if (restActions.response === childAction || restActions.request === childAction || restActions.error === childAction) {
294
- actionName = restAction;
295
- }
296
- });
297
- return actionName;
298
- };
299
- Actions.prototype.getActionCodeFromChild = function (childAction) {
300
- var _this = this;
301
- var actionCode;
302
- Object.keys(this.actions).forEach(function (restAction) {
303
- var restActions = _this.actions[restAction];
304
- if (restActions.response === childAction || restActions.request === childAction || restActions.error === childAction) {
305
- actionCode = restAction;
306
- }
307
- });
308
- return Number(actionCode);
309
- };
310
- return Actions;
311
- }());
312
-
313
- var StoreMetadata = '@Store';
314
- function Store(options) {
315
- var d = loggator.getDebugger("microphi:@Store:" + options.name);
316
- return function (target) {
317
- d('running store decorator');
318
- Reflect.defineMetadata(StoreMetadata, options, target);
319
- };
320
- }
321
- function getStoreMetadata(instance) {
322
- return Reflect.getMetadata(StoreMetadata, instance.constructor);
323
- }
324
-
325
- var ReduceMetadata = '@Reduce';
326
- function Reduce(onAction) {
327
- return function (target, key) {
328
- var d = loggator.getDebugger("microphi:@Reduce:" + target.constructor.name);
329
- var reducer = Reflect.getMetadata(ReduceMetadata, target) || {};
330
- reducer[onAction] = key;
331
- d('stored reducers', reducer);
332
- return Reflect.defineMetadata(ReduceMetadata, reducer, target);
333
- };
334
- }
335
- function getReduceMetadata(instance) {
336
- return Reflect.getMetadata(ReduceMetadata, instance) || {};
337
- }
338
-
339
- var BaseStore = /** @class */ (function () {
340
- function BaseStore() {
341
- var _this = this;
342
- this.logger = loggator.getDebugger("microphi:BaseStore:" + this.constructor.name);
343
- this.actions$ = new rxjs.Subject();
344
- this._loading$ = new rxjs.Subject();
345
- this._error$ = new rxjs.Subject();
346
- this.destroy$ = new rxjs.Subject();
347
- this.storeMetadata = getStoreMetadata(this);
348
- this.logger('@Store', this.storeMetadata);
349
- this._store$ = new rxjs.BehaviorSubject(this.storeMetadata.initialState);
350
- // dereference initial state so that is does not leak between instances
351
- this._state = Object.assign({}, this.storeMetadata.initialState);
352
- this.logger('InitialState', this.state);
353
- this.actionsMetadata = new Actions(this.storeMetadata.actions);
354
- var reducerMetadata = getReduceMetadata(this);
355
- this.logger('Reducers', reducerMetadata);
356
- var remappedReducers = this.parseReducers(reducerMetadata);
357
- this.logger('remapped reducers', remappedReducers);
358
- var effectsMetadata = Reflect.getMetadata('@Effect', this) || {};
359
- this.logger('Effects', effectsMetadata);
360
- var remappedEffects = this.remapEffects(effectsMetadata);
361
- this.logger('remapped effects', remappedEffects);
362
- this.actions$.pipe(operators.tap(function (action) {
363
- _this._loading$.next({
364
- type: action.type,
365
- code: _this.actionsMetadata.getActionCodeFromChild(action.type),
366
- payload: action.payload,
367
- status: action.type.endsWith('_REQUEST')
368
- });
369
- }), operators.takeUntil(this.destroy$)).subscribe(function (action) {
370
- _this.logger('got type', action);
371
- var type = action.type;
372
- // effects are associated with requests
373
- if (type.endsWith(REQUEST_SUFFIX)) {
374
- if (remappedEffects.hasOwnProperty(type)) {
375
- var effectName_1 = remappedEffects[type];
376
- _this.logger('should call', effectName_1);
377
- if (_this[effectName_1] instanceof Function) {
378
- var retValue = _this[effectName_1](action.payload);
379
- if (retValue instanceof rxjs.Observable) {
380
- retValue.pipe(operators.catchError(function (err) {
381
- console.error("@Effect: " + effectName_1 + " thrown the following error");
382
- console.error(err);
383
- _this._loading$.next({
384
- status: false,
385
- payload: action.payload,
386
- code: _this.actionsMetadata.getActionCodeFromChild(action.type),
387
- type: action.type,
388
- });
389
- _this._error$.next({ action: _this.actionsMetadata.getActionCodeFromChild(action.type), error: err });
390
- return rxjs.throwError(err);
391
- }), operators.takeUntil(_this.destroy$)).subscribe(function (value) {
392
- _this.logger('got data', value);
393
- _this.actions$.next({
394
- type: remappedEffects[effectName_1],
395
- payload: value
396
- });
397
- });
398
- }
399
- }
400
- }
401
- else {
402
- // if there is no effect associated with this action then we can proceed calling the reducer associated
403
- _this.actions$.next({
404
- type: type.replace(REQUEST_SUFFIX, RESPONSE_SUFFIX),
405
- payload: action.payload
406
- });
407
- }
408
- }
409
- else if (type.endsWith(RESPONSE_SUFFIX)) {
410
- var fn = remappedReducers[action.type];
411
- _this.logger('should call fn', fn);
412
- if (_this[remappedReducers[action.type]]) {
413
- try {
414
- _this.state = _this[remappedReducers[action.type]](_this.state, action.payload);
415
- }
416
- catch (e) {
417
- // we don't need to stop loading here at it has already been stopped
418
- console.error("@Reduce " + fn + " thrown an error", e);
419
- _this._error$.next({ action: _this.actionsMetadata.getActionCodeFromChild(action.type), error: e });
420
- }
421
- }
422
- }
423
- }, function (err) {
424
- _this.logger('got error', err);
425
- // TODO should handle the error or should we change loading$ to a more generic status$ so we can pass altogether
426
- // loadings and errors events?
427
- _this._error$.next(err);
428
- console.error(err);
429
- });
430
- }
431
- Object.defineProperty(BaseStore.prototype, "state", {
432
- get: function () {
433
- return this._state;
434
- },
435
- set: function (value) {
436
- this.logger('setting new state', value);
437
- this._state = value;
438
- this._store$.next(value);
439
- if (this.storeMetadata.useLocalStorage) {
440
- this.logger('saving on localStorage');
441
- localStorage.setItem(this.storeMetadata.name, JSON.stringify(value));
442
- }
443
- },
444
- enumerable: true,
445
- configurable: true
446
- });
447
- Object.defineProperty(BaseStore.prototype, "store$", {
448
- get: function () {
449
- return this._store$.asObservable();
450
- },
451
- enumerable: true,
452
- configurable: true
453
- });
454
- Object.defineProperty(BaseStore.prototype, "loading$", {
455
- get: function () {
456
- return this._loading$.asObservable();
457
- },
458
- enumerable: true,
459
- configurable: true
460
- });
461
- Object.defineProperty(BaseStore.prototype, "error$", {
462
- get: function () {
463
- return this._error$.asObservable();
464
- },
465
- enumerable: true,
466
- configurable: true
467
- });
468
- BaseStore.prototype.dispatch = function (type, payload) {
469
- var requestAction = this.actionsMetadata.getActionsByCode(type).request;
470
- this.logger('dispatching type', requestAction);
471
- this.actions$.next({ type: requestAction, payload: payload });
472
- };
473
- BaseStore.prototype.parseReducers = function (reducers) {
474
- var _this = this;
475
- var remappedReducers = {};
476
- Object.keys(reducers).forEach(function (action) {
477
- if (+action >= 0) {
478
- var _action = Number(action);
479
- // this.logger('remapping', type, 'to');
480
- var realAction = _this.actionsMetadata.getActionsByCode(_action).response;
481
- // this.logger('real type to map to', realAction);
482
- remappedReducers[realAction] = reducers[action];
483
- }
484
- });
485
- return remappedReducers;
486
- };
487
- BaseStore.prototype.remapEffects = function (effectsMetadata) {
488
- var _this = this;
489
- var remappedEffects = {};
490
- Object.keys(effectsMetadata).forEach(function (action) {
491
- // effects should react to REQUEST events
492
- if (+action >= 0) {
493
- var _action = Number(action);
494
- var realAction = _this.actionsMetadata.getActionsByCode(_action).request;
495
- remappedEffects[realAction] = effectsMetadata[action];
496
- // mapping the effect function so that it can return a response type
497
- remappedEffects[effectsMetadata[action]] = _this.actionsMetadata.getActionsByCode(_action).response;
498
- }
499
- });
500
- return remappedEffects;
501
- };
502
- BaseStore.prototype.ngOnDestroy = function () {
503
- this.destroy$.next();
504
- this.destroy$.complete();
505
- };
506
- return BaseStore;
507
- }());
508
-
509
- function Effect(onAction) {
510
- return function (target, key) {
511
- var d = loggator.getDebugger("microphi:@Effect:" + target.constructor.name);
512
- d('decorating', key, 'with type', onAction);
513
- var effects = Reflect.getMetadata('@Effect', target) || {};
514
- effects[onAction] = key;
515
- return Reflect.defineMetadata('@Effect', effects, target);
516
- };
517
- }
518
-
519
- var ObservableList = /** @class */ (function () {
520
- function ObservableList(items, idFieldName) {
521
- var e_1, _a;
522
- var _this = this;
523
- if (idFieldName === void 0) { idFieldName = 'id'; }
524
- this.idFieldName = idFieldName;
525
- this.ids = new Set();
526
- this.data = new Map();
527
- var ids = items.map(function (i) { return i[_this.idFieldName]; });
528
- this.ids = new Set(ids);
529
- try {
530
- for (var ids_1 = __values(ids), ids_1_1 = ids_1.next(); !ids_1_1.done; ids_1_1 = ids_1.next()) {
531
- var id = ids_1_1.value;
532
- this.data.set(id, new rxjs.BehaviorSubject(items[id]));
533
- }
534
- }
535
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
536
- finally {
537
- try {
538
- if (ids_1_1 && !ids_1_1.done && (_a = ids_1.return)) _a.call(ids_1);
539
- }
540
- finally { if (e_1) throw e_1.error; }
541
- }
542
- }
543
- ObservableList.difference = function (setA, setB) {
544
- var e_2, _a;
545
- var _difference = new Set(setA);
546
- try {
547
- for (var setB_1 = __values(setB), setB_1_1 = setB_1.next(); !setB_1_1.done; setB_1_1 = setB_1.next()) {
548
- var elem = setB_1_1.value;
549
- _difference.delete(elem);
550
- }
551
- }
552
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
553
- finally {
554
- try {
555
- if (setB_1_1 && !setB_1_1.done && (_a = setB_1.return)) _a.call(setB_1);
556
- }
557
- finally { if (e_2) throw e_2.error; }
558
- }
559
- return _difference;
560
- };
561
- ObservableList.intersection = function (setA, setB) {
562
- var e_3, _a;
563
- var _intersection = new Set();
564
- try {
565
- for (var setB_2 = __values(setB), setB_2_1 = setB_2.next(); !setB_2_1.done; setB_2_1 = setB_2.next()) {
566
- var elem = setB_2_1.value;
567
- if (setA.has(elem)) {
568
- _intersection.add(elem);
569
- }
570
- }
571
- }
572
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
573
- finally {
574
- try {
575
- if (setB_2_1 && !setB_2_1.done && (_a = setB_2.return)) _a.call(setB_2);
576
- }
577
- finally { if (e_3) throw e_3.error; }
578
- }
579
- return _intersection;
580
- };
581
- ObservableList.symmetricDifference = function (setA, setB) {
582
- var e_4, _a;
583
- var _difference = new Set(setA);
584
- try {
585
- for (var setB_3 = __values(setB), setB_3_1 = setB_3.next(); !setB_3_1.done; setB_3_1 = setB_3.next()) {
586
- var elem = setB_3_1.value;
587
- if (_difference.has(elem)) {
588
- _difference.delete(elem);
589
- }
590
- else {
591
- _difference.add(elem);
592
- }
593
- }
594
- }
595
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
596
- finally {
597
- try {
598
- if (setB_3_1 && !setB_3_1.done && (_a = setB_3.return)) _a.call(setB_3);
599
- }
600
- finally { if (e_4) throw e_4.error; }
601
- }
602
- return _difference;
603
- };
604
- ObservableList.prototype[Symbol.iterator] = function () {
605
- var _a, _b, id, e_5_1;
606
- var e_5, _c;
607
- return __generator(this, function (_d) {
608
- switch (_d.label) {
609
- case 0:
610
- _d.trys.push([0, 5, 6, 7]);
611
- _a = __values(this.ids.values()), _b = _a.next();
612
- _d.label = 1;
613
- case 1:
614
- if (!!_b.done) return [3 /*break*/, 4];
615
- id = _b.value;
616
- return [4 /*yield*/, this.data.get(id)];
617
- case 2:
618
- _d.sent();
619
- _d.label = 3;
620
- case 3:
621
- _b = _a.next();
622
- return [3 /*break*/, 1];
623
- case 4: return [3 /*break*/, 7];
624
- case 5:
625
- e_5_1 = _d.sent();
626
- e_5 = { error: e_5_1 };
627
- return [3 /*break*/, 7];
628
- case 6:
629
- try {
630
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
631
- }
632
- finally { if (e_5) throw e_5.error; }
633
- return [7 /*endfinally*/];
634
- case 7: return [2 /*return*/];
635
- }
636
- });
637
- };
638
- ObservableList.prototype.push = function () {
639
- var e_6, _a;
640
- var items = [];
641
- for (var _i = 0; _i < arguments.length; _i++) {
642
- items[_i] = arguments[_i];
643
- }
644
- var id = this.idFieldName;
645
- var _loop_1 = function (item) {
646
- if (this_1.data.has(item[id])) {
647
- // check whether the item changed;
648
- var storedItem_1 = this_1.data.get(item[id]).getValue();
649
- var shouldUpdate = Object.keys(storedItem_1).some(function (key) {
650
- return storedItem_1[key] !== item[key];
651
- });
652
- if (shouldUpdate) {
653
- this_1.data.get(item[id]).next(item);
654
- }
655
- }
656
- else {
657
- this_1.ids.add(item[id]);
658
- this_1.data.set(item[id], new rxjs.BehaviorSubject(item));
659
- }
660
- };
661
- var this_1 = this;
662
- try {
663
- for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
664
- var item = items_1_1.value;
665
- _loop_1(item);
666
- }
667
- }
668
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
669
- finally {
670
- try {
671
- if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
672
- }
673
- finally { if (e_6) throw e_6.error; }
674
- }
675
- };
676
- ObservableList.prototype.updateOne = function (item) {
677
- if (this.data.has(item[this.idFieldName])) {
678
- this.data.get(item[this.idFieldName]).next(item);
679
- }
680
- else {
681
- console.warn('item not found', item);
682
- }
683
- };
684
- ObservableList.prototype.toJSON = function () {
685
- var data = [];
686
- this.data.forEach(function (value) {
687
- data.push(value.getValue());
688
- });
689
- return data;
690
- };
691
- /**
692
- * Will update items that are already present only if they need to be updated and remove items that are not present.
693
- * @param items items of the list
694
- */
695
- ObservableList.prototype.set = function () {
696
- var e_7, _a, e_8, _b, e_9, _c;
697
- var _this = this;
698
- var items = [];
699
- for (var _i = 0; _i < arguments.length; _i++) {
700
- items[_i] = arguments[_i];
701
- }
702
- var newIds = items.map(function (i) { return i[_this.idFieldName]; });
703
- var intersection = ObservableList.intersection(this.ids, newIds);
704
- try {
705
- for (var intersection_1 = __values(intersection), intersection_1_1 = intersection_1.next(); !intersection_1_1.done; intersection_1_1 = intersection_1.next()) {
706
- var id = intersection_1_1.value;
707
- this.updateOne(items[id]);
708
- }
709
- }
710
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
711
- finally {
712
- try {
713
- if (intersection_1_1 && !intersection_1_1.done && (_a = intersection_1.return)) _a.call(intersection_1);
714
- }
715
- finally { if (e_7) throw e_7.error; }
716
- }
717
- var itemsToRemove = ObservableList.difference(this.ids, newIds);
718
- try {
719
- for (var itemsToRemove_1 = __values(itemsToRemove), itemsToRemove_1_1 = itemsToRemove_1.next(); !itemsToRemove_1_1.done; itemsToRemove_1_1 = itemsToRemove_1.next()) {
720
- var id = itemsToRemove_1_1.value;
721
- this.removeItem(id);
722
- }
723
- }
724
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
725
- finally {
726
- try {
727
- if (itemsToRemove_1_1 && !itemsToRemove_1_1.done && (_b = itemsToRemove_1.return)) _b.call(itemsToRemove_1);
728
- }
729
- finally { if (e_8) throw e_8.error; }
730
- }
731
- var itemsToAdd = ObservableList.difference(newIds, this.ids);
732
- try {
733
- for (var itemsToAdd_1 = __values(itemsToAdd), itemsToAdd_1_1 = itemsToAdd_1.next(); !itemsToAdd_1_1.done; itemsToAdd_1_1 = itemsToAdd_1.next()) {
734
- var id = itemsToAdd_1_1.value;
735
- this.addItem(items[id]);
736
- }
737
- }
738
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
739
- finally {
740
- try {
741
- if (itemsToAdd_1_1 && !itemsToAdd_1_1.done && (_c = itemsToAdd_1.return)) _c.call(itemsToAdd_1);
742
- }
743
- finally { if (e_9) throw e_9.error; }
744
- }
745
- };
746
- /**
747
- * Remove an item from the list given its id
748
- * @param id id of the item to remove
749
- */
750
- ObservableList.prototype.removeItem = function (id) {
751
- this.ids.delete(id);
752
- this.data.delete(id);
753
- };
754
- /**
755
- * Adds an new item to the list
756
- * @param item the item to be added
757
- */
758
- ObservableList.prototype.addItem = function (item) {
759
- var id = item[this.idFieldName];
760
- this.ids.add(id);
761
- this.data.set(id, new rxjs.BehaviorSubject(item));
762
- };
763
- return ObservableList;
764
- }());
765
-
766
-
767
- (function (HttpActions) {
768
- HttpActions[HttpActions["REQUEST"] = 0] = "REQUEST";
769
- HttpActions[HttpActions["RESPONSE"] = 1] = "RESPONSE";
770
- HttpActions[HttpActions["ERROR"] = 2] = "ERROR";
771
- })(exports.HttpActions || (exports.HttpActions = {}));
772
- var HttpStatusStore = /** @class */ (function (_super) {
773
- __extends(HttpStatusStore, _super);
774
- function HttpStatusStore() {
775
- var _this = _super !== null && _super.apply(this, arguments) || this;
776
- // TODO: add explicit constructor
777
- _this.isLoading$ = _this.store$.pipe(operators.map(function (state) {
778
- return state.isLoading;
779
- }));
780
- return _this;
781
- }
782
- HttpStatusStore.prototype.onRequest = function () {
783
- return {
784
- isLoading: true
785
- };
786
- };
787
- HttpStatusStore.prototype.onResponse = function () {
788
- return {
789
- isLoading: false
790
- };
791
- };
792
- HttpStatusStore.prototype.onError = function (state, err) {
793
- return {
794
- isLoading: false,
795
- error: err
796
- };
797
- };
798
- HttpStatusStore.ɵprov = core["ɵɵdefineInjectable"]({ factory: function HttpStatusStore_Factory() { return new HttpStatusStore(); }, token: HttpStatusStore, providedIn: "root" });
799
- __decorate([
800
- Reduce(exports.HttpActions.REQUEST)
801
- ], HttpStatusStore.prototype, "onRequest", null);
802
- __decorate([
803
- Reduce(exports.HttpActions.RESPONSE)
804
- ], HttpStatusStore.prototype, "onResponse", null);
805
- __decorate([
806
- Reduce(exports.HttpActions.ERROR)
807
- ], HttpStatusStore.prototype, "onError", null);
808
- HttpStatusStore = __decorate([
809
- Store({
810
- name: 'httpStatusStore',
811
- initialState: {
812
- isLoading: false,
813
- error: null
814
- },
815
- actions: exports.HttpActions
816
- }),
817
- core.Injectable({
818
- providedIn: 'root'
819
- })
820
- ], HttpStatusStore);
821
- return HttpStatusStore;
822
- }(BaseStore));
823
-
824
- var HttpStatusInterceptor = /** @class */ (function () {
825
- function HttpStatusInterceptor(httpStatusStore) {
826
- this.httpStatusStore = httpStatusStore;
827
- }
828
- HttpStatusInterceptor.prototype.intercept = function (req, next) {
829
- var _this = this;
830
- this.httpStatusStore.dispatch(exports.HttpActions.REQUEST);
831
- // extend server response observable with logging
832
- return next.handle(req)
833
- .pipe(operators.delay(100), operators.tap(
834
- // Succeeds when there is a response; ignore other events
835
- function () {
836
- _this.httpStatusStore.dispatch(exports.HttpActions.RESPONSE);
837
- },
838
- // Operation failed; error is an HttpErrorResponse
839
- function (error) {
840
- _this.httpStatusStore.dispatch(exports.HttpActions.ERROR, error);
841
- }));
842
- };
843
- HttpStatusInterceptor.ctorParameters = function () { return [
844
- { type: HttpStatusStore }
845
- ]; };
846
- HttpStatusInterceptor = __decorate([
847
- core.Injectable()
848
- ], HttpStatusInterceptor);
849
- return HttpStatusInterceptor;
850
- }());
851
-
852
- var List = /** @class */ (function () {
853
- function List(entities, IDField) {
854
- var _this = this;
855
- this.IDField = IDField;
856
- this.entities = new Map();
857
- /**
858
- * maps entities position from the original array to their ids
859
- */
860
- this.ids = [];
861
- this.ids = entities.map(function (e) { return _this.getId(e); });
862
- entities
863
- .forEach(function (item, idx) {
864
- var id = item[IDField];
865
- _this.entities.set(id, item);
866
- });
867
- }
868
- Object.defineProperty(List.prototype, "size", {
869
- get: function () {
870
- if (this.ids.length !== this.entities.size) {
871
- throw new Error("Size Mismatch " + this.ids.length + " !== " + this.entities.size);
872
- }
873
- return this.entities.size;
874
- },
875
- enumerable: true,
876
- configurable: true
877
- });
878
- List.prototype.get = function (id) {
879
- return this.entities.get(id);
880
- };
881
- List.prototype.getId = function (entity) {
882
- return entity[this.IDField];
883
- };
884
- List.prototype[Symbol.iterator] = function () {
885
- var _a, _b, _i, idx, id;
886
- return __generator(this, function (_c) {
887
- switch (_c.label) {
888
- case 0:
889
- _a = [];
890
- for (_b in this.ids)
891
- _a.push(_b);
892
- _i = 0;
893
- _c.label = 1;
894
- case 1:
895
- if (!(_i < _a.length)) return [3 /*break*/, 4];
896
- idx = _a[_i];
897
- id = this.ids[idx];
898
- return [4 /*yield*/, this.entities.get(id)];
899
- case 2:
900
- _c.sent();
901
- _c.label = 3;
902
- case 3:
903
- _i++;
904
- return [3 /*break*/, 1];
905
- case 4: return [2 /*return*/];
906
- }
907
- });
908
- };
909
- List.prototype.upsert = function () {
910
- var _this = this;
911
- var entities = [];
912
- for (var _i = 0; _i < arguments.length; _i++) {
913
- entities[_i] = arguments[_i];
914
- }
915
- entities.forEach(function (e) {
916
- var id = _this.getId(e);
917
- if (!_this.entities.has(id)) {
918
- _this.ids.push(id);
919
- }
920
- _this.entities.set(id, e);
921
- });
922
- };
923
- List.prototype.prepend = function () {
924
- var _this = this;
925
- var entities = [];
926
- for (var _i = 0; _i < arguments.length; _i++) {
927
- entities[_i] = arguments[_i];
928
- }
929
- entities.reverse().forEach(function (e) {
930
- var id = _this.getId(e);
931
- if (_this.entities.has(id)) {
932
- console.warn("Entity with id '" + id + "' already exists, moving to the top of the list. Entity will be updated");
933
- _this.delete(e);
934
- }
935
- _this.ids.unshift(id);
936
- _this.entities.set(id, e);
937
- });
938
- };
939
- List.prototype.delete = function () {
940
- var _this = this;
941
- var entities = [];
942
- for (var _i = 0; _i < arguments.length; _i++) {
943
- entities[_i] = arguments[_i];
944
- }
945
- entities.forEach(function (e) {
946
- var id = _this.getId(e);
947
- var indexOfId = _this.ids.findIndex(function (_id) { return _id === id; });
948
- if (indexOfId < 0) {
949
- console.warn("Cannot find id: " + id);
950
- }
951
- else {
952
- _this.ids.splice(indexOfId, 1);
953
- _this.entities.delete(id);
954
- }
955
- });
956
- };
957
- List.prototype.append = function () {
958
- var _this = this;
959
- var entities = [];
960
- for (var _i = 0; _i < arguments.length; _i++) {
961
- entities[_i] = arguments[_i];
962
- }
963
- entities.forEach(function (e) {
964
- var id = _this.getId(e);
965
- if (_this.entities.has(id)) {
966
- console.warn("Entity with id '" + id + "' already exists, moving to the end of the list. Entity will be updated.");
967
- _this.delete(e);
968
- }
969
- _this.ids.push(id);
970
- _this.entities.set(id, e);
971
- });
972
- };
973
- return List;
974
- }());
975
-
976
- exports.Actions = Actions;
977
- exports.BaseStore = BaseStore;
978
- exports.ERROR_SUFFIX = ERROR_SUFFIX;
979
- exports.Effect = Effect;
980
- exports.HttpStatusInterceptor = HttpStatusInterceptor;
981
- exports.HttpStatusStore = HttpStatusStore;
982
- exports.List = List;
983
- exports.ObservableList = ObservableList;
984
- exports.REQUEST_SUFFIX = REQUEST_SUFFIX;
985
- exports.RESPONSE_SUFFIX = RESPONSE_SUFFIX;
986
- exports.Reduce = Reduce;
987
- exports.ReduceMetadata = ReduceMetadata;
988
- exports.Store = Store;
989
- exports.StoreMetadata = StoreMetadata;
990
- exports.getReduceMetadata = getReduceMetadata;
991
- exports.getStoreMetadata = getStoreMetadata;
992
-
993
- Object.defineProperty(exports, '__esModule', { value: true });
994
-
995
- })));
996
- //# sourceMappingURL=microphi-store.umd.js.map