@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,403 @@
1
+ import { AbstractStore } from './AbstractStore';
2
+ import { StoreConfig } from './models/StoreConfig';
3
+ import { Predicate, Delta } from './models/';
4
+ import { Observable } from 'rxjs';
5
+ import { Slice } from './Slice';
6
+ /**
7
+ * This `todoFactory` code will be used to illustrate the API examples. The following
8
+ * utilities are used in the tests and the API Typedoc examples contained here.
9
+ * @example Utilities for API Examples
10
+ ```
11
+ export const enum TodoSliceEnum {
12
+ COMPLETE = "Complete",
13
+ INCOMPLETE = "Incomplete"
14
+ }
15
+
16
+ export class Todo {
17
+ constructor(public complete: boolean, public title: string,public gid?:string, public id?:string) {}
18
+ }
19
+
20
+ export let todos = [new Todo(false, "You complete me!"), new Todo(true, "You completed me!")];
21
+
22
+ export function todosFactory():Todo[] {
23
+ return [new Todo(false, "You complete me!"), new Todo(true, "You completed me!")];
24
+ }
25
+ ```
26
+ */
27
+ export declare class EStore<E> extends AbstractStore<E> {
28
+ /**
29
+ * Store constructor (Initialization with element is optional)
30
+ *
31
+ * perform initial notification to all observers,
32
+ * such that function like {@link combineLatest}{}
33
+ * will execute at least once.
34
+ * @param entities
35
+ * @example Dynamic `EStore<Todo>` Creation
36
+ ```
37
+ // Initialize the Store
38
+ let store: EStore<Todo> = new EStore<Todo>(todosFactory());
39
+ ```*/
40
+ constructor(entities?: E[], config?: StoreConfig);
41
+ /**
42
+ * Calls complete on all {@link BehaviorSubject} instances.
43
+ *
44
+ * Call destroy when disposing of the store.
45
+ */
46
+ destroy(): void;
47
+ /**
48
+ * Toggles the entity:
49
+ *
50
+ * If the store contains the entity
51
+ * it will be deleted. If the store
52
+ * does not contains the entity,
53
+ * it is added.
54
+ * @param e
55
+ * @example Toggle the `Todo` instance
56
+ ```
57
+ estore.post(todo);
58
+ // Remove todo
59
+ estore.toggle(todo);
60
+ // Add it back
61
+ estore.toggle(todo);
62
+
63
+ ```
64
+ */
65
+ toggle(e: E): void;
66
+ /**
67
+ * An Observable<E[]> reference so that
68
+ *
69
+ */
70
+ observable: Observable<E[]>;
71
+ /**
72
+ * Notifies observers when the store is empty.
73
+ */
74
+ private notifyActive;
75
+ /**
76
+ * `Map` of active entties. The instance is public and can be used
77
+ * directly to add and remove active entities, however we recommend
78
+ * using the {@link addActive} and {@link deleteActive} methods.
79
+ */
80
+ active: Map<string, E>;
81
+ /**
82
+ * Add multiple entity entities to active.
83
+ *
84
+ * If the entity is not contained in the store it is added
85
+ * to the store before it is added to `active`.
86
+ *
87
+ * Also we clone the map prior to broadcasting it with
88
+ * `notifyActive` to make sure we will trigger Angular
89
+ * change detection in the event that it maintains
90
+ * a reference to the `active` state `Map` instance.
91
+ *
92
+ * @example Add a `todo1` and `todo2` as active
93
+ ```
94
+ addActive(todo1);
95
+ addActive(todo2);
96
+ ```
97
+ */
98
+ addActive(e: E): void;
99
+ /**
100
+ * Delete an entity as active.
101
+ *
102
+ * Also we clone the map prior to broadcasting it with
103
+ * `notifyActive` to make sure we will trigger Angular
104
+ * change detection in the event that it maintains
105
+ * a reference to the `active` state `Map` instance.
106
+ *
107
+ * @example Mark a `todo` instance as active
108
+ ```
109
+ deleteActive(todo1);
110
+ deleteActive(todo2);
111
+ ```
112
+ */
113
+ deleteActive(e: E): void;
114
+ /**
115
+ * Clear / reset the active entity map.
116
+ *
117
+ * Also we clone the map prior to broadcasting it with
118
+ * `notifyActive` to make sure we will trigger Angular
119
+ * change detection in the event that it maintains
120
+ * a reference to the `active` state `Map` instance.
121
+ *
122
+ * @example Mark a `todo` instance as active
123
+ ```
124
+ deleteActive(todo1);
125
+ deleteActive(todo2);
126
+ ```
127
+ */
128
+ clearActive(): void;
129
+ /**
130
+ * Observe the active entity.
131
+ * @example
132
+ <pre>
133
+ let active$ = source.observeActive();
134
+ </pre>
135
+ */
136
+ observeActive(): Observable<Map<string, E>>;
137
+ /**
138
+ * Observable of errors occurred during a load request.
139
+ *
140
+ * The error Observable should be created by the
141
+ * client.
142
+ */
143
+ loadingError: Observable<any>;
144
+ /**
145
+ * Notifies observers when the store is loading.
146
+ *
147
+ * This is a common pattern found when implementing
148
+ * `Observable` data sources.
149
+ */
150
+ private notifyLoading;
151
+ /**
152
+ * The current loading state. Use loading when fetching new
153
+ * data for the store. The default loading state is `true`.
154
+ *
155
+ * This is such that if data is fetched asynchronously
156
+ * in a service, components can wait on loading notification
157
+ * before attempting to retrieve data from the service.
158
+ *
159
+ * Loading could be based on a composite response. For example
160
+ * when the stock and mutual funds have loaded, set loading to `false`.
161
+ */
162
+ private _loading;
163
+ /**
164
+ * Sets the current loading state and notifies observers.
165
+ */
166
+ set loading(loading: boolean);
167
+ /**
168
+ * @return A snapshot of the loading state.
169
+ */
170
+ get loading(): boolean;
171
+ /**
172
+ * Observe loading.
173
+ * @example
174
+ <pre>
175
+ let loading$ = source.observeLoading();
176
+ </pre>
177
+
178
+ Note that this obverable piped through
179
+ `takeWhile(v->v, true), such that it will
180
+ complete after each emission.
181
+
182
+ See:
183
+ https://medium.com/@ole.ersoy/waiting-on-estore-to-load-8dcbe161613c
184
+
185
+ For more details.
186
+ */
187
+ observeLoading(): Observable<boolean>;
188
+ /**
189
+ * Notfiies when loading has completed.
190
+ */
191
+ observeLoadingComplete(): Observable<boolean>;
192
+ /**
193
+ * Observable of errors occurred during a search request.
194
+ *
195
+ * The error Observable should be created by the
196
+ * client.
197
+ */
198
+ searchError: Observable<any>;
199
+ /**
200
+ * Notifies observers that a search is in progress.
201
+ *
202
+ * This is a common pattern found when implementing
203
+ * `Observable` data sources.
204
+ */
205
+ private notifySearching;
206
+ /**
207
+ * The current `searching` state. Use `searching`
208
+ * for example to display a spinnner
209
+ * when performing a search.
210
+ * The default `searching` state is `false`.
211
+ */
212
+ private _searching;
213
+ /**
214
+ * Sets the current searching state and notifies observers.
215
+ */
216
+ set searching(searching: boolean);
217
+ /**
218
+ * @return A snapshot of the searching state.
219
+ */
220
+ get searching(): boolean;
221
+ /**
222
+ * Observe searching.
223
+ * @example
224
+ <pre>
225
+ let searching$ = source.observeSearching();
226
+ </pre>
227
+
228
+ Note that this obverable piped through
229
+ `takeWhile(v->v, true), such that it will
230
+ complete after each emission.
231
+
232
+ See:
233
+ https://medium.com/@ole.ersoy/waiting-on-estore-to-load-8dcbe161613c
234
+
235
+ For more details.
236
+ */
237
+ observeSearching(): Observable<boolean>;
238
+ /**
239
+ * Notfiies when searching has completed.
240
+ */
241
+ observeSearchingComplete(): Observable<boolean>;
242
+ /**
243
+ * Store slices
244
+ */
245
+ private slices;
246
+ /**
247
+ * Adds a slice to the store and keys it by the slices label.
248
+ *
249
+ * @param p
250
+ * @param label
251
+ *
252
+ * @example Setup a Todo Slice for COMPLETE Todos
253
+ ```
254
+ source.addSlice(todo => todo.complete, TodoSlices.COMPLETE);
255
+ ```
256
+ */
257
+ addSlice(p: Predicate<E>, label: string): void;
258
+ /**
259
+ * Remove a slice
260
+ * @param label The label identifying the slice
261
+ *
262
+ * @example Remove the TodoSlices.COMPLETE Slice
263
+ ```
264
+ source.removeSlice(TodoSlices.COMPLETE);
265
+ ```
266
+ */
267
+ removeSlice(label: string): void;
268
+ /**
269
+ * Get a slice
270
+ * @param label The label identifying the slice
271
+ * @return The Slice instance or undefined
272
+ *
273
+ * @example Get the TodoSlices.COMPLETE slice
274
+ ```
275
+ source.getSlice(TodoSlices.COMPLETE);
276
+ ```
277
+ */
278
+ getSlice(label: string): Slice<E> | undefined;
279
+ /**
280
+ * Post (Add a new) element(s) to the store.
281
+ * @param e An indiidual entity or an array of entities
282
+ * @example Post a `todo`.
283
+ ```
284
+ store.post(todo);
285
+ ```
286
+ */
287
+ post(e: E | E[]): void;
288
+ /**
289
+ * Post elements to the store.
290
+ * @param ...e
291
+ * @example Post two `Todo` instances.
292
+ ```
293
+ store.post(todo1, todo2);
294
+ ```
295
+ */
296
+ postN(...e: E[]): void;
297
+ /**
298
+ * Post (Add) an array of elements to the store.
299
+ * @param e
300
+ * @example Post a `Todo` array.
301
+ ```
302
+ store.post([todo1, todo2]);
303
+ ```
304
+ */
305
+ postA(e: E[]): void;
306
+ /**
307
+ * Put (Update) an element.
308
+ * @param e
309
+ * @example Put a Todo instance.
310
+ ```
311
+ store.put(todo1);
312
+ ```
313
+ */
314
+ put(e: E | E[]): void;
315
+ /**
316
+ * Put (Update) an element or add an element that was read from a persistence source
317
+ * and thus already has an assigned global id`.
318
+ * @param e
319
+ * @example Put Todo instances.
320
+ ```
321
+ store.put(todo1, todo2);
322
+ ```
323
+ */
324
+ putN(...e: E[]): void;
325
+ /**
326
+ * Put (Update) the array of elements.
327
+ * @param e
328
+ * @example Put Todo instances.
329
+ ```
330
+ store.put([todo1, todo2]);
331
+ ```
332
+ */
333
+ putA(e: E[]): void;
334
+ /**
335
+ * Delete (Update) the array of elements.
336
+ * @param e
337
+ * @example Delete todo1.
338
+ ```
339
+ store.delete(todo1]);
340
+ ```
341
+ */
342
+ delete(e: E | E[]): void;
343
+ /**
344
+ * Delete N elements.
345
+ * @param ...e
346
+ * @example Put Todo instances.
347
+ ```
348
+ store.delete(todo1, todo2);
349
+ ```
350
+ */
351
+ deleteN(...e: E[]): void;
352
+ /**
353
+ * Delete N elements.
354
+ * @param ...e
355
+ * @example Put Todo instances.
356
+ ```
357
+ store.delete(todo1, todo2);
358
+ ```
359
+ */
360
+ deleteA(e: E[]): void;
361
+ /**
362
+ * Delete elements by {@link Predicate}.
363
+ * @param p The predicate.
364
+ * @example Put Todo instances.
365
+ ```
366
+ store.delete(todo1, todo2);
367
+ ```
368
+ */
369
+ deleteP(p: Predicate<E>): void;
370
+ /**
371
+ * If the entity has the `id` key initialized with a value,
372
+ * then also add the entity to the `idEntries`.
373
+ *
374
+ * @param e The element to be added to the `idEntries`.
375
+ */
376
+ private updateIDEntry;
377
+ /**
378
+ * If the entity has the `id` key initialized with a value,
379
+ * then also delete the entity to the `idEntries`.
380
+ *
381
+ * @param e The element to be added to the `idEntries`.
382
+ */
383
+ private deleteIDEntry;
384
+ /**
385
+ * Resets the store and all contained slice instances to empty.
386
+ * Also perform delta notification that sends all current store entries.
387
+ * The ActionType.RESET code is sent with the delta notification. Slices
388
+ * send their own delta notification.
389
+ *
390
+ * @example Reset the store.
391
+ ```
392
+ store.reset();
393
+ ```
394
+ */
395
+ reset(): void;
396
+ /**
397
+ * Call all the notifiers at once.
398
+ *
399
+ * @param v
400
+ * @param delta
401
+ */
402
+ protected notifyAll(v: E[], delta: Delta<E>): void;
403
+ }
@@ -0,0 +1,106 @@
1
+ import { Observable } from 'rxjs';
2
+ /**
3
+ * Initialize hte store with this.
4
+ */
5
+ export interface ValueReset {
6
+ value: any;
7
+ reset?: any;
8
+ }
9
+ /**
10
+ * OStore Key Value Reset
11
+ */
12
+ export interface ObsValueReset {
13
+ value: any;
14
+ reset?: any;
15
+ obs: Observable<any>;
16
+ }
17
+ export interface KeyObsValueReset {
18
+ [key: string]: ObsValueReset;
19
+ }
20
+ export interface OStoreStart {
21
+ [key: string]: ValueReset;
22
+ }
23
+ export declare class OStore<E extends KeyObsValueReset> {
24
+ /**
25
+ * Start keys and values
26
+ * passed in via constructor.
27
+ */
28
+ S: E;
29
+ constructor(start: OStoreStart);
30
+ /**
31
+ * Reset the state of the OStore to the
32
+ * values or reset provided in the constructor
33
+ * {@link OStoreStart} instance.
34
+ */
35
+ reset(): void;
36
+ /**
37
+ * Clear all entries
38
+ */
39
+ clear(): void;
40
+ /**
41
+ * Map of Key Value pair entries
42
+ * containing values store in this store.
43
+ */
44
+ entries: Map<any, any>;
45
+ /**
46
+ * Map of replay subject id to `ReplaySubject` instance.
47
+ */
48
+ private subjects;
49
+ /**
50
+ * Set create a key value pair entry and creates a
51
+ * corresponding replay subject instance that will
52
+ * be used to broadcast updates.
53
+ *
54
+ * @param key The key identifying the value
55
+ * @param value The value
56
+ */
57
+ post(key: any, value: any): void;
58
+ /**
59
+ * Update a value and notify subscribers.
60
+ *
61
+ * @param key
62
+ * @param value
63
+ */
64
+ put(key: any, value: any): void;
65
+ /**
66
+ * Deletes both the value entry and the corresponding {@link ReplaySubject}.
67
+ * Will unsubscribe the {@link ReplaySubject} prior to deleting it,
68
+ * severing communication with corresponding {@link Observable}s.
69
+ *
70
+ * @param key
71
+ */
72
+ delete(key: any): void;
73
+ /**
74
+ * Observe changes to the values.
75
+ *
76
+ * @param key
77
+ * @return An {@link Observable} of the value
78
+ */
79
+ observe(key: any): Observable<any>;
80
+ /**
81
+ * Check whether a value exists.
82
+ *
83
+ * @param key
84
+ * @return True if the entry exists ( Is not null or undefined ) and false otherwise.
85
+ */
86
+ exists(key: any): boolean;
87
+ /**
88
+ * Retrieve a snapshot of the
89
+ * value.
90
+ *
91
+ * @param key
92
+ * @return A snapshot of the value corresponding to the key.
93
+ */
94
+ snapshot(key: any): any;
95
+ /**
96
+ * Indicates whether the store is empty.
97
+ * @return true if the store is empty, false otherwise.
98
+ */
99
+ isEmpty(): boolean;
100
+ /**
101
+ * Returns the number of key value pairs contained.
102
+ *
103
+ * @return the number of entries in the store.
104
+ */
105
+ count(): number;
106
+ }
package/lib/Slice.d.ts ADDED
@@ -0,0 +1,97 @@
1
+ import { Predicate } from "./models";
2
+ import { AbstractStore } from "./AbstractStore";
3
+ import { EStore } from "./EStore";
4
+ export declare class Slice<E> extends AbstractStore<E> {
5
+ label: string;
6
+ predicate: Predicate<E>;
7
+ eStore: EStore<E>;
8
+ entries: Map<string, E>;
9
+ /**
10
+ * perform initial notification to all observers,
11
+ * such that operations like {@link combineLatest}{}
12
+ * will execute at least once.
13
+ *
14
+ * @param label The slice label
15
+ * @param predicate The slice predicate
16
+ * @param eStore The EStore instance containing the elements considered for slicing
17
+ *
18
+ * @example
19
+ <pre>
20
+ //Empty slice
21
+ new Slice<Todo>(Todo.COMPLETE, todo=>!todo.complete);
22
+
23
+ //Initialized slice
24
+ let todos = [new Todo(false, "You complete me!"),
25
+ new Todo(true, "You completed me!")];
26
+ new Slice<Todo>(Todo.COMPLETE, todo=>!todo.complete, todos);
27
+ </pre>
28
+ */
29
+ constructor(label: string, predicate: Predicate<E>, eStore: EStore<E>);
30
+ /**
31
+ * Add the element if it satisfies the predicate
32
+ * and notify subscribers that an element was added.
33
+ *
34
+ * @param e The element to be considered for slicing
35
+ */
36
+ post(e: E | E[]): void;
37
+ /**
38
+ * Add the elements if they satisfy the predicate
39
+ * and notify subscribers that elements were added.
40
+ *
41
+ * @param e The element to be considered for slicing
42
+ */
43
+ postN(...e: E[]): void;
44
+ /**
45
+ * Add the elements if they satisfy the predicate
46
+ * and notify subscribers that elements were added.
47
+ *
48
+ * @param e The element to be considered for slicing
49
+ */
50
+ postA(e: E[]): void;
51
+ /**
52
+ * Delete an element from the slice.
53
+ *
54
+ * @param e The element to be deleted if it satisfies the predicate
55
+ */
56
+ delete(e: E | E[]): void;
57
+ /**
58
+ * @param e The elements to be deleted if it satisfies the predicate
59
+ */
60
+ deleteN(...e: E[]): void;
61
+ /**
62
+ * @param e The elements to be deleted if they satisfy the predicate
63
+ */
64
+ deleteA(e: E[]): void;
65
+ /**
66
+ * Update the slice when an Entity instance mutates.
67
+ *
68
+ * @param e The element to be added or deleted depending on predicate reevaluation
69
+ */
70
+ put(e: E | E[]): void;
71
+ /**
72
+ * Update the slice with mutated Entity instances.
73
+ *
74
+ * @param e The elements to be deleted if it satisfies the predicate
75
+ */
76
+ putN(...e: E[]): void;
77
+ /**
78
+ * @param e The elements to be put
79
+ */
80
+ putA(e: E[]): void;
81
+ /**
82
+ * Resets the slice to empty.
83
+ */
84
+ reset(): void;
85
+ /**
86
+ * Utility method that applies the predicate to an array
87
+ * of entities and return the ones that pass the test.
88
+ *
89
+ * Used to create an initial set of values
90
+ * that should be part of the `Slice`.
91
+ *
92
+ * @param p
93
+ * @param e
94
+ * @return The the array of entities that pass the predicate test.
95
+ */
96
+ test(p: Predicate<E>, e: E[]): E[];
97
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The action types for the store.
3
+ */
4
+ export declare const enum ActionTypes {
5
+ POST = "Post",
6
+ PUT = "Put",
7
+ DELETE = "Delete",
8
+ INTIALIZE = "Initialize",
9
+ RESET = "Reset"
10
+ }
@@ -0,0 +1,10 @@
1
+ import { ActionTypes } from "./ActionTypes";
2
+ /**
3
+ * Delta update interface models
4
+ * the type of the update and the entities
5
+ * associated with the update.
6
+ */
7
+ export interface Delta<E> {
8
+ type: ActionTypes;
9
+ entries: E[];
10
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Function type for predicate operations
3
+ */
4
+ export declare type Predicate<E> = (e: E) => boolean;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The configuration interface for the entity store
3
+ * defines the strings for the ID Key and Global ID key.
4
+ */
5
+ export interface StoreConfig {
6
+ idKey: string;
7
+ guidKey: string;
8
+ }
@@ -0,0 +1,4 @@
1
+ export * from './ActionTypes';
2
+ export * from './Delta';
3
+ export * from './Predicate';
4
+ export * from './StoreConfig';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Scroll position function type used to auto hide
3
+ * the material toolbar in conjuction with Angular CDK.
4
+ */
5
+ export declare type scrollPosition = () => [number, number];