insite-subscriptions-client 1.0.295 → 2.0.1

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 (36) 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/dist/index.d.ts +7 -0
  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 +13 -12
  26. package/bun.lockb +0 -0
  27. package/lib/Subscription.js +0 -106
  28. package/lib/SubscriptionArray.js +0 -241
  29. package/lib/SubscriptionGroup/SubscriptionGroup.js +0 -292
  30. package/lib/SubscriptionGroup/SubscriptionGroupItem.js +0 -300
  31. package/lib/SubscriptionGroup/common.js +0 -1
  32. package/lib/SubscriptionGroup/index.js +0 -1
  33. package/lib/SubscriptionMap.js +0 -393
  34. package/lib/SubscriptionObject.js +0 -153
  35. package/lib/index.js +0 -21
  36. package/lib/symbols.js +0 -12
@@ -1,292 +0,0 @@
1
- import { debounce } from "@nesvet/n";
2
- import EventEmitter from "eventemitter3";
3
- import { privates } from "./common";
4
- import { SubscriptionGroupItem } from "./SubscriptionGroupItem";
5
-
6
-
7
- function parseDefinition(definition) {
8
- if (Array.isArray(definition)) {
9
- const args = definition.slice();
10
-
11
- const parsed = {};
12
-
13
- parsed.name = args.shift();
14
- if (typeof args[0] == "object" && !Array.isArray(args[0])) {
15
- parsed.value = args.shift();
16
- parsed.type = parsed.value.toString().match(/Subscription(Object|Array|Map)/)[1];
17
- if (parsed.type)
18
- parsed.type = parsed.type.toLowerCase();
19
- else
20
- throw new Error("Unknown value type");
21
- } else {
22
- if (typeof args[0] == "string")
23
- parsed.type = args.shift();
24
- if (typeof args[0] == "string")
25
- parsed.publicationName = args.shift();
26
- if (Array.isArray(args[0]))
27
- parsed.params = args.shift();
28
- }
29
- if (typeof args[0] == "function")
30
- parsed.handle = args.shift();
31
- if (typeof args[0] == "function")
32
- parsed.onBeforeInit = args.shift();
33
- if (typeof args[0] == "number")
34
- parsed.debounceLimit = args.shift();
35
- if (typeof args[0] == "boolean")
36
- parsed.preventBind = args.shift();
37
-
38
- return parsed;
39
- }
40
-
41
- return definition;
42
- }
43
-
44
- export class SubscriptionGroup extends EventEmitter {
45
- constructor(definitions, options = {}) {
46
- super();
47
-
48
- const {
49
- target,
50
- debounce: debounceLimit = SubscriptionGroup.debounceLimit,
51
- immediately
52
- } = options;
53
-
54
- this.#applyOptions({ target, debounceLimit });
55
-
56
- this.#initPromise = new Promise(resolve => (this.#initResolve = resolve));
57
-
58
- this.attach(definitions, false, immediately);
59
-
60
- }
61
-
62
- items = [];
63
- values = [];
64
-
65
- isLoaded = false;
66
- isInited = false;
67
-
68
- #loadPromise;
69
- #initPromise;
70
- #unloadPromise;
71
- #loadResolve;
72
- #initResolve;
73
- #unloadResolve;
74
-
75
- #debounceLimit = null;
76
-
77
- #applyOptions({ target, debounceLimit }) {
78
-
79
- if (this.target !== target) {
80
- if (this.target)
81
- for (const item of this.items)
82
- if (item.isLoaded && !item.preventBind)
83
- delete this.target[item.name];
84
-
85
- this.target = target;
86
-
87
- if (this.target)
88
- for (const item of this.items)
89
- if (item.isLoaded && !item.preventBind)
90
- this.target[item.name] = item.value;
91
- }
92
-
93
- if (this.#debounceLimit !== debounceLimit) {
94
- this.#debounceLimit = debounceLimit;
95
- if (typeof this.#debounceLimit == "number") {
96
- const emitUpdateDebounced = debounce(() => this.emit("update", this.values), this.#debounceLimit);
97
- this.emitUpdate = () => emitUpdateDebounced();
98
- } else
99
- this.emitUpdate = () => this.emit("update", this.values);
100
- }
101
-
102
- }
103
-
104
- attach(definitions, shouldReload = this.isLoaded, immediately = true) {
105
- if (shouldReload)
106
- this.isLoaded = false;
107
-
108
- if (!this.#loadPromise || this.#loadPromise.isResolved)
109
- this.#loadPromise = new Promise(resolve => (this.#loadResolve = resolve));
110
-
111
- if (!this.#unloadPromise || this.#unloadPromise.isResolved)
112
- this.#unloadPromise = new Promise(resolve => (this.#unloadResolve = resolve));
113
-
114
- return Promise.all(definitions.map(definition =>
115
- new SubscriptionGroupItem(parseDefinition(definition), this)
116
- ).map(item => {
117
-
118
- if (immediately)
119
- item.subscribe();
120
-
121
- return item.loaded();
122
- }));
123
- }
124
-
125
- detach(names, shouldUpdate = true) {
126
-
127
- for (const name of names)
128
- this.items[name]?.detach();
129
-
130
- if (shouldUpdate) {
131
- this.#unload();
132
- this.#load();
133
- }
134
-
135
- }
136
-
137
- redefine(definitions) {
138
-
139
- const itemsToDetach = new Map(this.items.map(item => [ item.name, item ]));
140
- const definitionsToRedefine = [];
141
- const definitionsToAttach = [];
142
-
143
- for (let definition of definitions) {
144
- definition = parseDefinition(definition);
145
- if (itemsToDetach.delete(definition.name))
146
- definitionsToRedefine.push(definition);
147
- else
148
- definitionsToAttach.push(definition);
149
- }
150
-
151
- this.isLoaded = false;
152
-
153
- this.detach([ ...itemsToDetach.keys() ], false);
154
-
155
- return Promise.all([
156
- ...definitionsToRedefine.map(definition => {
157
- const item = this.items[definition.name];
158
- item.define(definition);
159
-
160
- item.subscribe();
161
-
162
- return item.loaded();
163
- }),
164
- ...definitionsToAttach.length ? [ this.attach(definitionsToAttach, false) ] : []
165
- ]);
166
- }
167
-
168
- subscribe() {
169
- return this.items.map(item => item.subscribe());
170
- }
171
-
172
- unsubscribe() {
173
- return this.items.map(item => item.unsubscribe());
174
- }
175
-
176
- #load() {
177
-
178
- if (!this.isLoaded && this.items.every(item => item.isLoaded)) {
179
- if (this.target)
180
- for (const item of this.items)
181
- if (!item.preventBind)
182
- this.target[item.name] = item.value;
183
-
184
- this.isLoaded = true;
185
-
186
- for (const item of this.items)
187
- privates.itemLoad(item);
188
-
189
- this.emit("load", this.values);
190
-
191
- if (this.isInited)
192
- if (this.items.length)
193
- for (const item of this.items)
194
- item.emitUpdate();
195
- else
196
- this.emitUpdate();
197
-
198
-
199
- this.#loadResolve(this);
200
- this.#loadPromise.isResolved = true;
201
-
202
- if (this.#unloadPromise.isResolved)
203
- this.#unloadPromise = new Promise(resolve => (this.#unloadResolve = resolve));
204
- }
205
-
206
- }
207
-
208
- #init() {
209
-
210
- if (!this.isInited && this.items.every(item => item.isInited)) {
211
- this.isInited = true;
212
-
213
- if (this.isLoaded)
214
- if (this.items.length)
215
- for (const item of this.items)
216
- item.emitUpdate();
217
- else
218
- this.emitUpdate();
219
-
220
-
221
- for (const item of this.items)
222
- privates.itemInit(item);
223
-
224
- this.emit("init", this.values);
225
-
226
- this.#initResolve(this);
227
- }
228
-
229
- }
230
-
231
- #unload() {
232
-
233
- if (this.isLoaded && this.items.every(item => !item.isLoaded)) {
234
- if (this.target)
235
- for (const item of this.items)
236
- if (!item.preventBind)
237
- delete this.target[item.name];
238
-
239
- this.isLoaded = false;
240
-
241
- for (const item of this.items)
242
- privates.itemUnload(item);
243
-
244
- this.emit("unload", this.values);
245
-
246
- if (this.items.length)
247
- for (const item of this.items)
248
- item.emitUpdate();
249
- else
250
- this.emitUpdate();
251
-
252
- this.#unloadResolve(this);
253
- this.#unloadPromise.isResolved = true;
254
-
255
- if (this.#loadPromise.isResolved)
256
- this.#loadPromise = new Promise(resolve => (this.#loadResolve = resolve));
257
- }
258
-
259
- }
260
-
261
- loaded() {
262
- return this.#loadPromise;
263
- }
264
-
265
- inited() {
266
- return this.#initPromise;
267
- }
268
-
269
- unloaded() {
270
- return this.#unloadPromise;
271
- }
272
-
273
- valueOf() {
274
- return this.values;
275
- }
276
-
277
- [Symbol.iterator]() {
278
- return this.values[Symbol.iterator]();
279
- }
280
-
281
-
282
- static debounceLimit = 8;
283
-
284
- static {
285
-
286
- privates.groupLoad = group => group.#load();
287
- privates.groupInit = group => group.#init();
288
- privates.groupUnload = group => group.#unload();
289
-
290
- }
291
-
292
- }
@@ -1,300 +0,0 @@
1
- import { debounce, removeOne } from "@nesvet/n";
2
- import EventEmitter from "eventemitter3";
3
- import { SubscriptionArray } from "../SubscriptionArray";
4
- import { SubscriptionMap } from "../SubscriptionMap";
5
- import { SubscriptionObject } from "../SubscriptionObject";
6
- import {
7
- getAsInitialUpdatedSymbol,
8
- getAsUpdatesSymbol,
9
- getHandleUpdateSymbol,
10
- renewSymbol,
11
- setHandleUpdateSymbol,
12
- subscribeSymbol,
13
- unsubscribeSymbol
14
- } from "../symbols";
15
- import { privates } from "./common";
16
-
17
-
18
- const SubscriptionValuesByType = {
19
- object: SubscriptionObject,
20
- array: SubscriptionArray,
21
- map: SubscriptionMap
22
- };
23
-
24
-
25
- export class SubscriptionGroupItem extends EventEmitter {
26
- constructor(definition, group) {
27
- super();
28
-
29
- this.name = definition.name;
30
-
31
- this.define(definition, group);
32
-
33
- }
34
-
35
- isLoaded = false;
36
- isInited = false;
37
-
38
- #loadPromise;
39
- #initPromise;
40
- #unloadPromise;
41
- #loadResolve;
42
- #initResolve;
43
- #unloadResolve;
44
-
45
- define(definition, group = this.group) {
46
-
47
- const {
48
- value,
49
- type = "object",
50
- publicationName,
51
- params,
52
- handle,
53
- onBeforeInit,
54
- debounce: debounceLimit = SubscriptionGroupItem.debounceLimit,
55
- preventBind
56
- } = definition;
57
-
58
- if (this.group) {
59
- if (
60
- (!value || this.value === value) &&
61
- this.type === type &&
62
- this.publicationName === publicationName &&
63
- JSON.stringify(this.params) === JSON.stringify(params) &&
64
- this.handle === handle &&
65
- this.onBeforeInit === onBeforeInit
66
- )
67
- return this.#applyOptions({ debounceLimit, preventBind });
68
-
69
- this.detach();
70
- delete this.value;
71
- delete this.publicationName;
72
- delete this.params;
73
- }
74
-
75
- this.type = type;
76
- this.handle = handle;
77
- this.onBeforeInit = onBeforeInit;
78
- if (value)
79
- this.value = value;
80
- else {
81
- this.publicationName = publicationName ?? this.name;
82
- this.params = params ?? [];
83
- }
84
-
85
- this.#applyOptions({ debounceLimit, preventBind });
86
-
87
- this.#attachTo(group);
88
-
89
- }
90
-
91
- #applyOptions({ debounceLimit, preventBind }) {
92
-
93
- const emitUpdate = updated => {
94
- this.emit("update", this.value, updated);
95
- this.group.emit(`update.${this.name}`, this.value, updated);
96
- this.group.emitUpdate();
97
-
98
- };
99
-
100
- if (typeof debounceLimit == "number") {
101
- let debouncedUpdated;
102
- let group;
103
-
104
- const emitUpdateDebounced = debounce(() => {
105
-
106
- const isDetached = !this.group;
107
-
108
- if (isDetached)
109
- this.group = group;
110
-
111
- emitUpdate(debouncedUpdated);
112
-
113
- if (isDetached)
114
- delete this.group;
115
-
116
- debouncedUpdated = undefined;
117
-
118
- }, debounceLimit);
119
-
120
- this.emitUpdate = updated => {
121
- if (updated !== undefined)
122
- if (Array.isArray(updated))
123
- if (debouncedUpdated)
124
- for (const item of updated)
125
- debouncedUpdated.push(item);
126
- else
127
- debouncedUpdated = [ ...updated ];
128
- else if (typeof updated == "object")
129
- if (debouncedUpdated)
130
- Object.assign(debouncedUpdated, updated);
131
- else
132
- debouncedUpdated = { ...updated };
133
- else
134
- debouncedUpdated = updated;
135
-
136
- ({ group } = this);
137
-
138
- return emitUpdateDebounced();
139
- };
140
- } else
141
- this.emitUpdate = emitUpdate;
142
-
143
- if (this.preventBind === undefined)
144
- delete this.preventBind;
145
- else
146
- this.preventBind = preventBind;
147
-
148
- }
149
-
150
- #attachTo(group) {
151
- this.group = group;
152
-
153
- this.group.items.push(this);
154
- this.group.items[this.name] = this;
155
-
156
- if (!this.#loadPromise || this.#loadPromise.isResolved)
157
- this.#loadPromise = new Promise(resolve => (this.#loadResolve = resolve));
158
-
159
- if (!this.#initPromise || this.#initPromise.isResolved)
160
- this.#initPromise = new Promise(resolve => (this.#initResolve = resolve));
161
-
162
- if (!this.#unloadPromise || this.#unloadPromise.isResolved)
163
- this.#unloadPromise = new Promise(resolve => (this.#unloadResolve = resolve));
164
-
165
- if (this.value)
166
- this.value[setHandleUpdateSymbol](this.#handleSubscription);
167
- else
168
- this.value = new SubscriptionValuesByType[this.type].WithSubscription(this.publicationName, this.params, this.#handleSubscription, false);
169
-
170
- this.group.values.push(this.value);
171
- this.group.values[this.name] = this.value;
172
-
173
- this.onBeforeInit?.call(this.group, this.value);
174
-
175
- }
176
-
177
- detach() {
178
-
179
- this.value[unsubscribeSymbol]?.();
180
-
181
- this.isLoaded = false;
182
- this.#unload();
183
-
184
- delete this.group.values[this.name];
185
- removeOne(this.group.values, this.value);
186
- delete this.group.items[this.name];
187
- removeOne(this.group.items, this);
188
-
189
- delete this.group;
190
-
191
- }
192
-
193
- #loadUpdated = null;
194
-
195
- #handleSubscription = (_, updated, updates) => {
196
- if (updates)
197
- if (this.group.isLoaded) {
198
- this.handle?.call(this.group, updated, this.group);
199
- this.emitUpdate(updated);
200
- } else {
201
- this.#loadUpdated = updated;
202
- this.isLoaded = true;
203
- privates.groupLoad(this.group);
204
- }
205
- else if (this.isLoaded) {
206
- this.isLoaded = false;
207
- privates.groupUnload(this.group);
208
- }
209
-
210
- if (!this.isInited) {
211
- this.isInited = true;
212
- privates.groupInit(this.group);
213
- }
214
-
215
- };
216
-
217
- #load() {
218
-
219
- if (this.#loadUpdated) {
220
- this.handle?.call(this.group, this.#loadUpdated, this.group);
221
- this.#loadUpdated = null;
222
-
223
- this.emit("load", this.value);
224
-
225
- this.#loadResolve(this);
226
- this.#loadPromise.isResolved = true;
227
-
228
- if (this.#unloadPromise.isResolved)
229
- this.#unloadPromise = new Promise(resolve => (this.#unloadResolve = resolve));
230
- }
231
-
232
- }
233
-
234
- #init() {
235
-
236
- this.emit("init", this.value);
237
-
238
- this.#initResolve(this);
239
- this.#initPromise.isResolved = true;
240
-
241
- }
242
-
243
- #unload() {
244
-
245
- this.handle?.call(this.group, null, this.group);
246
-
247
- this.emit("unload", this.value);
248
-
249
- this.#unloadResolve(this);
250
- this.#unloadPromise.isResolved = true;
251
-
252
- if (this.#loadPromise.isResolved)
253
- this.#loadPromise = new Promise(resolve => (this.#loadResolve = resolve));
254
-
255
- }
256
-
257
- loaded() {
258
- return this.#loadPromise;
259
- }
260
-
261
- inited() {
262
- return this.#initPromise;
263
- }
264
-
265
- unloaded() {
266
- return this.#unloadPromise;
267
- }
268
-
269
- subscribe() {
270
- return (
271
- this.value[subscribeSymbol] ?
272
- this.value[subscribeSymbol]() :
273
- this.value[getHandleUpdateSymbol]()(this.value, this.value[getAsInitialUpdatedSymbol](), this.value[getAsUpdatesSymbol]())
274
- );
275
- }
276
-
277
- unsubscribe() {
278
- return this.value[unsubscribeSymbol]?.();
279
- }
280
-
281
- renew(publicationName, params) {
282
- return this.value[renewSymbol]?.(publicationName, params);
283
- }
284
-
285
- valueOf() {
286
- return this.value;
287
- }
288
-
289
-
290
- static debounceLimit = 4;
291
-
292
- static {
293
-
294
- privates.itemLoad = item => item.#load();
295
- privates.itemInit = item => item.#init();
296
- privates.itemUnload = item => item.#unload();
297
-
298
- }
299
-
300
- }
@@ -1 +0,0 @@
1
- export const privates = {};
@@ -1 +0,0 @@
1
- export * from "./SubscriptionGroup";