insite-subscriptions-client 2.0.0 → 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.
- package/dist/Subscription.d.ts +18 -0
- package/dist/Subscription.d.ts.map +1 -0
- package/dist/SubscriptionArray.d.ts +73 -0
- package/dist/SubscriptionArray.d.ts.map +1 -0
- package/dist/SubscriptionGroup/SubscriptionGroup.d.ts +37 -0
- package/dist/SubscriptionGroup/SubscriptionGroup.d.ts.map +1 -0
- package/dist/SubscriptionGroup/SubscriptionGroupItem.d.ts +31 -0
- package/dist/SubscriptionGroup/SubscriptionGroupItem.d.ts.map +1 -0
- package/dist/SubscriptionGroup/common.d.ts +12 -0
- package/dist/SubscriptionGroup/common.d.ts.map +1 -0
- package/dist/SubscriptionGroup/index.d.ts +3 -0
- package/dist/SubscriptionGroup/index.d.ts.map +1 -0
- package/dist/SubscriptionGroup/types.d.ts +41 -0
- package/dist/SubscriptionGroup/types.d.ts.map +1 -0
- package/dist/SubscriptionMap.d.ts +102 -0
- package/dist/SubscriptionMap.d.ts.map +1 -0
- package/dist/SubscriptionObject.d.ts +47 -0
- package/dist/SubscriptionObject.d.ts.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +2 -14
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +7 -0
- package/dist/symbols.d.ts +13 -0
- package/dist/symbols.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/Subscription.ts +0 -101
- package/src/SubscriptionArray.ts +0 -275
- package/src/SubscriptionGroup/SubscriptionGroup.ts +0 -319
- package/src/SubscriptionGroup/SubscriptionGroupItem.ts +0 -391
- package/src/SubscriptionGroup/common.ts +0 -13
- package/src/SubscriptionGroup/index.ts +0 -8
- package/src/SubscriptionGroup/types.ts +0 -121
- package/src/SubscriptionMap.ts +0 -455
- package/src/SubscriptionObject.ts +0 -168
- package/src/symbols.ts +0 -12
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "eventemitter3";
|
|
2
|
-
import { debounce, StatefulPromise } from "@nesvet/n";
|
|
3
|
-
import { privates } from "./common";
|
|
4
|
-
import { SubscriptionGroupItem } from "./SubscriptionGroupItem";
|
|
5
|
-
import {
|
|
6
|
-
Definition,
|
|
7
|
-
ParseValues,
|
|
8
|
-
SubscriptionHandle,
|
|
9
|
-
SubscriptionOnBeforeInit,
|
|
10
|
-
SubscriptionType,
|
|
11
|
-
TupleDefinition,
|
|
12
|
-
UnparsedDefinition
|
|
13
|
-
} from "./types";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
function isTupleDefinition<T extends SubscriptionType>(definition: UnparsedDefinition<T>): definition is TupleDefinition {
|
|
20
|
-
return Array.isArray(definition);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type Target = any;
|
|
24
|
-
|
|
25
|
-
type Options = {
|
|
26
|
-
target?: Target;
|
|
27
|
-
debounce?: number;
|
|
28
|
-
immediately?: boolean;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
function parseDefinition<T extends SubscriptionType>(definition: UnparsedDefinition<T>) {
|
|
33
|
-
if (isTupleDefinition(definition)) {
|
|
34
|
-
const args = definition.slice();
|
|
35
|
-
|
|
36
|
-
const parsed: Partial<Definition<T>> = {};
|
|
37
|
-
|
|
38
|
-
parsed.name = args.shift() as Definition<T>["name"];
|
|
39
|
-
if (typeof args[0] == "object" && !Array.isArray(args[0])) {
|
|
40
|
-
parsed.value = args.shift() as Definition<T>["value"];
|
|
41
|
-
parsed.type = parsed.value!.toString().match(/Subscription(Object|Array|Map)/)?.[1].toLowerCase() as Definition<T>["type"];
|
|
42
|
-
if (!parsed.type)
|
|
43
|
-
throw new Error("Unknown value type");
|
|
44
|
-
} else {
|
|
45
|
-
if (typeof args[0] == "string")
|
|
46
|
-
parsed.type = args.shift() as Definition<T>["type"];
|
|
47
|
-
if (typeof args[0] == "string")
|
|
48
|
-
parsed.publicationName = args.shift() as Definition<T>["publicationName"];
|
|
49
|
-
if (Array.isArray(args[0]))
|
|
50
|
-
parsed.params = args.shift() as Definition<T>["params"];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (typeof args[0] == "function")
|
|
54
|
-
parsed.handle = args.shift() as SubscriptionHandle<T>;
|
|
55
|
-
if (typeof args[0] == "function")
|
|
56
|
-
parsed.onBeforeInit = args.shift() as SubscriptionOnBeforeInit<T>;
|
|
57
|
-
if (typeof args[0] == "number")
|
|
58
|
-
parsed.debounce = args.shift() as Definition<T>["debounce"];
|
|
59
|
-
if (typeof args[0] == "boolean")
|
|
60
|
-
parsed.preventBind = args.shift() as Definition<T>["preventBind"];
|
|
61
|
-
|
|
62
|
-
return parsed as Definition<T>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
definition.type ??= "object" as T;
|
|
66
|
-
|
|
67
|
-
return definition;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export class SubscriptionGroup<DS extends UnparsedDefinition[] = any[]> extends EventEmitter {
|
|
71
|
-
constructor(
|
|
72
|
-
definitions: DS,
|
|
73
|
-
{
|
|
74
|
-
target,
|
|
75
|
-
debounce: debounceLimit = SubscriptionGroup.debounceLimit,
|
|
76
|
-
immediately
|
|
77
|
-
}: Options = {}
|
|
78
|
-
) {
|
|
79
|
-
super();
|
|
80
|
-
|
|
81
|
-
this.applyOptions({ target, debounceLimit });
|
|
82
|
-
|
|
83
|
-
this.#initPromise = new StatefulPromise();
|
|
84
|
-
|
|
85
|
-
this.attach(definitions, false, immediately);
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
target?: Target;
|
|
90
|
-
emitUpdate!: () => void;
|
|
91
|
-
|
|
92
|
-
items = [] as unknown as Record<string, SubscriptionGroupItem> & SubscriptionGroupItem[];
|
|
93
|
-
values = [] as DS extends any ? (any[] & Record<string, any>) : ParseValues<DS>;
|
|
94
|
-
|
|
95
|
-
isLoaded = false;
|
|
96
|
-
isInited = false;
|
|
97
|
-
|
|
98
|
-
#loadPromise!: StatefulPromise<SubscriptionGroup>;
|
|
99
|
-
#initPromise: StatefulPromise<SubscriptionGroup>;
|
|
100
|
-
#unloadPromise!: StatefulPromise<SubscriptionGroup>;
|
|
101
|
-
|
|
102
|
-
#debounceLimit?: null | number = null;
|
|
103
|
-
|
|
104
|
-
applyOptions({ target, debounceLimit }: { target?: Target; debounceLimit?: number }) {
|
|
105
|
-
|
|
106
|
-
if (this.target !== target) {
|
|
107
|
-
if (this.target)
|
|
108
|
-
for (const item of this.items)
|
|
109
|
-
if (item.isLoaded && !item.preventBind)
|
|
110
|
-
delete this.target[item.name];
|
|
111
|
-
|
|
112
|
-
this.target = target;
|
|
113
|
-
|
|
114
|
-
if (this.target)
|
|
115
|
-
for (const item of this.items)
|
|
116
|
-
if (item.isLoaded && !item.preventBind)
|
|
117
|
-
this.target[item.name] = item.value;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (this.#debounceLimit !== debounceLimit) {
|
|
121
|
-
this.#debounceLimit = debounceLimit;
|
|
122
|
-
if (typeof this.#debounceLimit == "number") {
|
|
123
|
-
const emitUpdateDebounced = debounce(() => this.emit("update", this.values), this.#debounceLimit);
|
|
124
|
-
this.emitUpdate = () => emitUpdateDebounced();
|
|
125
|
-
} else
|
|
126
|
-
this.emitUpdate = () => this.emit("update", this.values);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
attach(definitions: DS, shouldReload = this.isLoaded, immediately = true) {
|
|
132
|
-
if (shouldReload)
|
|
133
|
-
this.isLoaded = false;
|
|
134
|
-
|
|
135
|
-
if (!this.#loadPromise || this.#loadPromise.isFulfilled)
|
|
136
|
-
this.#loadPromise = new StatefulPromise();
|
|
137
|
-
|
|
138
|
-
if (!this.#unloadPromise || this.#unloadPromise.isFulfilled)
|
|
139
|
-
this.#unloadPromise = new StatefulPromise();
|
|
140
|
-
|
|
141
|
-
return Promise.all(definitions.map(definition =>
|
|
142
|
-
new SubscriptionGroupItem(parseDefinition(definition), this)
|
|
143
|
-
).map(item => {
|
|
144
|
-
|
|
145
|
-
if (immediately)
|
|
146
|
-
item.subscribe();
|
|
147
|
-
|
|
148
|
-
return item.loaded();
|
|
149
|
-
}));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
detach(names: string[], shouldUpdate = true) {
|
|
153
|
-
|
|
154
|
-
for (const name of names)
|
|
155
|
-
this.items[name]?.detach();
|
|
156
|
-
|
|
157
|
-
if (shouldUpdate) {
|
|
158
|
-
this.#unload();
|
|
159
|
-
this.#load();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
redefine(definitions: DS) {
|
|
165
|
-
|
|
166
|
-
const itemsToDetach = new Map(this.items.map(item => [ item.name, item ]));
|
|
167
|
-
const definitionsToRedefine = [];
|
|
168
|
-
const definitionsToAttach = [] as unknown as DS;
|
|
169
|
-
|
|
170
|
-
for (let definition of definitions) {
|
|
171
|
-
definition = parseDefinition(definition);
|
|
172
|
-
if (itemsToDetach.delete(definition.name))
|
|
173
|
-
definitionsToRedefine.push(definition);
|
|
174
|
-
else
|
|
175
|
-
definitionsToAttach.push(definition);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
this.isLoaded = false;
|
|
179
|
-
|
|
180
|
-
this.detach([ ...itemsToDetach.keys() ], false);
|
|
181
|
-
|
|
182
|
-
return Promise.all([
|
|
183
|
-
...definitionsToRedefine.map(definition => {
|
|
184
|
-
const item = this.items[definition.name];
|
|
185
|
-
item.define(definition);
|
|
186
|
-
|
|
187
|
-
item.subscribe();
|
|
188
|
-
|
|
189
|
-
return item.loaded();
|
|
190
|
-
}),
|
|
191
|
-
...definitionsToAttach.length ? [ this.attach(definitionsToAttach, false) ] : []
|
|
192
|
-
]);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
subscribe() {
|
|
196
|
-
return this.items.map(item => item.subscribe());
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
unsubscribe() {
|
|
200
|
-
return this.items.map(item => item.unsubscribe());
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
#load() {
|
|
204
|
-
|
|
205
|
-
if (!this.isLoaded && this.items.every(item => item.isLoaded)) {
|
|
206
|
-
if (this.target)
|
|
207
|
-
for (const item of this.items)
|
|
208
|
-
if (!item.preventBind)
|
|
209
|
-
this.target[item.name] = item.value;
|
|
210
|
-
|
|
211
|
-
this.isLoaded = true;
|
|
212
|
-
|
|
213
|
-
for (const item of this.items)
|
|
214
|
-
privates.itemLoad!(item);
|
|
215
|
-
|
|
216
|
-
this.emit("load", this.values);
|
|
217
|
-
|
|
218
|
-
if (this.isInited)
|
|
219
|
-
if (this.items.length)
|
|
220
|
-
for (const item of this.items)
|
|
221
|
-
item.emitUpdate();
|
|
222
|
-
else
|
|
223
|
-
this.emitUpdate();
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
this.#loadPromise.resolve(this);
|
|
227
|
-
|
|
228
|
-
if (this.#unloadPromise.isFulfilled)
|
|
229
|
-
this.#unloadPromise = new StatefulPromise();
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
#init() {
|
|
235
|
-
|
|
236
|
-
if (!this.isInited && this.items.every(item => item.isInited)) {
|
|
237
|
-
this.isInited = true;
|
|
238
|
-
|
|
239
|
-
if (this.isLoaded)
|
|
240
|
-
if (this.items.length)
|
|
241
|
-
for (const item of this.items)
|
|
242
|
-
item.emitUpdate();
|
|
243
|
-
else
|
|
244
|
-
this.emitUpdate();
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
for (const item of this.items)
|
|
248
|
-
privates.itemInit!(item);
|
|
249
|
-
|
|
250
|
-
this.emit("init", this.values);
|
|
251
|
-
|
|
252
|
-
this.#initPromise.resolve(this);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
#unload() {
|
|
258
|
-
|
|
259
|
-
if (this.isLoaded && this.items.every(item => !item.isLoaded)) {
|
|
260
|
-
if (this.target)
|
|
261
|
-
for (const item of this.items)
|
|
262
|
-
if (!item.preventBind)
|
|
263
|
-
delete this.target[item.name];
|
|
264
|
-
|
|
265
|
-
this.isLoaded = false;
|
|
266
|
-
|
|
267
|
-
for (const item of this.items)
|
|
268
|
-
privates.itemUnload!(item);
|
|
269
|
-
|
|
270
|
-
this.emit("unload", this.values);
|
|
271
|
-
|
|
272
|
-
if (this.items.length)
|
|
273
|
-
for (const item of this.items)
|
|
274
|
-
item.emitUpdate();
|
|
275
|
-
else
|
|
276
|
-
this.emitUpdate();
|
|
277
|
-
|
|
278
|
-
this.#unloadPromise.resolve(this);
|
|
279
|
-
|
|
280
|
-
if (this.#loadPromise.isFulfilled)
|
|
281
|
-
this.#loadPromise = new StatefulPromise();
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
loaded() {
|
|
287
|
-
return this.#loadPromise;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
inited() {
|
|
291
|
-
return this.#initPromise;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
unloaded() {
|
|
295
|
-
return this.#unloadPromise;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
valueOf() {
|
|
299
|
-
return this.values;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
[Symbol.iterator]() {
|
|
303
|
-
return this.values[Symbol.iterator]();
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
static debounceLimit = 8;
|
|
308
|
-
|
|
309
|
-
static {
|
|
310
|
-
|
|
311
|
-
privates.groupLoad = (group: SubscriptionGroup) => group.#load();
|
|
312
|
-
privates.groupInit = (group: SubscriptionGroup) => group.#init();
|
|
313
|
-
privates.groupUnload = (group: SubscriptionGroup) => group.#unload();
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
export { Options as SubscriptionGroupOptions, Target as SubscriptionGroupTarget };
|
|
@@ -1,391 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "eventemitter3";
|
|
2
|
-
import { debounce, removeOne, StatefulPromise } from "@nesvet/n";
|
|
3
|
-
import { type SubscriptionArrayUpdated, SubscriptionArrayWithSubscription } from "../SubscriptionArray";
|
|
4
|
-
import { type SubscriptionMapUpdated, SubscriptionMapWithSubscription } from "../SubscriptionMap";
|
|
5
|
-
import { type Updated as SubscriptionObjectUpdated, SubscriptionObjectWithSubscription } from "../SubscriptionObject";
|
|
6
|
-
import {
|
|
7
|
-
renewSymbol,
|
|
8
|
-
setHandleUpdateSymbol,
|
|
9
|
-
subscribeSymbol,
|
|
10
|
-
unsubscribeSymbol
|
|
11
|
-
} from "../symbols";
|
|
12
|
-
import { privates } from "./common";
|
|
13
|
-
import type { SubscriptionGroup } from "./SubscriptionGroup";
|
|
14
|
-
import type {
|
|
15
|
-
Definition,
|
|
16
|
-
SubscriptionHandle,
|
|
17
|
-
SubscriptionOnBeforeInit,
|
|
18
|
-
SubscriptionType,
|
|
19
|
-
SubscriptionUpdated,
|
|
20
|
-
SubscriptionUpdates,
|
|
21
|
-
SubscriptionValue
|
|
22
|
-
} from "./types";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
type HandleSubscription<T> = (_: any, updated: SubscriptionUpdated<T>, updates: SubscriptionUpdates<T>) => void;
|
|
29
|
-
|
|
30
|
-
function isUpdatedMapOrArray(updated: SubscriptionUpdated<SubscriptionType>): updated is SubscriptionArrayUpdated | SubscriptionMapUpdated {
|
|
31
|
-
return Array.isArray(updated);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
export class SubscriptionGroupItem<T extends SubscriptionType = SubscriptionType, D extends Definition<T> = Definition<T>> extends EventEmitter {
|
|
36
|
-
constructor(definition: D, group: SubscriptionGroup) {
|
|
37
|
-
super();
|
|
38
|
-
|
|
39
|
-
this.name = definition.name;
|
|
40
|
-
|
|
41
|
-
this.define(definition, group);
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
name;
|
|
46
|
-
group?: SubscriptionGroup;
|
|
47
|
-
value?: SubscriptionValue<T>;
|
|
48
|
-
type!: "object" | T;
|
|
49
|
-
publicationName!: Definition<T>["publicationName"];
|
|
50
|
-
params!: Definition<T>["params"];
|
|
51
|
-
handle!: Definition<T>["handle"];
|
|
52
|
-
onBeforeInit!: Definition<T>["onBeforeInit"];
|
|
53
|
-
preventBind!: Definition<T>["preventBind"];
|
|
54
|
-
|
|
55
|
-
emitUpdate!: (updated?: SubscriptionUpdated<T>) => void;
|
|
56
|
-
|
|
57
|
-
isLoaded = false;
|
|
58
|
-
isInited = false;
|
|
59
|
-
|
|
60
|
-
#loadPromise!: StatefulPromise<SubscriptionGroupItem<T, D>>;
|
|
61
|
-
#initPromise!: StatefulPromise<SubscriptionGroupItem<T, D>>;
|
|
62
|
-
#unloadPromise!: StatefulPromise<SubscriptionGroupItem<T, D>>;
|
|
63
|
-
|
|
64
|
-
define(definition: Definition<T>, group: SubscriptionGroup | undefined = this.group) {
|
|
65
|
-
if (!group)
|
|
66
|
-
throw new Error("group is undefined");
|
|
67
|
-
|
|
68
|
-
const {
|
|
69
|
-
value,
|
|
70
|
-
type = "object",
|
|
71
|
-
publicationName,
|
|
72
|
-
params,
|
|
73
|
-
handle,
|
|
74
|
-
onBeforeInit,
|
|
75
|
-
debounce: debounceLimit = SubscriptionGroupItem.debounceLimit,
|
|
76
|
-
preventBind
|
|
77
|
-
} = definition;
|
|
78
|
-
|
|
79
|
-
if (this.group) {
|
|
80
|
-
if (
|
|
81
|
-
(!value || this.value === value) &&
|
|
82
|
-
this.type === type &&
|
|
83
|
-
this.publicationName === publicationName &&
|
|
84
|
-
JSON.stringify(this.params) === JSON.stringify(params) &&
|
|
85
|
-
this.handle === handle &&
|
|
86
|
-
this.onBeforeInit === onBeforeInit
|
|
87
|
-
)
|
|
88
|
-
return this.#applyOptions({ debounceLimit, preventBind });
|
|
89
|
-
|
|
90
|
-
this.detach();
|
|
91
|
-
delete this.value;
|
|
92
|
-
delete this.publicationName;
|
|
93
|
-
delete this.params;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
this.type = type;
|
|
97
|
-
this.handle = handle;
|
|
98
|
-
this.onBeforeInit = onBeforeInit;
|
|
99
|
-
if (value)
|
|
100
|
-
this.value = value;
|
|
101
|
-
else {
|
|
102
|
-
this.publicationName = publicationName ?? this.name;
|
|
103
|
-
this.params = params ?? [];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
this.#applyOptions({ debounceLimit, preventBind });
|
|
107
|
-
|
|
108
|
-
this.#attachTo(group);
|
|
109
|
-
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
#applyOptions({ debounceLimit, preventBind }: { debounceLimit?: number; preventBind?: boolean }) {
|
|
114
|
-
|
|
115
|
-
const emitUpdate: typeof this.emitUpdate = updated => {
|
|
116
|
-
this.emit("update", this.value, updated);
|
|
117
|
-
this.group?.emit(`update.${this.name}`, this.value, updated);
|
|
118
|
-
this.group?.emitUpdate();
|
|
119
|
-
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
if (typeof debounceLimit == "number") {
|
|
123
|
-
let debouncedUpdated: SubscriptionUpdated<T> | undefined;
|
|
124
|
-
let group: SubscriptionGroup;
|
|
125
|
-
|
|
126
|
-
const emitUpdateDebounced = debounce(() => {
|
|
127
|
-
|
|
128
|
-
const isDetached = !this.group;
|
|
129
|
-
|
|
130
|
-
if (isDetached)
|
|
131
|
-
this.group = group;
|
|
132
|
-
|
|
133
|
-
emitUpdate(debouncedUpdated!);
|
|
134
|
-
|
|
135
|
-
if (isDetached)
|
|
136
|
-
delete this.group;
|
|
137
|
-
|
|
138
|
-
debouncedUpdated = undefined;
|
|
139
|
-
|
|
140
|
-
}, debounceLimit);
|
|
141
|
-
|
|
142
|
-
this.emitUpdate = updated => {
|
|
143
|
-
if (updated !== undefined)
|
|
144
|
-
if (isUpdatedMapOrArray(updated))
|
|
145
|
-
if (isUpdatedMapOrArray(debouncedUpdated!))
|
|
146
|
-
for (const item of updated)
|
|
147
|
-
debouncedUpdated.push(item as any);
|
|
148
|
-
else
|
|
149
|
-
debouncedUpdated = Object.assign([ ...updated ], {
|
|
150
|
-
added: [ ...updated.added ],
|
|
151
|
-
deleted: [ ...updated.deleted ]
|
|
152
|
-
}) as SubscriptionUpdated<T>;
|
|
153
|
-
else if (typeof updated == "object")
|
|
154
|
-
if (debouncedUpdated)
|
|
155
|
-
Object.assign(debouncedUpdated, updated);
|
|
156
|
-
else
|
|
157
|
-
debouncedUpdated = { ...updated };
|
|
158
|
-
else
|
|
159
|
-
debouncedUpdated = updated;
|
|
160
|
-
|
|
161
|
-
group = this.group!;
|
|
162
|
-
|
|
163
|
-
return emitUpdateDebounced();
|
|
164
|
-
};
|
|
165
|
-
} else
|
|
166
|
-
this.emitUpdate = emitUpdate;
|
|
167
|
-
|
|
168
|
-
if (this.preventBind === undefined)
|
|
169
|
-
delete this.preventBind;
|
|
170
|
-
else
|
|
171
|
-
this.preventBind = preventBind;
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
#attachTo(group: SubscriptionGroup) {
|
|
176
|
-
if (!this.group) {
|
|
177
|
-
this.group = group;
|
|
178
|
-
|
|
179
|
-
this.group.items.push(this);
|
|
180
|
-
this.group.items[this.name] = this;
|
|
181
|
-
|
|
182
|
-
if (!this.#loadPromise || this.#loadPromise.isFulfilled)
|
|
183
|
-
this.#loadPromise = new StatefulPromise();
|
|
184
|
-
|
|
185
|
-
if (!this.#initPromise || this.#initPromise.isFulfilled)
|
|
186
|
-
this.#initPromise = new StatefulPromise();
|
|
187
|
-
|
|
188
|
-
if (!this.#unloadPromise || this.#unloadPromise.isFulfilled)
|
|
189
|
-
this.#unloadPromise = new StatefulPromise();
|
|
190
|
-
|
|
191
|
-
switch (this.type) {
|
|
192
|
-
case "array": {
|
|
193
|
-
const handleSubscription = this.#handleSubscription as HandleSubscription<"array">;
|
|
194
|
-
if (this.value)
|
|
195
|
-
(this.value as SubscriptionArrayWithSubscription)[setHandleUpdateSymbol](handleSubscription);
|
|
196
|
-
else
|
|
197
|
-
this.value = new SubscriptionArrayWithSubscription(this.publicationName!, this.params!, handleSubscription, false) as SubscriptionValue<T>;
|
|
198
|
-
break;
|
|
199
|
-
} case "map": {
|
|
200
|
-
const handleSubscription = this.#handleSubscription as HandleSubscription<"map">;
|
|
201
|
-
if (this.value)
|
|
202
|
-
(this.value as SubscriptionMapWithSubscription)[setHandleUpdateSymbol](handleSubscription);
|
|
203
|
-
else
|
|
204
|
-
this.value = new SubscriptionMapWithSubscription(this.publicationName!, this.params!, handleSubscription, false) as SubscriptionValue<T>;
|
|
205
|
-
break;
|
|
206
|
-
} default: {
|
|
207
|
-
const handleSubscription = this.#handleSubscription as HandleSubscription<"object">;
|
|
208
|
-
if (this.value)
|
|
209
|
-
(this.value as SubscriptionObjectWithSubscription)[setHandleUpdateSymbol](handleSubscription);
|
|
210
|
-
else
|
|
211
|
-
this.value = new SubscriptionObjectWithSubscription(this.publicationName!, this.params!, handleSubscription, false) as SubscriptionValue<T>;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
this.group.values.push(this.value);
|
|
216
|
-
this.group.values[this.name] = this.value;
|
|
217
|
-
|
|
218
|
-
if (this.onBeforeInit)
|
|
219
|
-
switch (this.type) {
|
|
220
|
-
case "array":
|
|
221
|
-
(this.onBeforeInit as SubscriptionOnBeforeInit<"array">).call(this.group, this.value as SubscriptionArrayWithSubscription);
|
|
222
|
-
break;
|
|
223
|
-
case "map":
|
|
224
|
-
(this.onBeforeInit as SubscriptionOnBeforeInit<"map">).call(this.group, this.value as SubscriptionMapWithSubscription);
|
|
225
|
-
break;
|
|
226
|
-
default:
|
|
227
|
-
(this.onBeforeInit as SubscriptionOnBeforeInit<"object">).call(this.group, this.value as SubscriptionObjectWithSubscription);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
detach() {
|
|
234
|
-
|
|
235
|
-
if (this.group) {
|
|
236
|
-
this.value?.[unsubscribeSymbol]();
|
|
237
|
-
|
|
238
|
-
this.isLoaded = false;
|
|
239
|
-
this.#unload();
|
|
240
|
-
|
|
241
|
-
delete this.group.values[this.name];
|
|
242
|
-
removeOne(this.group.values, this.value);
|
|
243
|
-
delete this.group.items[this.name];
|
|
244
|
-
removeOne(this.group.items, this);
|
|
245
|
-
|
|
246
|
-
delete this.group;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
#loadUpdated: null | SubscriptionUpdated<T> = null;
|
|
252
|
-
|
|
253
|
-
#handleSubscription: HandleSubscription<T> = (_, updated, updates) => {
|
|
254
|
-
if (updates)
|
|
255
|
-
if (this.group!.isLoaded) {
|
|
256
|
-
if (this.handle)
|
|
257
|
-
switch (this.type) {
|
|
258
|
-
case "array":
|
|
259
|
-
(this.handle as SubscriptionHandle<"array">).call(this.group!, updated as SubscriptionArrayUpdated, this.group!);
|
|
260
|
-
break;
|
|
261
|
-
case "map":
|
|
262
|
-
(this.handle as SubscriptionHandle<"map">).call(this.group!, updated as SubscriptionMapUpdated, this.group!);
|
|
263
|
-
break;
|
|
264
|
-
default:
|
|
265
|
-
(this.handle as SubscriptionHandle<"object">).call(this.group!, updated as SubscriptionObjectUpdated, this.group!);
|
|
266
|
-
}
|
|
267
|
-
this.emitUpdate(updated);
|
|
268
|
-
} else {
|
|
269
|
-
this.#loadUpdated = updated;
|
|
270
|
-
this.isLoaded = true;
|
|
271
|
-
privates.groupLoad!(this.group!);
|
|
272
|
-
}
|
|
273
|
-
else if (this.isLoaded) {
|
|
274
|
-
this.isLoaded = false;
|
|
275
|
-
privates.groupUnload!(this.group!);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (!this.isInited) {
|
|
279
|
-
this.isInited = true;
|
|
280
|
-
privates.groupInit!(this.group!);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
#load() {
|
|
286
|
-
|
|
287
|
-
if (this.#loadUpdated) {
|
|
288
|
-
if (this.handle)
|
|
289
|
-
switch (this.type) {
|
|
290
|
-
case "array":
|
|
291
|
-
(this.handle as SubscriptionHandle<"array">).call(this.group!, this.#loadUpdated as SubscriptionArrayUpdated, this.group!);
|
|
292
|
-
break;
|
|
293
|
-
case "map":
|
|
294
|
-
(this.handle as SubscriptionHandle<"map">).call(this.group!, this.#loadUpdated as SubscriptionMapUpdated, this.group!);
|
|
295
|
-
break;
|
|
296
|
-
default:
|
|
297
|
-
(this.handle as SubscriptionHandle<"object">).call(this.group!, this.#loadUpdated as SubscriptionObjectUpdated, this.group!);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
this.#loadUpdated = null;
|
|
301
|
-
|
|
302
|
-
this.emit("load", this.value);
|
|
303
|
-
|
|
304
|
-
this.#loadPromise.resolve(this);
|
|
305
|
-
|
|
306
|
-
if (this.#unloadPromise.isFulfilled)
|
|
307
|
-
this.#unloadPromise = new StatefulPromise();
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
#init() {
|
|
313
|
-
|
|
314
|
-
this.emit("init", this.value);
|
|
315
|
-
|
|
316
|
-
this.#initPromise.resolve(this);
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
#unload() {
|
|
321
|
-
|
|
322
|
-
if (this.handle)
|
|
323
|
-
switch (this.type) {
|
|
324
|
-
case "array":
|
|
325
|
-
(this.handle as SubscriptionHandle<"array">).call(this.group!, null, this.group!);
|
|
326
|
-
break;
|
|
327
|
-
case "map":
|
|
328
|
-
(this.handle as SubscriptionHandle<"map">).call(this.group!, null, this.group!);
|
|
329
|
-
break;
|
|
330
|
-
default:
|
|
331
|
-
(this.handle as SubscriptionHandle<"object">).call(this.group!, null, this.group!);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
this.emit("unload", this.value);
|
|
335
|
-
|
|
336
|
-
this.#unloadPromise.resolve(this);
|
|
337
|
-
|
|
338
|
-
if (this.#loadPromise.isFulfilled)
|
|
339
|
-
this.#loadPromise = new StatefulPromise();
|
|
340
|
-
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
loaded() {
|
|
344
|
-
return this.#loadPromise;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
inited() {
|
|
348
|
-
return this.#initPromise;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
unloaded() {
|
|
352
|
-
return this.#unloadPromise;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
subscribe() {
|
|
356
|
-
return this.value?.[subscribeSymbol]();
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
unsubscribe() {
|
|
360
|
-
return this.value?.[unsubscribeSymbol]();
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
renew(publicationName: Definition<T>["publicationName"], params: Definition<T>["params"]) {
|
|
364
|
-
return this.value?.[renewSymbol](publicationName, params);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
valueOf() {
|
|
368
|
-
return this.value;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
static debounceLimit = 4;
|
|
373
|
-
|
|
374
|
-
static {
|
|
375
|
-
|
|
376
|
-
privates.itemLoad = <
|
|
377
|
-
DP extends Definition<TP>,
|
|
378
|
-
TP extends SubscriptionType
|
|
379
|
-
>(item: SubscriptionGroupItem<TP, DP>) => item.#load();
|
|
380
|
-
privates.itemInit = <
|
|
381
|
-
DP extends Definition<TP>,
|
|
382
|
-
TP extends SubscriptionType
|
|
383
|
-
>(item: SubscriptionGroupItem<TP, DP>) => item.#init();
|
|
384
|
-
privates.itemUnload = <
|
|
385
|
-
DP extends Definition<TP>,
|
|
386
|
-
TP extends SubscriptionType
|
|
387
|
-
>(item: SubscriptionGroupItem<TP, DP>) => item.#unload();
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { SubscriptionGroup } from "./SubscriptionGroup";
|
|
2
|
-
import type { SubscriptionGroupItem } from "./SubscriptionGroupItem";
|
|
3
|
-
import type { SubscriptionType } from "./types";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export const privates: {
|
|
7
|
-
groupLoad?: (group: SubscriptionGroup) => void;
|
|
8
|
-
groupInit?: (group: SubscriptionGroup) => void;
|
|
9
|
-
groupUnload?: (group: SubscriptionGroup) => void;
|
|
10
|
-
itemLoad?: <T extends SubscriptionType>(item: SubscriptionGroupItem<T>) => void;
|
|
11
|
-
itemInit?: <T extends SubscriptionType>(item: SubscriptionGroupItem<T>) => void;
|
|
12
|
-
itemUnload?: <T extends SubscriptionType>(item: SubscriptionGroupItem<T>) => void;
|
|
13
|
-
} = {};
|