insite-subscriptions-client 2.0.0 → 2.0.2

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 (35) hide show
  1. package/dist/Subscription.d.ts +18 -0
  2. package/dist/Subscription.d.ts.map +1 -0
  3. package/dist/SubscriptionArray.d.ts +73 -0
  4. package/dist/SubscriptionArray.d.ts.map +1 -0
  5. package/dist/SubscriptionGroup/SubscriptionGroup.d.ts +37 -0
  6. package/dist/SubscriptionGroup/SubscriptionGroup.d.ts.map +1 -0
  7. package/dist/SubscriptionGroup/SubscriptionGroupItem.d.ts +31 -0
  8. package/dist/SubscriptionGroup/SubscriptionGroupItem.d.ts.map +1 -0
  9. package/dist/SubscriptionGroup/common.d.ts +12 -0
  10. package/dist/SubscriptionGroup/common.d.ts.map +1 -0
  11. package/dist/SubscriptionGroup/index.d.ts +3 -0
  12. package/dist/SubscriptionGroup/index.d.ts.map +1 -0
  13. package/dist/SubscriptionGroup/types.d.ts +41 -0
  14. package/dist/SubscriptionGroup/types.d.ts.map +1 -0
  15. package/dist/SubscriptionMap.d.ts +102 -0
  16. package/dist/SubscriptionMap.d.ts.map +1 -0
  17. package/dist/SubscriptionObject.d.ts +47 -0
  18. package/dist/SubscriptionObject.d.ts.map +1 -0
  19. package/{src/index.ts → dist/index.d.ts} +2 -14
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +2 -0
  22. package/dist/index.js.map +7 -0
  23. package/dist/symbols.d.ts +13 -0
  24. package/dist/symbols.d.ts.map +1 -0
  25. package/package.json +2 -2
  26. package/src/Subscription.ts +0 -101
  27. package/src/SubscriptionArray.ts +0 -275
  28. package/src/SubscriptionGroup/SubscriptionGroup.ts +0 -319
  29. package/src/SubscriptionGroup/SubscriptionGroupItem.ts +0 -391
  30. package/src/SubscriptionGroup/common.ts +0 -13
  31. package/src/SubscriptionGroup/index.ts +0 -8
  32. package/src/SubscriptionGroup/types.ts +0 -121
  33. package/src/SubscriptionMap.ts +0 -455
  34. package/src/SubscriptionObject.ts +0 -168
  35. package/src/symbols.ts +0 -12
@@ -1,8 +0,0 @@
1
- export * from "./SubscriptionGroup";
2
- export {
3
- Definition as SubscriptionGroupDefinition,
4
- ParseValues as ParseSubscriptionGroupValues,
5
- SubscriptionHandle as SubscriptionGroupSubscriptionHandle,
6
- SubscriptionType as SubscriptionGroupSubscriptionType,
7
- UnparsedDefinition as SubscriptionGroupUnparsedDefinition
8
- } from "./types";
@@ -1,121 +0,0 @@
1
- import type {
2
- SubscriptionArrayUpdated,
3
- Updates as SubscriptionArrayUpdates,
4
- SubscriptionArrayWithSubscription
5
- } from "../SubscriptionArray";
6
- import type {
7
- SubscriptionMapUpdated,
8
- Updates as SubscriptionMapUpdates,
9
- SubscriptionMapWithSubscription
10
- } from "../SubscriptionMap";
11
- import type {
12
- Updated as SubscriptionObjectUpdated,
13
- Updates as SubscriptionObjectUpdates,
14
- SubscriptionObjectWithSubscription
15
- } from "../SubscriptionObject";
16
- import type { SubscriptionGroup } from "./SubscriptionGroup";
17
-
18
-
19
- /* eslint-disable @typescript-eslint/no-explicit-any */
20
-
21
-
22
- export type SubscriptionType = "array" | "map" | "object";
23
-
24
- export type SubscriptionUpdated<T> =
25
- T extends "array" ?
26
- SubscriptionArrayUpdated<any> :
27
- T extends "map" ?
28
- SubscriptionMapUpdated<any> :
29
- T extends "object" ?
30
- SubscriptionObjectUpdated :
31
- never;
32
-
33
- export type SubscriptionValue<T> =
34
- T extends "array" ?
35
- SubscriptionArrayWithSubscription<any> :
36
- T extends "map" ?
37
- SubscriptionMapWithSubscription<any> :
38
- T extends "object" ?
39
- SubscriptionObjectWithSubscription :
40
- never;
41
-
42
- export type SubscriptionHandle<T> =
43
- T extends "array" ?
44
- (updated: null | SubscriptionArrayUpdated<any>, group: SubscriptionGroup) => void :
45
- T extends "map" ?
46
- (updated: null | SubscriptionMapUpdated<any>, group: SubscriptionGroup) => void :
47
- T extends "object" ?
48
- (updated: null | SubscriptionObjectUpdated, group: SubscriptionGroup) => void :
49
- never;
50
-
51
- export type SubscriptionOnBeforeInit<T> =
52
- T extends "array" ?
53
- (value: SubscriptionArrayWithSubscription<any>) => void :
54
- T extends "map" ?
55
- (value: SubscriptionMapWithSubscription<any>) => void :
56
- T extends "object" ?
57
- (value: SubscriptionObjectWithSubscription) => void :
58
- never;
59
-
60
- export type SubscriptionUpdates<T> =
61
- T extends "array" ?
62
- SubscriptionArrayUpdates :
63
- T extends "map" ?
64
- SubscriptionMapUpdates :
65
- T extends "object" ?
66
- SubscriptionObjectUpdates :
67
- never;
68
-
69
- export type Definition<T extends SubscriptionType = SubscriptionType> = {
70
- name: string;
71
- value?: SubscriptionValue<T>;
72
- type?: T;
73
- publicationName?: string;
74
- params?: unknown[];
75
- handle?: (...args: any[]) => void;
76
- onBeforeInit?: (...args: any[]) => void;
77
- debounce?: number;
78
- preventBind?: boolean;
79
- };
80
-
81
- export type TupleDefinition = unknown[];
82
-
83
- export type UnparsedDefinition<T extends SubscriptionType = SubscriptionType> = Definition<T> | TupleDefinition;
84
-
85
- type ResolveObjectValue<T> =
86
- T extends { value: infer V } ? V :
87
- T extends { type: "array" } ? SubscriptionArrayWithSubscription<any> :
88
- T extends { type: "map" } ? SubscriptionMapWithSubscription<any> :
89
- T extends { type: "object" } ? SubscriptionObjectWithSubscription :
90
- SubscriptionObjectWithSubscription;
91
-
92
- type ResolveArrayValue<T extends any[]> =
93
- T[1] extends object ? T[1] :
94
- T[1] extends "array" ? SubscriptionArrayWithSubscription<any> :
95
- T[1] extends "map" ? SubscriptionMapWithSubscription<any> :
96
- T[1] extends "object" ? SubscriptionObjectWithSubscription :
97
- SubscriptionObjectWithSubscription;
98
-
99
- type ResolveValue<U> =
100
- U extends object ?
101
- ResolveObjectValue<U> :
102
- U extends any[] ?
103
- ResolveArrayValue<U> :
104
- never;
105
-
106
- export type ParseValues<T extends Array<any[] | object>> =
107
- {
108
- [K in keyof T as T[K] extends { name: infer N } ?
109
- N extends string ?
110
- N :
111
- never :
112
- T[K] extends [infer F, any] ?
113
- F extends string ?
114
- F :
115
- never :
116
- never
117
- ]: ResolveValue<T[K]>;
118
- } &
119
- T extends Array<infer U> ?
120
- ResolveValue<U>[] :
121
- never;
@@ -1,455 +0,0 @@
1
- import { removeOne } from "@nesvet/n";
2
- import { Subscription } from "./Subscription";
3
- import {
4
- clearSymbol,
5
- getAsInitialUpdatedSymbol,
6
- getAsUpdatesSymbol,
7
- getHandleUpdateSymbol,
8
- getSubscriptionSymbol,
9
- renewSymbol,
10
- setHandleUpdateSymbol,
11
- subscribeSymbol,
12
- unsubscribeSymbol,
13
- updateSymbol
14
- } from "./symbols";
15
-
16
-
17
- type GenericItem = { _id: string } & Record<string, any>;// eslint-disable-line @typescript-eslint/no-explicit-any
18
-
19
- type Item = GenericItem | SubscriptionMapItem;
20
-
21
- type Updated<I extends Item = Item> = {
22
- deleted: string[];
23
- added: I[];
24
- } & I[];
25
-
26
- type SortList = Record<string, -1 | 1>;
27
-
28
- export type Updates<I extends Item = Item> =
29
- (
30
- [ "c"/* create */, item: I ] |
31
- [ "d"/* delete */, _id: string ] |
32
- [ "i"/* initial */, items: I[], sortList?: SortList ] |
33
- [ "u"/* update */, itemUpdates: { _id: string } & Partial<I>, isAtomicOrUpdatedFields: string[] | true ] |
34
- null
35
- )[] |
36
- null;
37
-
38
- type UpdateHandler<I extends Item = Item> = (map: SubscriptionMap<I>, updated: Updated<I>, updates: Updates<I>) => void;
39
-
40
- declare global {
41
- var __insite_subscription_map_sort_function: ((a: Item, b: Item) => -1 | 0 | 1) | undefined;// eslint-disable-line no-var
42
- }
43
-
44
- function createSortFunction(sortList: SortList) {
45
-
46
- const tempScript = document.createElement("script");
47
-
48
- tempScript.textContent = `window.__insite_subscription_map_sort_function = function sort(a, b) { return ${
49
- Object.entries(sortList)
50
- .map(([ key, value ]) => value > 0 ? `a.${key} > b.${key} ? 1 : a.${key} < b.${key} ? -1 :` : `a.${key} < b.${key} ? 1 : a.${key} > b.${key} ? -1 :`)
51
- .concat("0")
52
- .join(" ")
53
- };}`;
54
-
55
- document.head.append(tempScript);
56
-
57
- const sortFunction = window.__insite_subscription_map_sort_function!;
58
-
59
- tempScript.remove();
60
- delete window.__insite_subscription_map_sort_function;
61
-
62
- return sortFunction;
63
- }
64
-
65
- // function createSortFunction(sortList: SortList) {
66
- // return new Function(// eslint-disable-line no-new-func
67
- // "a"/* Item */,
68
- // "b"/* Item */,
69
- // `return ${
70
- // Object.entries(sortList)
71
- // .map(([ key, value ]) => value > 0 ? `a.${key} > b.${key} ? 1 : a.${key} < b.${key} ? -1 :` : `a.${key} < b.${key} ? 1 : a.${key} > b.${key} ? -1 :`)
72
- // .concat("0")
73
- // .join(" ")
74
- // }`
75
- // ) as (a: Item, b: Item) => -1 | 0 | 1;
76
- // }
77
-
78
-
79
- export class SubscriptionMap<I extends Item = Item> extends Map<string, I> {
80
- constructor(updates?: Updates<I>, onUpdate?: UpdateHandler<I>) {
81
- super();
82
-
83
- if (onUpdate)
84
- this.handleUpdate = onUpdate;
85
-
86
- if (updates)
87
- this.update(updates);
88
-
89
- }
90
-
91
- handleUpdate;
92
-
93
- private updates = new Map();
94
-
95
- sortList: null | SortList = null;
96
-
97
- sortCompareFunction: ((a: I, b: I) => number) | null = null;
98
-
99
- private fieldsToSort: string[] = [];
100
-
101
- sorted: I[] = [];
102
-
103
- sort() {
104
-
105
- if (this.sortCompareFunction)
106
- this.sorted.sort(this.sortCompareFunction);
107
-
108
- }
109
-
110
- [getHandleUpdateSymbol]() {
111
- return this.handleUpdate;
112
- }
113
-
114
- [setHandleUpdateSymbol](handleUpdate: UpdateHandler<I>) {
115
- return (this.handleUpdate = handleUpdate);
116
- }
117
-
118
- private TheItem!: { new (map: SubscriptionMap<I>, updates: GenericItem): I };
119
-
120
- set Item(TheItem: { new (map: SubscriptionMap<I>, updates: GenericItem): I }) {
121
- this.TheItem = TheItem;
122
-
123
- if (this.size)
124
- for (const [ _id, item ] of this)
125
- this.set(_id, new TheItem(this, item));
126
-
127
- }
128
-
129
- get Item() {
130
- return this.TheItem;
131
- }
132
-
133
- update(updates: Updates<I>) {
134
-
135
- const updated: Updated<I> = Object.assign([], {
136
- added: [],
137
- deleted: []
138
- });
139
-
140
- if (updates) {
141
- let shouldSort = false;
142
-
143
- const TheItem = this.TheItem;// eslint-disable-line prefer-destructuring
144
- const prevUpdates = this.updates;
145
-
146
- for (const update of updates)
147
- switch (update?.[0]) {
148
- case "i"/* initial */: {
149
- const [ , array, sortList ] = update;
150
-
151
- if (sortList) {
152
- this.sortList = sortList;
153
- this.sortCompareFunction = createSortFunction(sortList);
154
- this.fieldsToSort = [ ...new Set(Object.keys(sortList).map(key => key.replace(/\..+/, ""))) ];
155
- shouldSort = true;
156
- } else {
157
- this.sortList = null;
158
- this.sortCompareFunction = null;
159
- this.fieldsToSort = [];
160
- }
161
-
162
- if (this.size) {
163
- const updatesMap = new Map();
164
- for (const itemUpdates of array)
165
- updatesMap.set(itemUpdates._id, itemUpdates);
166
-
167
- for (const [ _id, item ] of this) {
168
- const itemUpdates = updatesMap.get(_id);
169
- if (itemUpdates) {
170
- const prevItemUpdates = prevUpdates.get(_id);
171
-
172
- if (TheItem)
173
- item.update(itemUpdates);
174
- else {
175
- for (const key in prevItemUpdates)// eslint-disable-line guard-for-in
176
- delete item[key as keyof typeof item];
177
-
178
- Object.assign(item, itemUpdates);
179
- }
180
-
181
- prevUpdates.set(_id, itemUpdates);
182
- updated.push(item);
183
- updatesMap.delete(_id);
184
- } else {
185
- if (TheItem)
186
- item.delete();
187
- else
188
- this.delete(_id);
189
- prevUpdates.delete(_id);
190
- updated.deleted.push(_id);
191
- }
192
- }
193
-
194
- for (const [ _id, itemUpdates ] of updatesMap) {
195
- const item = TheItem ? new TheItem(this, itemUpdates) : { ...itemUpdates };
196
- this.set(_id, item);
197
- prevUpdates.set(_id, itemUpdates);
198
- updated.push(item);
199
- updated.added.push(item);
200
- }
201
-
202
- if (this.sortList)
203
- this.sorted = array.map(newItem => this.get(newItem._id)!);
204
-
205
- } else
206
- for (const itemUpdates of array) {
207
- const item = (TheItem ? new TheItem(this, itemUpdates) : { ...itemUpdates }) as I;
208
- this.set(item._id, item);
209
- this.sorted.push(item);
210
- prevUpdates.set(item._id, itemUpdates);
211
- updated.push(item);
212
- updated.added.push(item);
213
- }
214
-
215
- break;
216
- }
217
-
218
- case "c"/* create */:
219
- case "u"/* update */: {
220
- let [ , itemUpdates ] = update;
221
- let item = this.get(itemUpdates._id);
222
-
223
- if (item) {
224
- const isAtomic = update[2] === true;
225
- const updatedFields = isAtomic ? Object.keys(itemUpdates) : update[2] as string[];
226
- const prevItemUpdates = prevUpdates.get(item._id);
227
-
228
- if (TheItem)
229
- item.update(itemUpdates);
230
- else {
231
- if (!isAtomic)
232
- for (const key in prevItemUpdates)// eslint-disable-line guard-for-in
233
- delete item[key as keyof typeof item];
234
-
235
- Object.assign(item, itemUpdates);
236
- }
237
-
238
- if (isAtomic)
239
- itemUpdates = Object.assign(prevItemUpdates, itemUpdates);
240
-
241
- if (this.sortList && !shouldSort && (!updatedFields || updatedFields.some(fieldName => this.fieldsToSort.includes(fieldName))))
242
- shouldSort = true;
243
- } else {
244
- item = (TheItem ? new TheItem(this, itemUpdates) : { ...itemUpdates }) as I;
245
- this.set(item._id, item);
246
- this.sorted.push(item);
247
- if (this.sortList)
248
- shouldSort = true;
249
- updated.added.push(item);
250
- }
251
-
252
- prevUpdates.set(item._id, itemUpdates);
253
- updated.push(item);
254
-
255
- break;
256
- }
257
-
258
- case "d"/* delete */: {
259
- const [ , _id ] = update;
260
- const itemToDelete = this.get(_id);
261
-
262
- if (itemToDelete) {
263
- if (TheItem)
264
- itemToDelete.delete();
265
- else
266
- this.delete(_id);
267
- prevUpdates.delete(_id);
268
- removeOne(this.sorted, itemToDelete);
269
- updated.deleted.push(_id);
270
- }
271
-
272
- break;
273
- }
274
-
275
- default:
276
- this.clear();
277
- }
278
-
279
- if (shouldSort)
280
- this.sort();
281
-
282
- } else
283
- this.clear();
284
-
285
- this.handleUpdate?.(this, updated, updates?.[0] ? updates : null);
286
-
287
- return this;
288
- }
289
-
290
- [updateSymbol] = this.update;
291
-
292
- getAsUpdates() {
293
- return (
294
- (this.size || this.sortList) ?
295
- [ [ "i"/* initial */, [ ...this.updates.values() ], this.sortList ] ] :
296
- null
297
- );
298
- }
299
-
300
- [getAsUpdatesSymbol] = this.getAsUpdates;
301
-
302
- getAsInitialUpdated() {
303
- return Object.assign([ ...this.sorted ], {
304
- added: [ ...this.sorted ],
305
- deleted: []
306
- });
307
- }
308
-
309
- [getAsInitialUpdatedSymbol] = this.getAsInitialUpdated;
310
-
311
- clear() {
312
-
313
- super.clear();
314
- this.updates.clear();
315
- this.sorted = [];
316
- this.sortList = null;
317
- this.sortCompareFunction = null;
318
- this.fieldsToSort = [];
319
-
320
- }
321
-
322
- [clearSymbol] = this.clear;
323
-
324
- valueOf() {
325
- return this.sorted;
326
- }
327
-
328
- get [Symbol.toStringTag]() {
329
- return "SubscriptionMap";
330
- }
331
-
332
-
333
- static updateSymbol = updateSymbol;
334
- static getHandleUpdateSymbol = getHandleUpdateSymbol;
335
- static setHandleUpdateSymbol = setHandleUpdateSymbol;
336
- static getAsUpdatesSymbol = getAsUpdatesSymbol;
337
- static getAsInitialUpdatedSymbol = getAsInitialUpdatedSymbol;
338
- static clearSymbol = clearSymbol;
339
- static WithSubscription: typeof SubscriptionMapWithSubscription;
340
- static Item: typeof SubscriptionMapItem;
341
-
342
- }
343
-
344
-
345
- export class SubscriptionMapWithSubscription<I extends Item = Item> extends SubscriptionMap<I> {
346
- constructor(publicationName: string, params: unknown[], onUpdate: UpdateHandler<I>, immediately = true) {
347
- super(null, onUpdate);
348
-
349
- this.publicationName = publicationName;
350
- this.params = params;
351
-
352
- if (immediately)
353
- this.subscribe();
354
-
355
- }
356
-
357
- publicationName;
358
- params;
359
-
360
- subscription: null | Subscription = null;
361
-
362
- subscribe() {
363
-
364
- if (!this.subscription)
365
- this.subscription = new Subscription("map", this.publicationName, this.params, updates => this.update(updates as Updates<I>));
366
-
367
- }
368
-
369
- get [subscribeSymbol]() {
370
- return this.subscribe;
371
- }
372
-
373
- unsubscribe() {
374
-
375
- if (this.subscription) {
376
- this.subscription.cancel();
377
- this.subscription = null;
378
- }
379
-
380
- }
381
-
382
- get [unsubscribeSymbol]() {
383
- return this.unsubscribe;
384
- }
385
-
386
- renew(publicationName?: string, params?: unknown[]) {
387
-
388
- if (this.subscription) {
389
- this.unsubscribe();
390
-
391
- if (publicationName)
392
- if (Array.isArray(publicationName))
393
- params = publicationName;
394
- else
395
- this.publicationName = publicationName;
396
-
397
- if (params)
398
- this.params = params;
399
-
400
- this.subscribe();
401
- }
402
-
403
- }
404
-
405
- get [renewSymbol]() {
406
- return this.renew;
407
- }
408
-
409
- [getSubscriptionSymbol]() {
410
- return this.subscription;
411
- }
412
-
413
- get [Symbol.toStringTag]() {
414
- return "SubscriptionMapWithSubscription";
415
- }
416
-
417
-
418
- static subscribeSymbol = subscribeSymbol;
419
- static unsubscribeSymbol = unsubscribeSymbol;
420
- static renewSymbol = renewSymbol;
421
- static getSubscriptionSymbol = getSubscriptionSymbol;
422
-
423
- }
424
-
425
- SubscriptionMap.WithSubscription = SubscriptionMapWithSubscription;
426
-
427
-
428
- export class SubscriptionMapItem {
429
- constructor(map: SubscriptionMap, updates: GenericItem) {
430
- this.map = map;
431
-
432
- this.update(updates);
433
-
434
- }
435
-
436
- _id!: string;
437
-
438
- private map;
439
-
440
- update(updates: GenericItem) {
441
- Object.assign(this, updates);
442
-
443
- }
444
-
445
- delete() {
446
-
447
- this.map.delete(this._id);
448
-
449
- }
450
-
451
- }
452
-
453
- SubscriptionMap.Item = SubscriptionMapItem;
454
-
455
- export { Updated as SubscriptionMapUpdated };