@fireflysemantics/slice 14.0.7

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.
@@ -0,0 +1,1863 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs'), require('rxjs/operators'), require('nanoid')) :
3
+ typeof define === 'function' && define.amd ? define('@fireflysemantics/slice', ['exports', 'rxjs', 'rxjs/operators', 'nanoid'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.fireflysemantics = global.fireflysemantics || {}, global.fireflysemantics.slice = {}), global.rxjs, global.rxjs.operators, global.nanoid));
5
+ }(this, (function (exports, rxjs, operators, nanoid) { 'use strict';
6
+
7
+ ;
8
+
9
+ var freeze = Object.freeze;
10
+ var ESTORE_DEFAULT_ID_KEY = "id";
11
+ var ESTORE_DEFAULT_GID_KEY = "gid";
12
+ var ESTORE_CONFIG_DEFAULT = freeze({
13
+ idKey: ESTORE_DEFAULT_ID_KEY,
14
+ guidKey: ESTORE_DEFAULT_GID_KEY
15
+ });
16
+ var AbstractStore = /** @class */ (function () {
17
+ function AbstractStore(config) {
18
+ /**
19
+ * Notifies observers of the store query.
20
+ */
21
+ this.notifyQuery = new rxjs.ReplaySubject(1);
22
+ /**
23
+ * The current query state.
24
+ */
25
+ this._query = '';
26
+ /**
27
+ * Primary index for the stores elements.
28
+ */
29
+ this.entries = new Map();
30
+ /**
31
+ * The element entries that are keyed by
32
+ * an id generated on the server.
33
+ */
34
+ this.idEntries = new Map();
35
+ /**
36
+ * Create notifications that broacast
37
+ * the entire set of entries.
38
+ */
39
+ this.notify = new rxjs.ReplaySubject(1);
40
+ /**
41
+ * Create notifications that broacast
42
+ * store or slice delta state changes.
43
+ */
44
+ this.notifyDelta = new rxjs.ReplaySubject(1);
45
+ this.config = config
46
+ ? freeze(Object.assign(Object.assign({}, ESTORE_CONFIG_DEFAULT), config))
47
+ : ESTORE_CONFIG_DEFAULT;
48
+ }
49
+ Object.defineProperty(AbstractStore.prototype, "query", {
50
+ /**
51
+ * @return A snapshot of the query state.
52
+ */
53
+ get: function () {
54
+ return this._query;
55
+ },
56
+ /**
57
+ * Sets the current query state and notifies observers.
58
+ */
59
+ set: function (query) {
60
+ this._query = query;
61
+ this.notifyQuery.next(this._query);
62
+ },
63
+ enumerable: false,
64
+ configurable: true
65
+ });
66
+ /**
67
+ * Observe the query.
68
+ * @example
69
+ <pre>
70
+ let query$ = source.observeQuery();
71
+ </pre>
72
+ */
73
+ AbstractStore.prototype.observeQuery = function () {
74
+ return this.notifyQuery.asObservable();
75
+ };
76
+ Object.defineProperty(AbstractStore.prototype, "ID_KEY", {
77
+ /**
78
+ * The current id key for the EStore instance.
79
+ * @return this.config.idKey;
80
+ */
81
+ get: function () {
82
+ return this.config.idKey;
83
+ },
84
+ enumerable: false,
85
+ configurable: true
86
+ });
87
+ Object.defineProperty(AbstractStore.prototype, "GUID_KEY", {
88
+ /**
89
+ * The current guid key for the EStore instance.
90
+ * @return this.config.guidKey;
91
+ */
92
+ get: function () {
93
+ return this.config.guidKey;
94
+ },
95
+ enumerable: false,
96
+ configurable: true
97
+ });
98
+ /**
99
+ * Call all the notifiers at once.
100
+ *
101
+ * @param v
102
+ * @param delta
103
+ */
104
+ AbstractStore.prototype.notifyAll = function (v, delta) {
105
+ this.notify.next(v);
106
+ this.notifyDelta.next(delta);
107
+ };
108
+ /**
109
+ * Observe store state changes.
110
+ * @param sort Optional sorting function yielding a sorted observable.
111
+ * @example
112
+ ```
113
+ let todos$ = source.observe();
114
+ //or with a sort by title function
115
+ let todos$ = source.observe((a, b)=>(a.title > b.title ? -1 : 1));
116
+ ```
117
+ */
118
+ AbstractStore.prototype.observe = function (sort) {
119
+ if (sort) {
120
+ return this.notify.pipe(operators.map(function (e) { return e.sort(sort); }));
121
+ }
122
+ return this.notify.asObservable();
123
+ };
124
+ /**
125
+ * Observe delta updates.
126
+ * @example
127
+ <pre>
128
+ let todos$ = source.observeDelta();
129
+ </pre>
130
+ */
131
+ AbstractStore.prototype.observeDelta = function () {
132
+ return this.notifyDelta.asObservable();
133
+ };
134
+ /**
135
+ * Check whether the store is empty.
136
+ *
137
+ * @return A hot {@link Observable} that indicates whether the store is empty.
138
+ *
139
+ * @example
140
+ <pre>
141
+ source.isEmpty();
142
+ </pre>
143
+ */
144
+ AbstractStore.prototype.isEmpty = function () {
145
+ return this.notify.pipe(operators.map(function (entries) { return entries.length == 0; }));
146
+ };
147
+ /**
148
+ * Check whether the store is empty.
149
+ *
150
+ * @return A snapshot that indicates whether the store is empty.
151
+ *
152
+ * @example
153
+ <pre>
154
+ source.isEmpty();
155
+ </pre>
156
+ */
157
+ AbstractStore.prototype.isEmptySnapshot = function () {
158
+ return Array.from(this.entries.values()).length == 0;
159
+ };
160
+ /**
161
+ * Returns the number of entries contained.
162
+ * @param p The predicate to apply in order to filter the count
163
+ */
164
+ AbstractStore.prototype.count = function (p) {
165
+ if (p) {
166
+ return this.notify.pipe(operators.map(function (e) { return e.reduce(function (total, e) { return total + (p(e) ? 1 : 0); }, 0); }));
167
+ }
168
+ return this.notify.pipe(operators.map(function (entries) { return entries.length; }));
169
+ };
170
+ /**
171
+ * Returns a snapshot of the number of entries contained in the store.
172
+ * @param p The predicate to apply in order to filter the count
173
+ */
174
+ AbstractStore.prototype.countSnapshot = function (p) {
175
+ if (p) {
176
+ return Array.from(this.entries.values()).filter(p).length;
177
+ }
178
+ return Array.from(this.entries.values()).length;
179
+ };
180
+ /**
181
+ * Snapshot of all entries.
182
+ *
183
+ * @return Snapshot array of all the elements the entities the store contains.
184
+ *
185
+ * @example Observe a snapshot of all the entities in the store.
186
+ ```
187
+ let selectedTodos:Todo[] = source.allSnapshot();
188
+ ```
189
+ */
190
+ AbstractStore.prototype.allSnapshot = function () {
191
+ return Array.from(this.entries.values());
192
+ };
193
+ /**
194
+ * Returns true if the entries contain the identified instance.
195
+ *
196
+ * @param target Either an instance of type `E` or a `guid` identifying the instance.
197
+ * @param byId Whether the lookup should be performed with the `id` key rather than the `guid`.
198
+ * @returns true if the instance identified by the guid exists, false otherwise.
199
+ *
200
+ * @example
201
+ <pre>
202
+ let contains:boolean = source.contains(guid);
203
+ </pre>
204
+ */
205
+ AbstractStore.prototype.contains = function (target) {
206
+ if (typeof target === "string") {
207
+ return this.entries.get(target) ? true : false;
208
+ }
209
+ var guid = target[this.config.guidKey];
210
+ return this.entries.get(guid) ? true : false;
211
+ };
212
+ /**
213
+ * Returns true if the entries contain the identified instance.
214
+ *
215
+ * @param target Either an instance of type `E` or a `id` identifying the instance.
216
+ * @returns true if the instance identified by the `id` exists, false otherwise.
217
+ *
218
+ * @example
219
+ <pre>
220
+ let contains:boolean = source.contains(guid);
221
+ </pre>
222
+ */
223
+ AbstractStore.prototype.containsById = function (target) {
224
+ if (typeof target === "string") {
225
+ return this.idEntries.get(target) ? true : false;
226
+ }
227
+ var id = target[this.config.idKey];
228
+ return this.idEntries.get(id) ? true : false;
229
+ };
230
+ /**
231
+ * Find and return the entity identified by the GUID parameter
232
+ * if it exists and return it.
233
+ *
234
+ * @param guid
235
+ * @return The entity instance if it exists, null otherwise
236
+ */
237
+ AbstractStore.prototype.findOne = function (guid) {
238
+ return this.entries.get(guid);
239
+ };
240
+ /**
241
+ * Find and return the entity identified by the ID parameter
242
+ * if it exists and return it.
243
+ *
244
+ * @param id
245
+ * @return The entity instance if it exists, null otherwise
246
+ */
247
+ AbstractStore.prototype.findOneByID = function (id) {
248
+ return this.idEntries.get(id);
249
+ };
250
+ /**
251
+ * Snapshot of the entries that match the predicate.
252
+ *
253
+ * @param p The predicate used to query for the selection.
254
+ * @return A snapshot array containing the entities that match the predicate.
255
+ *
256
+ * @example Select all the `Todo` instance where the `title` length is greater than 100.
257
+ ```
258
+ let todos:Todo[]=store.select(todo=>todo.title.length>100);
259
+ ```
260
+ */
261
+ AbstractStore.prototype.select = function (p) {
262
+ var selected = [];
263
+ Array.from(this.entries.values()).forEach(function (e) {
264
+ if (p(e)) {
265
+ selected.push(e);
266
+ }
267
+ });
268
+ return selected;
269
+ };
270
+ /**
271
+ * Compare entities by GUID
272
+ * @param e1 The first entity
273
+ * @param e2 The second entity
274
+ * @return true if the two entities have equal GUID ids
275
+ * @example Compare `todo1` with `todo2` by `gid`.
276
+ ```
277
+ if (equalsByGUID(todo1, todo2)){...};
278
+ ```
279
+ */
280
+ AbstractStore.prototype.equalsByGUID = function (e1, e2) {
281
+ return e1[this.GUID_KEY] == e2[this.GUID_KEY];
282
+ };
283
+ /**
284
+ * Compare entities by ID
285
+ * @param e1 The first entity
286
+ * @param e2 The second entity
287
+ * @return true if the two entities have equal ID ids
288
+ * @example Compare `todo1` with `todo2` by `id`.
289
+ ```
290
+ if (equalsByID(todo1, todo2)){...};
291
+ ```
292
+ */
293
+ AbstractStore.prototype.equalsByID = function (e1, e2) {
294
+ return e1[this.ID_KEY] == e2[this.ID_KEY];
295
+ };
296
+ /**
297
+ * Calls complete on all {@link BehaviorSubject} instances.
298
+ *
299
+ * Call destroy when disposing of the store.
300
+ */
301
+ AbstractStore.prototype.destroy = function () {
302
+ this.notify.complete();
303
+ this.notifyDelta.complete();
304
+ this.notifyQuery.complete();
305
+ };
306
+ return AbstractStore;
307
+ }());
308
+
309
+ /*! *****************************************************************************
310
+ Copyright (c) Microsoft Corporation.
311
+
312
+ Permission to use, copy, modify, and/or distribute this software for any
313
+ purpose with or without fee is hereby granted.
314
+
315
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
316
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
317
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
318
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
319
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
320
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
321
+ PERFORMANCE OF THIS SOFTWARE.
322
+ ***************************************************************************** */
323
+ /* global Reflect, Promise */
324
+ var extendStatics = function (d, b) {
325
+ extendStatics = Object.setPrototypeOf ||
326
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
327
+ function (d, b) { for (var p in b)
328
+ if (Object.prototype.hasOwnProperty.call(b, p))
329
+ d[p] = b[p]; };
330
+ return extendStatics(d, b);
331
+ };
332
+ function __extends(d, b) {
333
+ if (typeof b !== "function" && b !== null)
334
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
335
+ extendStatics(d, b);
336
+ function __() { this.constructor = d; }
337
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
338
+ }
339
+ var __assign = function () {
340
+ __assign = Object.assign || function __assign(t) {
341
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
342
+ s = arguments[i];
343
+ for (var p in s)
344
+ if (Object.prototype.hasOwnProperty.call(s, p))
345
+ t[p] = s[p];
346
+ }
347
+ return t;
348
+ };
349
+ return __assign.apply(this, arguments);
350
+ };
351
+ function __rest(s, e) {
352
+ var t = {};
353
+ for (var p in s)
354
+ if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
355
+ t[p] = s[p];
356
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
357
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
358
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
359
+ t[p[i]] = s[p[i]];
360
+ }
361
+ return t;
362
+ }
363
+ function __decorate(decorators, target, key, desc) {
364
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
365
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
366
+ r = Reflect.decorate(decorators, target, key, desc);
367
+ else
368
+ for (var i = decorators.length - 1; i >= 0; i--)
369
+ if (d = decorators[i])
370
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
371
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
372
+ }
373
+ function __param(paramIndex, decorator) {
374
+ return function (target, key) { decorator(target, key, paramIndex); };
375
+ }
376
+ function __metadata(metadataKey, metadataValue) {
377
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
378
+ return Reflect.metadata(metadataKey, metadataValue);
379
+ }
380
+ function __awaiter(thisArg, _arguments, P, generator) {
381
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
382
+ return new (P || (P = Promise))(function (resolve, reject) {
383
+ function fulfilled(value) { try {
384
+ step(generator.next(value));
385
+ }
386
+ catch (e) {
387
+ reject(e);
388
+ } }
389
+ function rejected(value) { try {
390
+ step(generator["throw"](value));
391
+ }
392
+ catch (e) {
393
+ reject(e);
394
+ } }
395
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
396
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
397
+ });
398
+ }
399
+ function __generator(thisArg, body) {
400
+ var _ = { label: 0, sent: function () { if (t[0] & 1)
401
+ throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
402
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
403
+ function verb(n) { return function (v) { return step([n, v]); }; }
404
+ function step(op) {
405
+ if (f)
406
+ throw new TypeError("Generator is already executing.");
407
+ while (_)
408
+ try {
409
+ 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)
410
+ return t;
411
+ if (y = 0, t)
412
+ op = [op[0] & 2, t.value];
413
+ switch (op[0]) {
414
+ case 0:
415
+ case 1:
416
+ t = op;
417
+ break;
418
+ case 4:
419
+ _.label++;
420
+ return { value: op[1], done: false };
421
+ case 5:
422
+ _.label++;
423
+ y = op[1];
424
+ op = [0];
425
+ continue;
426
+ case 7:
427
+ op = _.ops.pop();
428
+ _.trys.pop();
429
+ continue;
430
+ default:
431
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
432
+ _ = 0;
433
+ continue;
434
+ }
435
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
436
+ _.label = op[1];
437
+ break;
438
+ }
439
+ if (op[0] === 6 && _.label < t[1]) {
440
+ _.label = t[1];
441
+ t = op;
442
+ break;
443
+ }
444
+ if (t && _.label < t[2]) {
445
+ _.label = t[2];
446
+ _.ops.push(op);
447
+ break;
448
+ }
449
+ if (t[2])
450
+ _.ops.pop();
451
+ _.trys.pop();
452
+ continue;
453
+ }
454
+ op = body.call(thisArg, _);
455
+ }
456
+ catch (e) {
457
+ op = [6, e];
458
+ y = 0;
459
+ }
460
+ finally {
461
+ f = t = 0;
462
+ }
463
+ if (op[0] & 5)
464
+ throw op[1];
465
+ return { value: op[0] ? op[1] : void 0, done: true };
466
+ }
467
+ }
468
+ var __createBinding = Object.create ? (function (o, m, k, k2) {
469
+ if (k2 === undefined)
470
+ k2 = k;
471
+ Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
472
+ }) : (function (o, m, k, k2) {
473
+ if (k2 === undefined)
474
+ k2 = k;
475
+ o[k2] = m[k];
476
+ });
477
+ function __exportStar(m, o) {
478
+ for (var p in m)
479
+ if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p))
480
+ __createBinding(o, m, p);
481
+ }
482
+ function __values(o) {
483
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
484
+ if (m)
485
+ return m.call(o);
486
+ if (o && typeof o.length === "number")
487
+ return {
488
+ next: function () {
489
+ if (o && i >= o.length)
490
+ o = void 0;
491
+ return { value: o && o[i++], done: !o };
492
+ }
493
+ };
494
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
495
+ }
496
+ function __read(o, n) {
497
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
498
+ if (!m)
499
+ return o;
500
+ var i = m.call(o), r, ar = [], e;
501
+ try {
502
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
503
+ ar.push(r.value);
504
+ }
505
+ catch (error) {
506
+ e = { error: error };
507
+ }
508
+ finally {
509
+ try {
510
+ if (r && !r.done && (m = i["return"]))
511
+ m.call(i);
512
+ }
513
+ finally {
514
+ if (e)
515
+ throw e.error;
516
+ }
517
+ }
518
+ return ar;
519
+ }
520
+ /** @deprecated */
521
+ function __spread() {
522
+ for (var ar = [], i = 0; i < arguments.length; i++)
523
+ ar = ar.concat(__read(arguments[i]));
524
+ return ar;
525
+ }
526
+ /** @deprecated */
527
+ function __spreadArrays() {
528
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++)
529
+ s += arguments[i].length;
530
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
531
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
532
+ r[k] = a[j];
533
+ return r;
534
+ }
535
+ function __spreadArray(to, from, pack) {
536
+ if (pack || arguments.length === 2)
537
+ for (var i = 0, l = from.length, ar; i < l; i++) {
538
+ if (ar || !(i in from)) {
539
+ if (!ar)
540
+ ar = Array.prototype.slice.call(from, 0, i);
541
+ ar[i] = from[i];
542
+ }
543
+ }
544
+ return to.concat(ar || from);
545
+ }
546
+ function __await(v) {
547
+ return this instanceof __await ? (this.v = v, this) : new __await(v);
548
+ }
549
+ function __asyncGenerator(thisArg, _arguments, generator) {
550
+ if (!Symbol.asyncIterator)
551
+ throw new TypeError("Symbol.asyncIterator is not defined.");
552
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
553
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
554
+ function verb(n) { if (g[n])
555
+ i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
556
+ function resume(n, v) { try {
557
+ step(g[n](v));
558
+ }
559
+ catch (e) {
560
+ settle(q[0][3], e);
561
+ } }
562
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
563
+ function fulfill(value) { resume("next", value); }
564
+ function reject(value) { resume("throw", value); }
565
+ function settle(f, v) { if (f(v), q.shift(), q.length)
566
+ resume(q[0][0], q[0][1]); }
567
+ }
568
+ function __asyncDelegator(o) {
569
+ var i, p;
570
+ return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
571
+ 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; }
572
+ }
573
+ function __asyncValues(o) {
574
+ if (!Symbol.asyncIterator)
575
+ throw new TypeError("Symbol.asyncIterator is not defined.");
576
+ var m = o[Symbol.asyncIterator], i;
577
+ 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);
578
+ 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); }); }; }
579
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); }
580
+ }
581
+ function __makeTemplateObject(cooked, raw) {
582
+ if (Object.defineProperty) {
583
+ Object.defineProperty(cooked, "raw", { value: raw });
584
+ }
585
+ else {
586
+ cooked.raw = raw;
587
+ }
588
+ return cooked;
589
+ }
590
+ ;
591
+ var __setModuleDefault = Object.create ? (function (o, v) {
592
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
593
+ }) : function (o, v) {
594
+ o["default"] = v;
595
+ };
596
+ function __importStar(mod) {
597
+ if (mod && mod.__esModule)
598
+ return mod;
599
+ var result = {};
600
+ if (mod != null)
601
+ for (var k in mod)
602
+ if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
603
+ __createBinding(result, mod, k);
604
+ __setModuleDefault(result, mod);
605
+ return result;
606
+ }
607
+ function __importDefault(mod) {
608
+ return (mod && mod.__esModule) ? mod : { default: mod };
609
+ }
610
+ function __classPrivateFieldGet(receiver, state, kind, f) {
611
+ if (kind === "a" && !f)
612
+ throw new TypeError("Private accessor was defined without a getter");
613
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
614
+ throw new TypeError("Cannot read private member from an object whose class did not declare it");
615
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
616
+ }
617
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
618
+ if (kind === "m")
619
+ throw new TypeError("Private method is not writable");
620
+ if (kind === "a" && !f)
621
+ throw new TypeError("Private accessor was defined without a setter");
622
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
623
+ throw new TypeError("Cannot write private member to an object whose class did not declare it");
624
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
625
+ }
626
+
627
+ /**
628
+ * Returns all the entities are distinct by the
629
+ * `property` value argument.
630
+ *
631
+ * Note that the implementation uses a `Map<string, E>` to
632
+ * index the entities by key. Therefore the more recent occurences
633
+ * matching a key instance will overwrite the previous ones.
634
+ *
635
+ * @param property The name of the property to check for distinct values by.
636
+ * @param entities The entities in the array.
637
+ *
638
+ * @example
639
+ ```
640
+ let todos: Todo[] = [
641
+ { id: 1, title: "Lets do it!" },
642
+ { id: 1, title: "Lets do it again!" },
643
+ { id: 2, title: "All done!" }
644
+ ];
645
+
646
+ let todos2: Todo[] = [
647
+ { id: 1, title: "Lets do it!" },
648
+ { id: 2, title: "All done!" }
649
+ ];
650
+
651
+ expect(distinct(todos, "id").length).toEqual(2);
652
+ expect(distinct(todos2, "id").length).toEqual(2);
653
+
654
+ ```
655
+ */
656
+ function distinct(entities, property) {
657
+ var entitiesByProperty = new Map(entities.map(function (e) { return [e[property], e]; }));
658
+ return Array.from(entitiesByProperty.values());
659
+ }
660
+ /**
661
+ * Returns true if all the entities are distinct by the
662
+ * `property` value argument.
663
+ *
664
+ * @param property The name of the property to check for distinct values by.
665
+ * @param entities The entities in the array.
666
+ *
667
+ * @example
668
+ *
669
+ ```
670
+ let todos: Todo[] = [
671
+ { id: 1, title: "Lets do it!" },
672
+ { id: 1, title: "Lets do it again!" },
673
+ { id: 2, title: "All done!" }
674
+ ];
675
+
676
+ let todos2: Todo[] = [
677
+ { id: 1, title: "Lets do it!" },
678
+ { id: 2, title: "All done!" }
679
+ ];
680
+
681
+ expect(unique(todos, "id")).toBeFalsy();
682
+ expect(unique(todos2, "id")).toBeTruthy();
683
+ ```
684
+ */
685
+ function unique(entities, property) {
686
+ return entities.length == distinct(entities, property).length ? true : false;
687
+ }
688
+ /**
689
+ * Create a global ID
690
+ * @return The global id.
691
+ *
692
+ * @example
693
+ * let e.guid = GUID();
694
+ */
695
+ function GUID() {
696
+ return nanoid.nanoid();
697
+ }
698
+ /**
699
+ * Set the global identfication property on the instance.
700
+ *
701
+ * @param e Entity we want to set the global identifier on.
702
+ * @param gid The name of the `gid` property. If not specified it defaults to `ESTORE_CONFIG_DEFAULT.guidKey`.
703
+ */
704
+ function attachGUID(e, gid) {
705
+ var guidKey = gid ? gid : ESTORE_CONFIG_DEFAULT.guidKey;
706
+ var id = nanoid.nanoid();
707
+ e[guidKey] = id;
708
+ return id;
709
+ }
710
+ /**
711
+ * Set the global identfication property on the instance.
712
+ *
713
+ * @param e[] Entity array we want to set the global identifiers on.
714
+ * @param gid The name of the `gid` property. If not specified it defaults to `gid`.
715
+ */
716
+ function attachGUIDs(e, gid) {
717
+ e.forEach(function (e) {
718
+ attachGUID(e, gid);
719
+ });
720
+ }
721
+ /**
722
+ * Create a shallow copy of the argument.
723
+ * @param o The object to copy
724
+ */
725
+ function shallowCopy(o) {
726
+ return Object.assign({}, o);
727
+ }
728
+ /**
729
+ * Create a deep copy of the argument.
730
+ * @param o The object to copy
731
+ */
732
+ function deepCopy(o) {
733
+ return JSON.parse(JSON.stringify(o));
734
+ }
735
+ /**
736
+ * Gets the current active value from the `active`
737
+ * Map.
738
+ *
739
+ * This is used for the scenario where we are managing
740
+ * a single active instance. For example
741
+ * when selecting a book from a collection of books.
742
+ *
743
+ * The selected `Book` instance becomes the active value.
744
+ *
745
+ * @example
746
+ * const book:Book = getActiveValue(bookStore.active);
747
+ * @param m
748
+ */
749
+ function getActiveValue(m) {
750
+ if (m.size) {
751
+ return m.entries().next().value[1];
752
+ }
753
+ return null;
754
+ }
755
+ /**
756
+ * The method can be used to exclude keys from an instance
757
+ * of type `E`.
758
+ *
759
+ * We can use this to exclude values when searching an object.
760
+ *
761
+ * @param entity An instance of type E
762
+ * @param exclude The keys to exclude
763
+ *
764
+ * @example
765
+ * todo = { id: '1', description: 'Do it!' }
766
+ * let keys = excludeKeys<Todo>(todo, ['id]);
767
+ * // keys = ['description']
768
+ */
769
+ function excludeKeys(entity, exclude) {
770
+ var keys = Object.keys(entity);
771
+ return keys.filter(function (key) {
772
+ return exclude.indexOf(key) < 0;
773
+ });
774
+ }
775
+ /**
776
+ *
777
+ * @param entities The entity to search
778
+ * @param exclude Keys to exclude from each entity
779
+ *
780
+ * @return E[] Array of entities with properties containing the search term.
781
+ */
782
+ function search(query, entities, exclude) {
783
+ if (query === void 0) { query = ''; }
784
+ if (exclude === void 0) { exclude = []; }
785
+ var isArray = Array.isArray;
786
+ query = query.toLowerCase();
787
+ return entities.filter(function (e) {
788
+ //Do the keys calculation on each instance e:E
789
+ //because an instance can have optional parameters,
790
+ //and thus we have to check each instance, not just
791
+ //the first one in the array.
792
+ var keys = excludeKeys(e, exclude);
793
+ return keys.some(function (key) {
794
+ var value = e[key];
795
+ if (!value) {
796
+ return false;
797
+ }
798
+ if (isArray(value)) {
799
+ return value.some(function (v) {
800
+ return String(v).toLowerCase().includes(query);
801
+ });
802
+ }
803
+ else {
804
+ return String(value).toLowerCase().includes(query);
805
+ }
806
+ });
807
+ });
808
+ }
809
+ /**
810
+ * @param scrollable The element being scrolled
811
+ * @param debounceMS The number of milliseconds to debounce scroll events
812
+ * @param sp The function returning the scroll position coordinates.
813
+ * @return A boolean valued observable indicating whether the element is scrolling up or down
814
+ */
815
+ function scrollingUp(scrollable, debounceMS, sp) {
816
+ return rxjs.fromEvent(scrollable, 'scroll').pipe(operators.debounceTime(debounceMS), operators.distinctUntilChanged(), operators.map(function (v) { return sp(); }), operators.pairwise(), operators.switchMap(function (p) {
817
+ var y1 = p[0][1];
818
+ var y2 = p[1][1];
819
+ return y1 - y2 > 0 ? rxjs.of(false) : rxjs.of(true);
820
+ }));
821
+ }
822
+ /**
823
+ * Filters the entities properties to the set contained in the
824
+ * `keys` array.
825
+ *
826
+ * @param keys The array of keys that the entity be limited to
827
+ * @param entity The entity to map
828
+ * @return An entity instance that has only the keys provided in the keys array
829
+ */
830
+ function mapEntity(keys, entity) {
831
+ var result = {};
832
+ keys.forEach(function (k) {
833
+ result[k] = entity[k];
834
+ });
835
+ return result;
836
+ }
837
+
838
+ var isArray = Array.isArray;
839
+ var Slice = /** @class */ (function (_super) {
840
+ __extends(Slice, _super);
841
+ /**
842
+ * perform initial notification to all observers,
843
+ * such that operations like {@link combineLatest}{}
844
+ * will execute at least once.
845
+ *
846
+ * @param label The slice label
847
+ * @param predicate The slice predicate
848
+ * @param eStore The EStore instance containing the elements considered for slicing
849
+ *
850
+ * @example
851
+ <pre>
852
+ //Empty slice
853
+ new Slice<Todo>(Todo.COMPLETE, todo=>!todo.complete);
854
+
855
+ //Initialized slice
856
+ let todos = [new Todo(false, "You complete me!"),
857
+ new Todo(true, "You completed me!")];
858
+ new Slice<Todo>(Todo.COMPLETE, todo=>!todo.complete, todos);
859
+ </pre>
860
+ */
861
+ function Slice(label, predicate, eStore) {
862
+ var _this = _super.call(this) || this;
863
+ _this.label = label;
864
+ _this.predicate = predicate;
865
+ _this.eStore = eStore;
866
+ /* The slice element entries */
867
+ _this.entries = new Map();
868
+ var entities = eStore.allSnapshot();
869
+ _this.config = eStore.config;
870
+ var passed = _this.test(predicate, entities);
871
+ var delta = { type: "Initialize" /* INTIALIZE */, entries: passed };
872
+ _this.post(passed);
873
+ _this.notifyDelta.next(delta);
874
+ return _this;
875
+ }
876
+ /**
877
+ * Add the element if it satisfies the predicate
878
+ * and notify subscribers that an element was added.
879
+ *
880
+ * @param e The element to be considered for slicing
881
+ */
882
+ Slice.prototype.post = function (e) {
883
+ if (isArray(e)) {
884
+ this.postA(e);
885
+ }
886
+ else {
887
+ if (this.predicate(e)) {
888
+ var id = e[this.config.guidKey];
889
+ this.entries.set(id, e);
890
+ var delta = { type: "Post" /* POST */, entries: [e] };
891
+ this.notifyAll(__spreadArray([], __read(Array.from(this.entries.values()))), delta);
892
+ }
893
+ }
894
+ };
895
+ /**
896
+ * Add the elements if they satisfy the predicate
897
+ * and notify subscribers that elements were added.
898
+ *
899
+ * @param e The element to be considered for slicing
900
+ */
901
+ Slice.prototype.postN = function () {
902
+ var e = [];
903
+ for (var _i = 0; _i < arguments.length; _i++) {
904
+ e[_i] = arguments[_i];
905
+ }
906
+ this.postA(e);
907
+ };
908
+ /**
909
+ * Add the elements if they satisfy the predicate
910
+ * and notify subscribers that elements were added.
911
+ *
912
+ * @param e The element to be considered for slicing
913
+ */
914
+ Slice.prototype.postA = function (e) {
915
+ var _this = this;
916
+ var d = [];
917
+ e.forEach(function (e) {
918
+ if (_this.predicate(e)) {
919
+ var id = e[_this.config.guidKey];
920
+ _this.entries.set(id, e);
921
+ d.push(e);
922
+ }
923
+ });
924
+ var delta = { type: "Post" /* POST */, entries: d };
925
+ this.notifyAll(__spreadArray([], __read(Array.from(this.entries.values()))), delta);
926
+ };
927
+ /**
928
+ * Delete an element from the slice.
929
+ *
930
+ * @param e The element to be deleted if it satisfies the predicate
931
+ */
932
+ Slice.prototype.delete = function (e) {
933
+ if (isArray(e)) {
934
+ this.deleteA(e);
935
+ }
936
+ else {
937
+ if (this.predicate(e)) {
938
+ var id = e[this.config.guidKey];
939
+ this.entries.delete(id);
940
+ var delta = { type: "Delete" /* DELETE */, entries: [e] };
941
+ this.notifyAll(Array.from(this.entries.values()), delta);
942
+ }
943
+ }
944
+ };
945
+ /**
946
+ * @param e The elements to be deleted if it satisfies the predicate
947
+ */
948
+ Slice.prototype.deleteN = function () {
949
+ var e = [];
950
+ for (var _i = 0; _i < arguments.length; _i++) {
951
+ e[_i] = arguments[_i];
952
+ }
953
+ this.deleteA(e);
954
+ };
955
+ /**
956
+ * @param e The elements to be deleted if they satisfy the predicate
957
+ */
958
+ Slice.prototype.deleteA = function (e) {
959
+ var _this = this;
960
+ var d = [];
961
+ e.forEach(function (e) {
962
+ if (_this.predicate(e)) {
963
+ var id = e[_this.config.guidKey];
964
+ d.push(_this.entries.get(id));
965
+ _this.entries.delete(id);
966
+ }
967
+ });
968
+ var delta = { type: "Delete" /* DELETE */, entries: d };
969
+ this.notifyAll(__spreadArray([], __read(Array.from(this.entries.values()))), delta);
970
+ };
971
+ /**
972
+ * Update the slice when an Entity instance mutates.
973
+ *
974
+ * @param e The element to be added or deleted depending on predicate reevaluation
975
+ */
976
+ Slice.prototype.put = function (e) {
977
+ if (isArray(e)) {
978
+ this.putA(e);
979
+ }
980
+ else {
981
+ var id = e[this.config.guidKey];
982
+ if (this.entries.get(id)) {
983
+ if (!this.predicate(e)) {
984
+ //Note that this is a ActionTypes.DELETE because we are removing the
985
+ //entity from the slice.
986
+ var delta = { type: "Delete" /* DELETE */, entries: [e] };
987
+ this.entries.delete(id);
988
+ this.notifyAll(__spreadArray([], __read(Array.from(this.entries.values()))), delta);
989
+ }
990
+ }
991
+ else if (this.predicate(e)) {
992
+ this.entries.set(id, e);
993
+ var delta = { type: "Put" /* PUT */, entries: [e] };
994
+ this.notifyAll(__spreadArray([], __read(Array.from(this.entries.values()))), delta);
995
+ }
996
+ }
997
+ };
998
+ /**
999
+ * Update the slice with mutated Entity instances.
1000
+ *
1001
+ * @param e The elements to be deleted if it satisfies the predicate
1002
+ */
1003
+ Slice.prototype.putN = function () {
1004
+ var e = [];
1005
+ for (var _i = 0; _i < arguments.length; _i++) {
1006
+ e[_i] = arguments[_i];
1007
+ }
1008
+ this.putA(e);
1009
+ };
1010
+ /**
1011
+ * @param e The elements to be put
1012
+ */
1013
+ Slice.prototype.putA = function (e) {
1014
+ var _this = this;
1015
+ var d = []; //instances to delete
1016
+ var u = []; //instances to update
1017
+ e.forEach(function (e) {
1018
+ var id = e[_this.config.guidKey];
1019
+ if (_this.entries.get(id)) {
1020
+ if (!_this.predicate(e)) {
1021
+ d.push(_this.entries.get(id));
1022
+ }
1023
+ }
1024
+ else if (_this.predicate(e)) {
1025
+ u.push(e);
1026
+ }
1027
+ });
1028
+ if (d.length > 0) {
1029
+ d.forEach(function (e) {
1030
+ _this.entries.delete(e[_this.config.guidKey]);
1031
+ });
1032
+ var delta = { type: "Delete" /* DELETE */, entries: d };
1033
+ this.notifyAll(__spreadArray([], __read(Array.from(this.entries.values()))), delta);
1034
+ }
1035
+ if (u.length > 0) {
1036
+ u.forEach(function (e) {
1037
+ _this.entries.set(e[_this.config.guidKey], e);
1038
+ });
1039
+ var delta = { type: "Put" /* PUT */, entries: u };
1040
+ this.notifyAll(__spreadArray([], __read(Array.from(this.entries.values()))), delta);
1041
+ }
1042
+ };
1043
+ /**
1044
+ * Resets the slice to empty.
1045
+ */
1046
+ Slice.prototype.reset = function () {
1047
+ var delta = {
1048
+ type: "Reset" /* RESET */,
1049
+ entries: __spreadArray([], __read(Array.from(this.entries.values())))
1050
+ };
1051
+ this.notifyAll([], delta);
1052
+ this.entries = new Map();
1053
+ };
1054
+ /**
1055
+ * Utility method that applies the predicate to an array
1056
+ * of entities and return the ones that pass the test.
1057
+ *
1058
+ * Used to create an initial set of values
1059
+ * that should be part of the `Slice`.
1060
+ *
1061
+ * @param p
1062
+ * @param e
1063
+ * @return The the array of entities that pass the predicate test.
1064
+ */
1065
+ Slice.prototype.test = function (p, e) {
1066
+ var v = [];
1067
+ e.forEach(function (e) {
1068
+ if (p(e)) {
1069
+ v.push(e);
1070
+ }
1071
+ });
1072
+ return v;
1073
+ };
1074
+ return Slice;
1075
+ }(AbstractStore));
1076
+
1077
+ /**
1078
+ * This `todoFactory` code will be used to illustrate the API examples. The following
1079
+ * utilities are used in the tests and the API Typedoc examples contained here.
1080
+ * @example Utilities for API Examples
1081
+ ```
1082
+ export const enum TodoSliceEnum {
1083
+ COMPLETE = "Complete",
1084
+ INCOMPLETE = "Incomplete"
1085
+ }
1086
+
1087
+ export class Todo {
1088
+ constructor(public complete: boolean, public title: string,public gid?:string, public id?:string) {}
1089
+ }
1090
+
1091
+ export let todos = [new Todo(false, "You complete me!"), new Todo(true, "You completed me!")];
1092
+
1093
+ export function todosFactory():Todo[] {
1094
+ return [new Todo(false, "You complete me!"), new Todo(true, "You completed me!")];
1095
+ }
1096
+ ```
1097
+ */
1098
+ var EStore = /** @class */ (function (_super) {
1099
+ __extends(EStore, _super);
1100
+ /**
1101
+ * Store constructor (Initialization with element is optional)
1102
+ *
1103
+ * perform initial notification to all observers,
1104
+ * such that function like {@link combineLatest}{}
1105
+ * will execute at least once.
1106
+ * @param entities
1107
+ * @example Dynamic `EStore<Todo>` Creation
1108
+ ```
1109
+ // Initialize the Store
1110
+ let store: EStore<Todo> = new EStore<Todo>(todosFactory());
1111
+ ```*/
1112
+ function EStore(entities, config) {
1113
+ if (entities === void 0) { entities = []; }
1114
+ var _this = _super.call(this, config) || this;
1115
+ /**
1116
+ * An Observable<E[]> reference so that
1117
+ *
1118
+ */
1119
+ _this.observable = _this.observe();
1120
+ /**
1121
+ * Notifies observers when the store is empty.
1122
+ */
1123
+ _this.notifyActive = new rxjs.ReplaySubject(1);
1124
+ /**
1125
+ * `Map` of active entties. The instance is public and can be used
1126
+ * directly to add and remove active entities, however we recommend
1127
+ * using the {@link addActive} and {@link deleteActive} methods.
1128
+ */
1129
+ _this.active = new Map();
1130
+ /**
1131
+ * Notifies observers when the store is loading.
1132
+ *
1133
+ * This is a common pattern found when implementing
1134
+ * `Observable` data sources.
1135
+ */
1136
+ _this.notifyLoading = new rxjs.ReplaySubject(1);
1137
+ /**
1138
+ * The current loading state. Use loading when fetching new
1139
+ * data for the store. The default loading state is `true`.
1140
+ *
1141
+ * This is such that if data is fetched asynchronously
1142
+ * in a service, components can wait on loading notification
1143
+ * before attempting to retrieve data from the service.
1144
+ *
1145
+ * Loading could be based on a composite response. For example
1146
+ * when the stock and mutual funds have loaded, set loading to `false`.
1147
+ */
1148
+ _this._loading = true;
1149
+ /**
1150
+ * Notifies observers that a search is in progress.
1151
+ *
1152
+ * This is a common pattern found when implementing
1153
+ * `Observable` data sources.
1154
+ */
1155
+ _this.notifySearching = new rxjs.ReplaySubject(1);
1156
+ /**
1157
+ * The current `searching` state. Use `searching`
1158
+ * for example to display a spinnner
1159
+ * when performing a search.
1160
+ * The default `searching` state is `false`.
1161
+ */
1162
+ _this._searching = false;
1163
+ /**
1164
+ * Store slices
1165
+ */
1166
+ _this.slices = new Map();
1167
+ var delta = { type: "Initialize" /* INTIALIZE */, entries: entities };
1168
+ _this.post(entities);
1169
+ _this.notifyDelta.next(delta);
1170
+ return _this;
1171
+ }
1172
+ /**
1173
+ * Calls complete on all {@link BehaviorSubject} instances.
1174
+ *
1175
+ * Call destroy when disposing of the store.
1176
+ */
1177
+ EStore.prototype.destroy = function () {
1178
+ _super.prototype.destroy.call(this);
1179
+ this.notifyLoading.complete();
1180
+ this.notifyActive.complete();
1181
+ this.slices.forEach(function (slice) { return slice.destroy(); });
1182
+ };
1183
+ /**
1184
+ * Toggles the entity:
1185
+ *
1186
+ * If the store contains the entity
1187
+ * it will be deleted. If the store
1188
+ * does not contains the entity,
1189
+ * it is added.
1190
+ * @param e
1191
+ * @example Toggle the `Todo` instance
1192
+ ```
1193
+ estore.post(todo);
1194
+ // Remove todo
1195
+ estore.toggle(todo);
1196
+ // Add it back
1197
+ estore.toggle(todo);
1198
+
1199
+ ```
1200
+ */
1201
+ EStore.prototype.toggle = function (e) {
1202
+ if (this.contains(e)) {
1203
+ this.delete(e);
1204
+ }
1205
+ else {
1206
+ this.post(e);
1207
+ }
1208
+ };
1209
+ /**
1210
+ * Add multiple entity entities to active.
1211
+ *
1212
+ * If the entity is not contained in the store it is added
1213
+ * to the store before it is added to `active`.
1214
+ *
1215
+ * Also we clone the map prior to broadcasting it with
1216
+ * `notifyActive` to make sure we will trigger Angular
1217
+ * change detection in the event that it maintains
1218
+ * a reference to the `active` state `Map` instance.
1219
+ *
1220
+ * @example Add a `todo1` and `todo2` as active
1221
+ ```
1222
+ addActive(todo1);
1223
+ addActive(todo2);
1224
+ ```
1225
+ */
1226
+ EStore.prototype.addActive = function (e) {
1227
+ if (this.contains(e)) {
1228
+ this.active.set(e.gid, e);
1229
+ this.notifyActive.next(new Map(this.active));
1230
+ }
1231
+ else {
1232
+ this.post(e);
1233
+ this.active.set(e.gid, e);
1234
+ this.notifyActive.next(new Map(this.active));
1235
+ }
1236
+ };
1237
+ /**
1238
+ * Delete an entity as active.
1239
+ *
1240
+ * Also we clone the map prior to broadcasting it with
1241
+ * `notifyActive` to make sure we will trigger Angular
1242
+ * change detection in the event that it maintains
1243
+ * a reference to the `active` state `Map` instance.
1244
+ *
1245
+ * @example Mark a `todo` instance as active
1246
+ ```
1247
+ deleteActive(todo1);
1248
+ deleteActive(todo2);
1249
+ ```
1250
+ */
1251
+ EStore.prototype.deleteActive = function (e) {
1252
+ this.active.delete(e.gid);
1253
+ this.notifyActive.next(new Map(this.active));
1254
+ };
1255
+ /**
1256
+ * Clear / reset the active entity map.
1257
+ *
1258
+ * Also we clone the map prior to broadcasting it with
1259
+ * `notifyActive` to make sure we will trigger Angular
1260
+ * change detection in the event that it maintains
1261
+ * a reference to the `active` state `Map` instance.
1262
+ *
1263
+ * @example Mark a `todo` instance as active
1264
+ ```
1265
+ deleteActive(todo1);
1266
+ deleteActive(todo2);
1267
+ ```
1268
+ */
1269
+ EStore.prototype.clearActive = function () {
1270
+ this.active.clear();
1271
+ this.notifyActive.next(new Map(this.active));
1272
+ };
1273
+ /**
1274
+ * Observe the active entity.
1275
+ * @example
1276
+ <pre>
1277
+ let active$ = source.observeActive();
1278
+ </pre>
1279
+ */
1280
+ EStore.prototype.observeActive = function () {
1281
+ return this.notifyActive.asObservable();
1282
+ };
1283
+ Object.defineProperty(EStore.prototype, "loading", {
1284
+ /**
1285
+ * @return A snapshot of the loading state.
1286
+ */
1287
+ get: function () {
1288
+ return this._loading;
1289
+ },
1290
+ /**
1291
+ * Sets the current loading state and notifies observers.
1292
+ */
1293
+ set: function (loading) {
1294
+ this._loading = loading;
1295
+ this.notifyLoading.next(this._loading);
1296
+ },
1297
+ enumerable: false,
1298
+ configurable: true
1299
+ });
1300
+ /**
1301
+ * Observe loading.
1302
+ * @example
1303
+ <pre>
1304
+ let loading$ = source.observeLoading();
1305
+ </pre>
1306
+
1307
+ Note that this obverable piped through
1308
+ `takeWhile(v->v, true), such that it will
1309
+ complete after each emission.
1310
+
1311
+ See:
1312
+ https://medium.com/@ole.ersoy/waiting-on-estore-to-load-8dcbe161613c
1313
+
1314
+ For more details.
1315
+ */
1316
+ EStore.prototype.observeLoading = function () {
1317
+ return this.notifyLoading.asObservable().
1318
+ pipe(operators.takeWhile(function (v) { return v; }, true));
1319
+ };
1320
+ /**
1321
+ * Notfiies when loading has completed.
1322
+ */
1323
+ EStore.prototype.observeLoadingComplete = function () {
1324
+ return this.observeLoading().pipe(operators.filter(function (loading) { return loading == false; }), operators.switchMap(function () { return rxjs.of(true); }));
1325
+ };
1326
+ Object.defineProperty(EStore.prototype, "searching", {
1327
+ /**
1328
+ * @return A snapshot of the searching state.
1329
+ */
1330
+ get: function () {
1331
+ return this._searching;
1332
+ },
1333
+ /**
1334
+ * Sets the current searching state and notifies observers.
1335
+ */
1336
+ set: function (searching) {
1337
+ this._searching = searching;
1338
+ this.notifySearching.next(this._searching);
1339
+ },
1340
+ enumerable: false,
1341
+ configurable: true
1342
+ });
1343
+ /**
1344
+ * Observe searching.
1345
+ * @example
1346
+ <pre>
1347
+ let searching$ = source.observeSearching();
1348
+ </pre>
1349
+
1350
+ Note that this obverable piped through
1351
+ `takeWhile(v->v, true), such that it will
1352
+ complete after each emission.
1353
+
1354
+ See:
1355
+ https://medium.com/@ole.ersoy/waiting-on-estore-to-load-8dcbe161613c
1356
+
1357
+ For more details.
1358
+ */
1359
+ EStore.prototype.observeSearching = function () {
1360
+ return this.notifySearching.asObservable().
1361
+ pipe(operators.takeWhile(function (v) { return v; }, true));
1362
+ };
1363
+ /**
1364
+ * Notfiies when searching has completed.
1365
+ */
1366
+ EStore.prototype.observeSearchingComplete = function () {
1367
+ return this.observeSearching().pipe(operators.filter(function (searching) { return searching == false; }), operators.switchMap(function () { return rxjs.of(true); }));
1368
+ };
1369
+ /**
1370
+ * Adds a slice to the store and keys it by the slices label.
1371
+ *
1372
+ * @param p
1373
+ * @param label
1374
+ *
1375
+ * @example Setup a Todo Slice for COMPLETE Todos
1376
+ ```
1377
+ source.addSlice(todo => todo.complete, TodoSlices.COMPLETE);
1378
+ ```
1379
+ */
1380
+ EStore.prototype.addSlice = function (p, label) {
1381
+ var slice = new Slice(label, p, this);
1382
+ this.slices.set(slice.label, slice);
1383
+ };
1384
+ /**
1385
+ * Remove a slice
1386
+ * @param label The label identifying the slice
1387
+ *
1388
+ * @example Remove the TodoSlices.COMPLETE Slice
1389
+ ```
1390
+ source.removeSlice(TodoSlices.COMPLETE);
1391
+ ```
1392
+ */
1393
+ EStore.prototype.removeSlice = function (label) {
1394
+ this.slices.delete(label);
1395
+ };
1396
+ /**
1397
+ * Get a slice
1398
+ * @param label The label identifying the slice
1399
+ * @return The Slice instance or undefined
1400
+ *
1401
+ * @example Get the TodoSlices.COMPLETE slice
1402
+ ```
1403
+ source.getSlice(TodoSlices.COMPLETE);
1404
+ ```
1405
+ */
1406
+ EStore.prototype.getSlice = function (label) {
1407
+ return this.slices.get(label);
1408
+ };
1409
+ /**
1410
+ * Post (Add a new) element(s) to the store.
1411
+ * @param e An indiidual entity or an array of entities
1412
+ * @example Post a `todo`.
1413
+ ```
1414
+ store.post(todo);
1415
+ ```
1416
+ */
1417
+ EStore.prototype.post = function (e) {
1418
+ if (!Array.isArray(e)) {
1419
+ var guid = e[this.GUID_KEY]
1420
+ ? e[this.GUID_KEY]
1421
+ : GUID();
1422
+ e[this.GUID_KEY] = guid;
1423
+ this.entries.set(guid, e);
1424
+ this.updateIDEntry(e);
1425
+ Array.from(this.slices.values()).forEach(function (s) {
1426
+ s.post(e);
1427
+ });
1428
+ //Create a new array reference to trigger Angular change detection.
1429
+ var v = __spreadArray([], __read(Array.from(this.entries.values())));
1430
+ var delta = { type: "Post" /* POST */, entries: [e] };
1431
+ this.notifyAll(v, delta);
1432
+ }
1433
+ else {
1434
+ this.postA(e);
1435
+ }
1436
+ };
1437
+ /**
1438
+ * Post elements to the store.
1439
+ * @param ...e
1440
+ * @example Post two `Todo` instances.
1441
+ ```
1442
+ store.post(todo1, todo2);
1443
+ ```
1444
+ */
1445
+ EStore.prototype.postN = function () {
1446
+ var _this = this;
1447
+ var e = [];
1448
+ for (var _i = 0; _i < arguments.length; _i++) {
1449
+ e[_i] = arguments[_i];
1450
+ }
1451
+ e.forEach(function (e) {
1452
+ var guid = e[_this.GUID_KEY]
1453
+ ? e[_this.GUID_KEY]
1454
+ : GUID();
1455
+ e[_this.GUID_KEY] = guid;
1456
+ _this.entries.set(guid, e);
1457
+ _this.updateIDEntry(e);
1458
+ });
1459
+ Array.from(this.slices.values()).forEach(function (s) {
1460
+ s.postA(e);
1461
+ });
1462
+ //Create a new array reference to trigger Angular change detection.
1463
+ var v = __spreadArray([], __read(Array.from(this.entries.values())));
1464
+ var delta = { type: "Post" /* POST */, entries: e };
1465
+ this.notifyAll(v, delta);
1466
+ };
1467
+ /**
1468
+ * Post (Add) an array of elements to the store.
1469
+ * @param e
1470
+ * @example Post a `Todo` array.
1471
+ ```
1472
+ store.post([todo1, todo2]);
1473
+ ```
1474
+ */
1475
+ EStore.prototype.postA = function (e) {
1476
+ this.postN.apply(this, __spreadArray([], __read(e)));
1477
+ };
1478
+ /**
1479
+ * Put (Update) an element.
1480
+ * @param e
1481
+ * @example Put a Todo instance.
1482
+ ```
1483
+ store.put(todo1);
1484
+ ```
1485
+ */
1486
+ EStore.prototype.put = function (e) {
1487
+ if (!Array.isArray(e)) {
1488
+ var id = e[this.GUID_KEY];
1489
+ this.entries.set(id, e);
1490
+ this.updateIDEntry(e);
1491
+ var v = __spreadArray([], __read(Array.from(this.entries.values())));
1492
+ this.notify.next(v);
1493
+ var delta = { type: "Put" /* PUT */, entries: [e] };
1494
+ this.notifyDelta.next(delta);
1495
+ Array.from(this.slices.values()).forEach(function (s) {
1496
+ s.put(e);
1497
+ });
1498
+ }
1499
+ else {
1500
+ this.putA(e);
1501
+ }
1502
+ };
1503
+ /**
1504
+ * Put (Update) an element or add an element that was read from a persistence source
1505
+ * and thus already has an assigned global id`.
1506
+ * @param e
1507
+ * @example Put Todo instances.
1508
+ ```
1509
+ store.put(todo1, todo2);
1510
+ ```
1511
+ */
1512
+ EStore.prototype.putN = function () {
1513
+ var e = [];
1514
+ for (var _i = 0; _i < arguments.length; _i++) {
1515
+ e[_i] = arguments[_i];
1516
+ }
1517
+ this.putA(e);
1518
+ };
1519
+ /**
1520
+ * Put (Update) the array of elements.
1521
+ * @param e
1522
+ * @example Put Todo instances.
1523
+ ```
1524
+ store.put([todo1, todo2]);
1525
+ ```
1526
+ */
1527
+ EStore.prototype.putA = function (e) {
1528
+ var _this = this;
1529
+ e.forEach(function (e) {
1530
+ var guid = e[_this.GUID_KEY];
1531
+ _this.entries.set(guid, e);
1532
+ _this.updateIDEntry(e);
1533
+ });
1534
+ //Create a new array reference to trigger Angular change detection.
1535
+ var v = __spreadArray([], __read(Array.from(this.entries.values())));
1536
+ this.notify.next(v);
1537
+ var delta = { type: "Put" /* PUT */, entries: e };
1538
+ this.notifyDelta.next(delta);
1539
+ Array.from(this.slices.values()).forEach(function (s) {
1540
+ s.putA(e);
1541
+ });
1542
+ };
1543
+ /**
1544
+ * Delete (Update) the array of elements.
1545
+ * @param e
1546
+ * @example Delete todo1.
1547
+ ```
1548
+ store.delete(todo1]);
1549
+ ```
1550
+ */
1551
+ EStore.prototype.delete = function (e) {
1552
+ if (!Array.isArray(e)) {
1553
+ this.deleteActive(e);
1554
+ var guid_1 = e[this.GUID_KEY];
1555
+ this.entries.delete(guid_1);
1556
+ this.deleteIDEntry(e);
1557
+ Array.from(this.slices.values()).forEach(function (s) {
1558
+ s.entries.delete(guid_1);
1559
+ });
1560
+ //Create a new array reference to trigger Angular change detection.
1561
+ var v = __spreadArray([], __read(Array.from(this.entries.values())));
1562
+ var delta = { type: "Delete" /* DELETE */, entries: [e] };
1563
+ this.notifyAll(v, delta);
1564
+ Array.from(this.slices.values()).forEach(function (s) {
1565
+ s.delete(e);
1566
+ });
1567
+ }
1568
+ else {
1569
+ this.deleteA(e);
1570
+ }
1571
+ };
1572
+ /**
1573
+ * Delete N elements.
1574
+ * @param ...e
1575
+ * @example Put Todo instances.
1576
+ ```
1577
+ store.delete(todo1, todo2);
1578
+ ```
1579
+ */
1580
+ EStore.prototype.deleteN = function () {
1581
+ var e = [];
1582
+ for (var _i = 0; _i < arguments.length; _i++) {
1583
+ e[_i] = arguments[_i];
1584
+ }
1585
+ this.deleteA(e);
1586
+ };
1587
+ /**
1588
+ * Delete N elements.
1589
+ * @param ...e
1590
+ * @example Put Todo instances.
1591
+ ```
1592
+ store.delete(todo1, todo2);
1593
+ ```
1594
+ */
1595
+ EStore.prototype.deleteA = function (e) {
1596
+ var _this = this;
1597
+ e.forEach(function (e) {
1598
+ _this.deleteActive(e);
1599
+ var guid = e[_this.GUID_KEY];
1600
+ _this.entries.delete(guid);
1601
+ _this.deleteIDEntry(e);
1602
+ Array.from(_this.slices.values()).forEach(function (s) {
1603
+ s.entries.delete(guid);
1604
+ });
1605
+ });
1606
+ //Create a new array reference to trigger Angular change detection.
1607
+ var v = __spreadArray([], __read(Array.from(this.entries.values())));
1608
+ var delta = { type: "Delete" /* DELETE */, entries: e };
1609
+ this.notifyAll(v, delta);
1610
+ Array.from(this.slices.values()).forEach(function (s) {
1611
+ s.deleteA(e);
1612
+ });
1613
+ };
1614
+ /**
1615
+ * Delete elements by {@link Predicate}.
1616
+ * @param p The predicate.
1617
+ * @example Put Todo instances.
1618
+ ```
1619
+ store.delete(todo1, todo2);
1620
+ ```
1621
+ */
1622
+ EStore.prototype.deleteP = function (p) {
1623
+ var _this = this;
1624
+ var d = [];
1625
+ Array.from(this.entries.values()).forEach(function (e) {
1626
+ if (p(e)) {
1627
+ d.push(e);
1628
+ var id = e[_this.GUID_KEY];
1629
+ _this.entries.delete(id);
1630
+ _this.deleteActive(e);
1631
+ _this.deleteIDEntry(e);
1632
+ }
1633
+ });
1634
+ //Create a new array reference to trigger Angular change detection.
1635
+ var v = __spreadArray([], __read(Array.from(this.entries.values())));
1636
+ var delta = { type: "Delete" /* DELETE */, entries: d };
1637
+ this.notifyAll(v, delta);
1638
+ Array.from(this.slices.values()).forEach(function (s) {
1639
+ s.deleteA(d);
1640
+ });
1641
+ };
1642
+ /**
1643
+ * If the entity has the `id` key initialized with a value,
1644
+ * then also add the entity to the `idEntries`.
1645
+ *
1646
+ * @param e The element to be added to the `idEntries`.
1647
+ */
1648
+ EStore.prototype.updateIDEntry = function (e) {
1649
+ if (e[this.ID_KEY]) {
1650
+ this.idEntries.set(e[this.ID_KEY], e);
1651
+ }
1652
+ };
1653
+ /**
1654
+ * If the entity has the `id` key initialized with a value,
1655
+ * then also delete the entity to the `idEntries`.
1656
+ *
1657
+ * @param e The element to be added to the `idEntries`.
1658
+ */
1659
+ EStore.prototype.deleteIDEntry = function (e) {
1660
+ if (e[this.ID_KEY]) {
1661
+ this.idEntries.delete(e[this.ID_KEY]);
1662
+ }
1663
+ };
1664
+ /**
1665
+ * Resets the store and all contained slice instances to empty.
1666
+ * Also perform delta notification that sends all current store entries.
1667
+ * The ActionType.RESET code is sent with the delta notification. Slices
1668
+ * send their own delta notification.
1669
+ *
1670
+ * @example Reset the store.
1671
+ ```
1672
+ store.reset();
1673
+ ```
1674
+ */
1675
+ EStore.prototype.reset = function () {
1676
+ var delta = {
1677
+ type: "Reset" /* RESET */,
1678
+ entries: Array.from(this.entries.values())
1679
+ };
1680
+ this.notifyAll([], delta);
1681
+ this.entries = new Map();
1682
+ Array.from(this.slices.values()).forEach(function (s) {
1683
+ s.reset();
1684
+ });
1685
+ };
1686
+ /**
1687
+ * Call all the notifiers at once.
1688
+ *
1689
+ * @param v
1690
+ * @param delta
1691
+ */
1692
+ EStore.prototype.notifyAll = function (v, delta) {
1693
+ _super.prototype.notifyAll.call(this, v, delta);
1694
+ this.notifyLoading.next(this.loading);
1695
+ };
1696
+ return EStore;
1697
+ }(AbstractStore));
1698
+
1699
+ var OStore = /** @class */ (function () {
1700
+ function OStore(start) {
1701
+ var _this = this;
1702
+ /**
1703
+ * Map of Key Value pair entries
1704
+ * containing values store in this store.
1705
+ */
1706
+ this.entries = new Map();
1707
+ /**
1708
+ * Map of replay subject id to `ReplaySubject` instance.
1709
+ */
1710
+ this.subjects = new Map();
1711
+ if (start) {
1712
+ this.S = start;
1713
+ var keys = Object.keys(start);
1714
+ keys.forEach(function (k) {
1715
+ var ovr = start[k];
1716
+ _this.post(ovr, ovr.value);
1717
+ ovr.obs = _this.observe(ovr);
1718
+ });
1719
+ }
1720
+ }
1721
+ /**
1722
+ * Reset the state of the OStore to the
1723
+ * values or reset provided in the constructor
1724
+ * {@link OStoreStart} instance.
1725
+ */
1726
+ OStore.prototype.reset = function () {
1727
+ var _this = this;
1728
+ if (this.S) {
1729
+ var keys = Object.keys(this.S);
1730
+ keys.forEach(function (k) {
1731
+ var ovr = _this.S[k];
1732
+ _this.put(ovr, ovr.reset ? ovr.reset : ovr.value);
1733
+ });
1734
+ }
1735
+ };
1736
+ /**
1737
+ * Clear all entries
1738
+ */
1739
+ OStore.prototype.clear = function () {
1740
+ this.entries.clear();
1741
+ };
1742
+ /**
1743
+ * Set create a key value pair entry and creates a
1744
+ * corresponding replay subject instance that will
1745
+ * be used to broadcast updates.
1746
+ *
1747
+ * @param key The key identifying the value
1748
+ * @param value The value
1749
+ */
1750
+ OStore.prototype.post = function (key, value) {
1751
+ this.entries.set(key, value);
1752
+ this.subjects.set(key, new rxjs.ReplaySubject(1));
1753
+ //Emit immediately so that Observers can receive
1754
+ //the value straight away.
1755
+ var subject = this.subjects.get(key);
1756
+ if (subject) {
1757
+ subject.next(value);
1758
+ }
1759
+ };
1760
+ /**
1761
+ * Update a value and notify subscribers.
1762
+ *
1763
+ * @param key
1764
+ * @param value
1765
+ */
1766
+ OStore.prototype.put = function (key, value) {
1767
+ this.entries.set(key, value);
1768
+ var subject = this.subjects.get(key);
1769
+ if (subject) {
1770
+ subject.next(value);
1771
+ }
1772
+ };
1773
+ /**
1774
+ * Deletes both the value entry and the corresponding {@link ReplaySubject}.
1775
+ * Will unsubscribe the {@link ReplaySubject} prior to deleting it,
1776
+ * severing communication with corresponding {@link Observable}s.
1777
+ *
1778
+ * @param key
1779
+ */
1780
+ OStore.prototype.delete = function (key) {
1781
+ this.entries.delete(key);
1782
+ this.subjects.delete(key);
1783
+ var subject = this.subjects.get(key);
1784
+ if (subject) {
1785
+ subject.unsubscribe();
1786
+ }
1787
+ };
1788
+ /**
1789
+ * Observe changes to the values.
1790
+ *
1791
+ * @param key
1792
+ * @return An {@link Observable} of the value
1793
+ */
1794
+ OStore.prototype.observe = function (key) {
1795
+ return this.subjects.get(key).asObservable();
1796
+ };
1797
+ /**
1798
+ * Check whether a value exists.
1799
+ *
1800
+ * @param key
1801
+ * @return True if the entry exists ( Is not null or undefined ) and false otherwise.
1802
+ */
1803
+ OStore.prototype.exists = function (key) {
1804
+ return this.entries.get(key) != null;
1805
+ };
1806
+ /**
1807
+ * Retrieve a snapshot of the
1808
+ * value.
1809
+ *
1810
+ * @param key
1811
+ * @return A snapshot of the value corresponding to the key.
1812
+ */
1813
+ OStore.prototype.snapshot = function (key) {
1814
+ return this.entries.get(key);
1815
+ };
1816
+ /**
1817
+ * Indicates whether the store is empty.
1818
+ * @return true if the store is empty, false otherwise.
1819
+ */
1820
+ OStore.prototype.isEmpty = function () {
1821
+ return Array.from(this.entries.values()).length == 0;
1822
+ };
1823
+ /**
1824
+ * Returns the number of key value pairs contained.
1825
+ *
1826
+ * @return the number of entries in the store.
1827
+ */
1828
+ OStore.prototype.count = function () {
1829
+ return Array.from(this.entries.values()).length;
1830
+ };
1831
+ return OStore;
1832
+ }());
1833
+
1834
+ /*
1835
+ * Public API Surface of slice
1836
+ */
1837
+
1838
+ /**
1839
+ * Generated bundle index. Do not edit.
1840
+ */
1841
+
1842
+ exports.AbstractStore = AbstractStore;
1843
+ exports.ESTORE_CONFIG_DEFAULT = ESTORE_CONFIG_DEFAULT;
1844
+ exports.EStore = EStore;
1845
+ exports.GUID = GUID;
1846
+ exports.OStore = OStore;
1847
+ exports.Slice = Slice;
1848
+ exports.attachGUID = attachGUID;
1849
+ exports.attachGUIDs = attachGUIDs;
1850
+ exports.deepCopy = deepCopy;
1851
+ exports.distinct = distinct;
1852
+ exports.excludeKeys = excludeKeys;
1853
+ exports.getActiveValue = getActiveValue;
1854
+ exports.mapEntity = mapEntity;
1855
+ exports.scrollingUp = scrollingUp;
1856
+ exports.search = search;
1857
+ exports.shallowCopy = shallowCopy;
1858
+ exports.unique = unique;
1859
+
1860
+ Object.defineProperty(exports, '__esModule', { value: true });
1861
+
1862
+ })));
1863
+ //# sourceMappingURL=fireflysemantics-slice.umd.js.map