insite-subscriptions-client 1.0.294 → 2.0.0

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.
@@ -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";
@@ -1,153 +0,0 @@
1
- import { Subscription } from "./Subscription";
2
- import {
3
- clearSymbol,
4
- getAsInitialUpdatedSymbol,
5
- getAsUpdatesSymbol,
6
- getHandleUpdateSymbol,
7
- getSubscriptionSymbol,
8
- renewSymbol,
9
- setHandleUpdateSymbol,
10
- subscribeSymbol,
11
- unsubscribeSymbol,
12
- updateSymbol
13
- } from "./symbols";
14
-
15
-
16
- export class SubscriptionObject {
17
- #handleUpdate;
18
-
19
- constructor(updates, onUpdate) {
20
- this.#handleUpdate = onUpdate;
21
-
22
- if (updates)
23
- this[updateSymbol](updates);
24
-
25
- }
26
-
27
- #updates = null;
28
-
29
- [getHandleUpdateSymbol]() {
30
- return this.#handleUpdate;
31
- }
32
-
33
- [setHandleUpdateSymbol](handleUpdate) {
34
- return (this.#handleUpdate = handleUpdate);
35
- }
36
-
37
- [updateSymbol](updates) {
38
- if (updates) {
39
- for (const key in this.#updates)
40
- delete this[key];
41
- for (const key in updates)
42
- this[key] = updates[key];
43
- this.#updates = updates;
44
- } else
45
- this[clearSymbol]();
46
-
47
- this.#handleUpdate?.(this, this, this.#updates);
48
-
49
- return this;
50
- }
51
-
52
- [getAsUpdatesSymbol]() {
53
- return this.#updates && { ...this.#updates };
54
- }
55
-
56
- [getAsInitialUpdatedSymbol]() {
57
- return this.valueOf();
58
- }
59
-
60
- [clearSymbol]() {
61
-
62
- for (const key in this)
63
- delete this[key];
64
- this.#updates = null;
65
-
66
- }
67
-
68
- valueOf() {
69
- return this.#updates && this;
70
- }
71
-
72
- get [Symbol.toStringTag]() {
73
- return "SubscriptionObject";
74
- }
75
-
76
-
77
- static updateSymbol = updateSymbol;
78
- static getHandleUpdateSymbol = getHandleUpdateSymbol;
79
- static setHandleUpdateSymbol = setHandleUpdateSymbol;
80
- static getAsUpdatesSymbol = getAsUpdatesSymbol;
81
- static getAsInitialUpdatedSymbol = getAsInitialUpdatedSymbol;
82
- static clearSymbol = clearSymbol;
83
-
84
- }
85
-
86
-
87
- SubscriptionObject.WithSubscription = class SubscriptionObjectWithSubscription extends SubscriptionObject {
88
- #publicationName;
89
- #params;
90
-
91
- constructor(publicationName, params, onUpdate, immediately = true) {
92
- super(null, onUpdate);
93
-
94
- this.#publicationName = publicationName;
95
- this.#params = params;
96
-
97
- if (immediately)
98
- this[subscribeSymbol]();
99
-
100
- }
101
-
102
- #subscription = null;
103
-
104
- [subscribeSymbol]() {
105
-
106
- if (!this.#subscription)
107
- this.#subscription = new Subscription("object", this.#publicationName, this.#params, updates => this[updateSymbol](updates));
108
-
109
- }
110
-
111
- [unsubscribeSymbol]() {
112
-
113
- if (this.#subscription) {
114
- this.#subscription.cancel();
115
- this.#subscription = null;
116
- }
117
-
118
- }
119
-
120
- [renewSymbol](publicationName, params) {
121
-
122
- if (this.#subscription) {
123
- this[unsubscribeSymbol]();
124
-
125
- if (publicationName)
126
- if (Array.isArray(publicationName))
127
- params = publicationName;
128
- else
129
- this.#publicationName = publicationName;
130
-
131
- if (params)
132
- this.#params = params;
133
-
134
- this[subscribeSymbol]();
135
- }
136
-
137
- }
138
-
139
- [getSubscriptionSymbol]() {
140
- return this.#subscription;
141
- }
142
-
143
- get [Symbol.toStringTag]() {
144
- return "SubscriptionObjectWithSubscription";
145
- }
146
-
147
-
148
- static subscribeSymbol = subscribeSymbol;
149
- static unsubscribeSymbol = unsubscribeSymbol;
150
- static renewSymbol = renewSymbol;
151
- static getSubscriptionSymbol = getSubscriptionSymbol;
152
-
153
- };
package/lib/index.js DELETED
@@ -1,21 +0,0 @@
1
- //#if _SSR
2
- // export { default as Subscription, initialsMap, subscriptionContext } from "./Subscription.js";
3
- //#else
4
- export * from "./Subscription.js";
5
- export * from "./SubscriptionArray";
6
- export * from "./SubscriptionGroup";
7
- export * from "./SubscriptionMap";
8
- export * from "./SubscriptionObject";
9
- export { clearSymbol,
10
- getAsInitialUpdatedSymbol,
11
- getAsUpdatesSymbol,
12
- getHandleSubscriptionSymbol,
13
- getHandleUpdateSymbol,
14
- getSubscriptionSymbol,
15
- renewSymbol,
16
- setHandleSubscriptionSymbol,
17
- setHandleUpdateSymbol,
18
- subscribeSymbol,
19
- unsubscribeSymbol,
20
- updateSymbol } from "./symbols";
21
- //#endif
File without changes