@signaldb/sync 2.0.0-beta.2 → 2.0.0-beta.21
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/SyncManager.d.ts +5 -5
- package/dist/applyChanges.d.ts +2 -2
- package/dist/computeChanges.d.ts +1 -1
- package/dist/getSnapshot.d.ts +2 -2
- package/dist/index.mjs +532 -597
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +3 -4
- package/dist/index.umd.js.map +1 -1
- package/dist/sync.d.ts +2 -2
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,606 +1,541 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { Collection as e, DefaultDataAdapter as t, isEqual as n, modify as r, randomId as i } from "@signaldb/core";
|
|
2
|
+
//#region src/utils/debounce.ts
|
|
3
|
+
function a(e, t, n = {}) {
|
|
4
|
+
let r, i, { leading: a = !1, trailing: o = !0 } = n;
|
|
5
|
+
function s(...n) {
|
|
6
|
+
let s = a && !r, c = o && !r;
|
|
7
|
+
return r && clearTimeout(r), r = setTimeout(() => {
|
|
8
|
+
r = null, o && !s && (i = e.apply(this, n));
|
|
9
|
+
}, t), s ? i = e.apply(this, n) : c || (i = null), i;
|
|
10
|
+
}
|
|
11
|
+
return s;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function C(l, e) {
|
|
50
|
-
const t = [], s = Object.keys(l), n = Object.keys(e), a = /* @__PURE__ */ new Set([...s, ...n]);
|
|
51
|
-
for (const o of a)
|
|
52
|
-
if (e[o] !== l[o])
|
|
53
|
-
if (typeof e[o] == "object" && typeof l[o] == "object" && e[o] != null && l[o] != null) {
|
|
54
|
-
const r = C(l[o], e[o]);
|
|
55
|
-
for (const c of r)
|
|
56
|
-
t.push(`${o}.${c}`);
|
|
57
|
-
} else
|
|
58
|
-
t.push(o);
|
|
59
|
-
return t;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/utils/PromiseQueue.ts
|
|
15
|
+
var o = class {
|
|
16
|
+
queue = [];
|
|
17
|
+
pendingPromise = !1;
|
|
18
|
+
add(e) {
|
|
19
|
+
return new Promise((t, n) => {
|
|
20
|
+
this.queue.push(() => e().then(t).catch((e) => {
|
|
21
|
+
throw n(e), e;
|
|
22
|
+
})), this.dequeue();
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
hasPendingPromise() {
|
|
26
|
+
return this.pendingPromise;
|
|
27
|
+
}
|
|
28
|
+
dequeue() {
|
|
29
|
+
if (this.pendingPromise || this.queue.length === 0) return;
|
|
30
|
+
let e = this.queue.shift();
|
|
31
|
+
e && (this.pendingPromise = !0, e().then(() => {
|
|
32
|
+
this.pendingPromise = !1, this.dequeue();
|
|
33
|
+
}).catch(() => {
|
|
34
|
+
this.pendingPromise = !1, this.dequeue();
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/computeChanges.ts
|
|
40
|
+
function s(e, t) {
|
|
41
|
+
let n = [], r = Object.keys(e), i = Object.keys(t), a = new Set([...r, ...i]);
|
|
42
|
+
for (let r of a) if (t[r] !== e[r]) if (typeof t[r] == "object" && typeof e[r] == "object" && t[r] != null && e[r] != null) {
|
|
43
|
+
let i = s(e[r], t[r]);
|
|
44
|
+
for (let e of i) n.push(`${r}.${e}`);
|
|
45
|
+
} else n.push(r);
|
|
46
|
+
return n;
|
|
60
47
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
};
|
|
48
|
+
function c(e, t) {
|
|
49
|
+
let r = [], i = [], a = /* @__PURE__ */ new Map(), o = [], c = new Map(e.map((e) => [e.id, e])), l = new Map(t.map((e) => [e.id, e]));
|
|
50
|
+
for (let [e, t] of c) {
|
|
51
|
+
let r = l.get(e);
|
|
52
|
+
r ? n(r, t) || (a.set(r.id, s(t, r)), i.push(r)) : o.push(t);
|
|
53
|
+
}
|
|
54
|
+
for (let [e, t] of l) c.has(e) || r.push(t);
|
|
55
|
+
return {
|
|
56
|
+
added: r,
|
|
57
|
+
modified: i,
|
|
58
|
+
modifiedFields: a,
|
|
59
|
+
removed: o
|
|
60
|
+
};
|
|
75
61
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/getSnapshot.ts
|
|
64
|
+
function l(e, t) {
|
|
65
|
+
if (t.items != null) return t.items;
|
|
66
|
+
let n = e || [];
|
|
67
|
+
return t.changes.added.forEach((e) => {
|
|
68
|
+
let t = n.findIndex((t) => t.id === e.id);
|
|
69
|
+
t === -1 ? n.push(e) : n[t] = e;
|
|
70
|
+
}), t.changes.modified.forEach((e) => {
|
|
71
|
+
let t = n.findIndex((t) => t.id === e.id);
|
|
72
|
+
t === -1 ? n.push(e) : n[t] = e;
|
|
73
|
+
}), t.changes.removed.forEach((e) => {
|
|
74
|
+
let t = n.findIndex((t) => t.id === e.id);
|
|
75
|
+
t !== -1 && n.splice(t, 1);
|
|
76
|
+
}), n;
|
|
90
77
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/applyChanges.ts
|
|
80
|
+
function u(e, t) {
|
|
81
|
+
let n = new Map(e.map((e) => [e.id, e]));
|
|
82
|
+
return t.forEach((e) => {
|
|
83
|
+
if (e.type === "remove") n.delete(e.data);
|
|
84
|
+
else if (e.type === "insert") {
|
|
85
|
+
let t = n.get(e.data.id);
|
|
86
|
+
n.set(e.data.id, t ? {
|
|
87
|
+
...t,
|
|
88
|
+
...e.data
|
|
89
|
+
} : e.data);
|
|
90
|
+
} else {
|
|
91
|
+
let t = n.get(e.data.id);
|
|
92
|
+
n.set(e.data.id, r(t || { id: e.data.id }, e.data.modifier));
|
|
93
|
+
}
|
|
94
|
+
}), [...n.values()];
|
|
104
95
|
}
|
|
105
|
-
|
|
106
|
-
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/sync.ts
|
|
98
|
+
function d(e) {
|
|
99
|
+
return e.added.length > 0 || e.modified.length > 0 || e.removed.length > 0;
|
|
107
100
|
}
|
|
108
|
-
function
|
|
109
|
-
|
|
101
|
+
function f(e, t) {
|
|
102
|
+
return d(c(e, t));
|
|
110
103
|
}
|
|
111
|
-
async function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
104
|
+
async function p({ changes: e, lastSnapshot: t, data: n, pull: r, push: i, insert: a, update: o, remove: s, batch: p }) {
|
|
105
|
+
let m = n, h = t || [], g = l(t, m);
|
|
106
|
+
if (e.length > 0) {
|
|
107
|
+
let t = u(h, e);
|
|
108
|
+
if (f(h, t)) {
|
|
109
|
+
let n = u(g, e), a = c(g, n);
|
|
110
|
+
d(a) && (await i(a), m = await r(), g = l(g, m)), h = t;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
let _ = m.changes == null ? c(h, m.items) : m.changes;
|
|
114
|
+
return await p(async () => {
|
|
115
|
+
await Promise.all(_.added.map((e) => a(e))), await Promise.all(_.modified.map((e) => o(e.id, { $set: e }))), await Promise.all(_.removed.map((e) => s(e.id)));
|
|
116
|
+
}), g;
|
|
124
117
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
},
|
|
545
|
-
remove: async (i) => {
|
|
546
|
-
await s.find({
|
|
547
|
-
id: i
|
|
548
|
-
}, { reactive: !1, async: !0 }).count() > 0 && (this.remoteChanges.push({
|
|
549
|
-
collectionName: e,
|
|
550
|
-
type: "remove",
|
|
551
|
-
data: i
|
|
552
|
-
}), await s.removeOne({ id: i }));
|
|
553
|
-
},
|
|
554
|
-
batch: async (i) => s.batch(async () => {
|
|
555
|
-
await i();
|
|
556
|
-
})
|
|
557
|
-
}).then(async (i) => {
|
|
558
|
-
if (await this.snapshots.removeMany({
|
|
559
|
-
collectionName: e,
|
|
560
|
-
time: { $lte: a }
|
|
561
|
-
}), await this.changes.removeMany({
|
|
562
|
-
collectionName: e,
|
|
563
|
-
id: { $in: c.map((d) => d.id) }
|
|
564
|
-
}), await this.snapshots.insert({
|
|
565
|
-
time: a,
|
|
566
|
-
collectionName: e,
|
|
567
|
-
items: i
|
|
568
|
-
}), await new Promise((d) => {
|
|
569
|
-
setTimeout(d, 0);
|
|
570
|
-
}), await this.changes.find({
|
|
571
|
-
collectionName: e
|
|
572
|
-
}, { reactive: !1, async: !0 }).count() > 0) {
|
|
573
|
-
await this.sync(e, {
|
|
574
|
-
force: !0,
|
|
575
|
-
onlyWithChanges: !0
|
|
576
|
-
});
|
|
577
|
-
return;
|
|
578
|
-
}
|
|
579
|
-
const p = await s.find({
|
|
580
|
-
id: { $nin: i.map((d) => d.id) }
|
|
581
|
-
}, {
|
|
582
|
-
reactive: !1,
|
|
583
|
-
async: !0
|
|
584
|
-
}).map((d) => d.id);
|
|
585
|
-
await s.batch(async () => {
|
|
586
|
-
await Promise.all(i.map(async (d) => {
|
|
587
|
-
this.remoteChanges.push({
|
|
588
|
-
collectionName: e,
|
|
589
|
-
type: "insert",
|
|
590
|
-
data: d
|
|
591
|
-
}, {
|
|
592
|
-
collectionName: e,
|
|
593
|
-
type: "update",
|
|
594
|
-
data: { id: d.id, modifier: { $set: d } }
|
|
595
|
-
}), await s.replaceOne({ id: d.id }, d, { upsert: !0 });
|
|
596
|
-
})), await Promise.all(p.map(async (d) => {
|
|
597
|
-
await s.removeOne({ id: d });
|
|
598
|
-
}));
|
|
599
|
-
});
|
|
600
|
-
});
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
export {
|
|
604
|
-
b as SyncManager
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/SyncManager.ts
|
|
120
|
+
var m = class {
|
|
121
|
+
options;
|
|
122
|
+
collections = /* @__PURE__ */ new Map();
|
|
123
|
+
changes;
|
|
124
|
+
snapshots;
|
|
125
|
+
syncOperations;
|
|
126
|
+
scheduledPushes = /* @__PURE__ */ new Set();
|
|
127
|
+
remoteChanges = [];
|
|
128
|
+
syncQueues = /* @__PURE__ */ new Map();
|
|
129
|
+
collectionsReady;
|
|
130
|
+
isDisposed = !1;
|
|
131
|
+
instanceId = i();
|
|
132
|
+
id;
|
|
133
|
+
debouncedFlush;
|
|
134
|
+
constructor(n) {
|
|
135
|
+
this.options = {
|
|
136
|
+
autostart: !0,
|
|
137
|
+
...n
|
|
138
|
+
}, this.id = this.options.id || "default-sync-manager";
|
|
139
|
+
let { reactivity: r } = this.options, i = this.options.dataAdapter ?? new t();
|
|
140
|
+
this.changes = new e(`${this.options.id}-changes`, i, {
|
|
141
|
+
indices: ["collectionName"],
|
|
142
|
+
reactivity: r
|
|
143
|
+
}), this.snapshots = new e(`${this.options.id}-snapshots`, i, {
|
|
144
|
+
indices: ["collectionName"],
|
|
145
|
+
reactivity: r
|
|
146
|
+
}), this.syncOperations = new e(`${this.options.id}-sync-operations`, i, {
|
|
147
|
+
indices: ["collectionName", "status"],
|
|
148
|
+
reactivity: r
|
|
149
|
+
});
|
|
150
|
+
let o = [
|
|
151
|
+
Promise.resolve(this.syncOperations.isReady()),
|
|
152
|
+
Promise.resolve(this.changes.isReady()),
|
|
153
|
+
Promise.resolve(this.snapshots.isReady())
|
|
154
|
+
];
|
|
155
|
+
this.collectionsReady = Promise.all(o).then(() => {}), this.changes.setMaxListeners(1e3), this.snapshots.setMaxListeners(1e3), this.syncOperations.setMaxListeners(1e3), this.debouncedFlush = a(this.flushScheduledPushes, this.options.debounceTime ?? 100);
|
|
156
|
+
}
|
|
157
|
+
getSyncQueue(e) {
|
|
158
|
+
return this.syncQueues.get(e) ?? this.syncQueues.set(e, new o()), this.syncQueues.get(e);
|
|
159
|
+
}
|
|
160
|
+
async dispose() {
|
|
161
|
+
this.isDisposed = !0, this.collections.clear(), this.syncQueues.clear(), this.remoteChanges.splice(0), await Promise.all([
|
|
162
|
+
this.changes.dispose(),
|
|
163
|
+
this.snapshots.dispose(),
|
|
164
|
+
this.syncOperations.dispose()
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
getCollection(e) {
|
|
168
|
+
let { collection: t, options: n } = this.getCollectionProperties(e);
|
|
169
|
+
return [t, n];
|
|
170
|
+
}
|
|
171
|
+
getCollectionProperties(e) {
|
|
172
|
+
let t = this.collections.get(e);
|
|
173
|
+
if (t == null) throw Error(`Collection with id '${e}' not found`);
|
|
174
|
+
return t;
|
|
175
|
+
}
|
|
176
|
+
addCollection(e, t) {
|
|
177
|
+
if (this.isDisposed) throw Error("SyncManager is disposed");
|
|
178
|
+
this.collections.set(t.name, {
|
|
179
|
+
collection: e,
|
|
180
|
+
options: t,
|
|
181
|
+
readyPromise: e.ready(),
|
|
182
|
+
syncPaused: !0
|
|
183
|
+
});
|
|
184
|
+
let n = (e) => {
|
|
185
|
+
for (let t of this.remoteChanges) if (t != null && t.collectionName === e.collectionName && t.type === e.type && !(e.type === "remove" && t.data !== e.data) && t.data.id === e.data.id) return !0;
|
|
186
|
+
return !1;
|
|
187
|
+
}, r = (e, t) => {
|
|
188
|
+
let n = [...this.remoteChanges];
|
|
189
|
+
for (let r = 0; r < n.length; r += 1) {
|
|
190
|
+
let i = n[r];
|
|
191
|
+
i != null && i.collectionName === e && (i.type === "remove" && i.data !== t || i.data.id === t && (n[r] = null));
|
|
192
|
+
}
|
|
193
|
+
this.remoteChanges = n.filter((e) => e != null);
|
|
194
|
+
};
|
|
195
|
+
e.on("added", (e) => {
|
|
196
|
+
if (!this.isDisposed) {
|
|
197
|
+
if (n({
|
|
198
|
+
collectionName: t.name,
|
|
199
|
+
type: "insert",
|
|
200
|
+
data: e
|
|
201
|
+
})) {
|
|
202
|
+
r(t.name, e.id);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
this.changes.insert({
|
|
206
|
+
collectionName: t.name,
|
|
207
|
+
time: Date.now(),
|
|
208
|
+
type: "insert",
|
|
209
|
+
data: e
|
|
210
|
+
}).then(() => {
|
|
211
|
+
this.getCollectionProperties(t.name).syncPaused || this.schedulePush(t.name);
|
|
212
|
+
}).catch((e) => {
|
|
213
|
+
this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, e);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}), e.on("changed", ({ id: e }, i) => {
|
|
217
|
+
if (this.isDisposed) return;
|
|
218
|
+
let a = {
|
|
219
|
+
id: e,
|
|
220
|
+
modifier: i
|
|
221
|
+
};
|
|
222
|
+
if (n({
|
|
223
|
+
collectionName: t.name,
|
|
224
|
+
type: "update",
|
|
225
|
+
data: a
|
|
226
|
+
})) {
|
|
227
|
+
r(t.name, e);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
this.changes.insert({
|
|
231
|
+
collectionName: t.name,
|
|
232
|
+
time: Date.now(),
|
|
233
|
+
type: "update",
|
|
234
|
+
data: a
|
|
235
|
+
}).then(() => {
|
|
236
|
+
this.getCollectionProperties(t.name).syncPaused || this.schedulePush(t.name);
|
|
237
|
+
}).catch((e) => {
|
|
238
|
+
this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, e);
|
|
239
|
+
});
|
|
240
|
+
}), e.on("removed", ({ id: e }) => {
|
|
241
|
+
if (!this.isDisposed) {
|
|
242
|
+
if (n({
|
|
243
|
+
collectionName: t.name,
|
|
244
|
+
type: "remove",
|
|
245
|
+
data: e
|
|
246
|
+
})) {
|
|
247
|
+
r(t.name, e);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
this.changes.insert({
|
|
251
|
+
collectionName: t.name,
|
|
252
|
+
time: Date.now(),
|
|
253
|
+
type: "remove",
|
|
254
|
+
data: e
|
|
255
|
+
}).then(() => {
|
|
256
|
+
this.getCollectionProperties(t.name).syncPaused || this.schedulePush(t.name);
|
|
257
|
+
}).catch((e) => {
|
|
258
|
+
this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, e);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}), this.options.autostart && this.startSync(t.name).catch((e) => {
|
|
262
|
+
this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, e);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
flushScheduledPushes() {
|
|
266
|
+
this.scheduledPushes.forEach((e) => {
|
|
267
|
+
this.pushChanges(e).catch(() => {});
|
|
268
|
+
}), this.scheduledPushes.clear();
|
|
269
|
+
}
|
|
270
|
+
schedulePush(e) {
|
|
271
|
+
this.scheduledPushes.add(e), this.debouncedFlush();
|
|
272
|
+
}
|
|
273
|
+
async startAll() {
|
|
274
|
+
await Promise.all([...this.collections.keys()].map((e) => this.startSync(e)));
|
|
275
|
+
}
|
|
276
|
+
async startSync(e) {
|
|
277
|
+
let t = this.getCollectionProperties(e);
|
|
278
|
+
if (!t.syncPaused) return;
|
|
279
|
+
this.schedulePush(e);
|
|
280
|
+
let n = this.options.registerRemoteChange ? await this.options.registerRemoteChange(t.options, async (t) => {
|
|
281
|
+
this.isDisposed || (t == null ? await this.sync(e) : await this.getSyncQueue(e).add(async () => {
|
|
282
|
+
let n = Date.now(), r = await this.syncOperations.insert({
|
|
283
|
+
start: n,
|
|
284
|
+
collectionName: e,
|
|
285
|
+
instanceId: this.instanceId,
|
|
286
|
+
status: "active"
|
|
287
|
+
});
|
|
288
|
+
await this.syncWithData(e, t).then(async () => {
|
|
289
|
+
this.isDisposed || (await this.syncOperations.removeMany({
|
|
290
|
+
id: { $ne: r },
|
|
291
|
+
collectionName: e,
|
|
292
|
+
$or: [{ end: { $lte: n } }, { status: "active" }]
|
|
293
|
+
}), await this.syncOperations.updateOne({ id: r }, { $set: {
|
|
294
|
+
status: "done",
|
|
295
|
+
end: Date.now()
|
|
296
|
+
} }));
|
|
297
|
+
}).catch(async (t) => {
|
|
298
|
+
throw this.options.onError && this.options.onError(this.getCollectionProperties(e).options, t), await this.syncOperations.updateOne({ id: r }, { $set: {
|
|
299
|
+
status: "error",
|
|
300
|
+
end: Date.now(),
|
|
301
|
+
error: t.stack || t.message
|
|
302
|
+
} }), t;
|
|
303
|
+
});
|
|
304
|
+
}));
|
|
305
|
+
}) : void 0;
|
|
306
|
+
this.collections.set(e, {
|
|
307
|
+
...t,
|
|
308
|
+
syncPaused: !1,
|
|
309
|
+
cleanupFunction: n
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
async pauseAll() {
|
|
313
|
+
await Promise.all([...this.collections.keys()].map((e) => this.pauseSync(e)));
|
|
314
|
+
}
|
|
315
|
+
async pauseSync(e) {
|
|
316
|
+
let t = this.getCollectionProperties(e);
|
|
317
|
+
t.syncPaused || (t.cleanupFunction && await t.cleanupFunction(), this.collections.set(e, {
|
|
318
|
+
...t,
|
|
319
|
+
cleanupFunction: void 0,
|
|
320
|
+
syncPaused: !0
|
|
321
|
+
}));
|
|
322
|
+
}
|
|
323
|
+
async syncAll() {
|
|
324
|
+
if (this.isDisposed) throw Error("SyncManager is disposed");
|
|
325
|
+
let e = [];
|
|
326
|
+
if (await Promise.all([...this.collections.keys()].map((t) => this.sync(t).catch((n) => {
|
|
327
|
+
e.push({
|
|
328
|
+
id: t,
|
|
329
|
+
error: n
|
|
330
|
+
});
|
|
331
|
+
}))), e.length > 0) throw Error(`Error while syncing collections:\n${e.map((e) => `${e.id}: ${e.error.message}`).join("\n\n")}`);
|
|
332
|
+
}
|
|
333
|
+
isSyncing(e, t) {
|
|
334
|
+
let n = this.syncOperations.findOne({
|
|
335
|
+
...e ? { collectionName: e } : {},
|
|
336
|
+
status: "active"
|
|
337
|
+
}, {
|
|
338
|
+
fields: { status: 1 },
|
|
339
|
+
async: t
|
|
340
|
+
});
|
|
341
|
+
return n instanceof Promise ? n.then((e) => e != null) : n != null;
|
|
342
|
+
}
|
|
343
|
+
async isReady() {
|
|
344
|
+
await this.collectionsReady;
|
|
345
|
+
}
|
|
346
|
+
async sync(e, t = {}) {
|
|
347
|
+
if (this.isDisposed) throw Error("SyncManager is disposed");
|
|
348
|
+
await this.isReady();
|
|
349
|
+
let { options: n, readyPromise: r } = this.getCollectionProperties(e);
|
|
350
|
+
await r;
|
|
351
|
+
let i = await this.syncOperations.find({
|
|
352
|
+
collectionName: e,
|
|
353
|
+
instanceId: this.instanceId,
|
|
354
|
+
status: "active"
|
|
355
|
+
}, {
|
|
356
|
+
reactive: !1,
|
|
357
|
+
async: !0
|
|
358
|
+
}).count() > 0, a = Date.now(), o = null;
|
|
359
|
+
await new Promise((e) => {
|
|
360
|
+
setTimeout(e, 0);
|
|
361
|
+
});
|
|
362
|
+
let s = async () => {
|
|
363
|
+
let r = await this.syncOperations.findOne({
|
|
364
|
+
collectionName: e,
|
|
365
|
+
status: "done"
|
|
366
|
+
}, {
|
|
367
|
+
sort: { end: -1 },
|
|
368
|
+
reactive: !1,
|
|
369
|
+
async: !0
|
|
370
|
+
});
|
|
371
|
+
if (t?.onlyWithChanges && await this.changes.find({
|
|
372
|
+
collectionName: e,
|
|
373
|
+
time: { $lte: a }
|
|
374
|
+
}, {
|
|
375
|
+
sort: { time: 1 },
|
|
376
|
+
reactive: !1,
|
|
377
|
+
async: !0
|
|
378
|
+
}).count() === 0) return;
|
|
379
|
+
i || (o = await this.syncOperations.insert({
|
|
380
|
+
start: a,
|
|
381
|
+
collectionName: e,
|
|
382
|
+
instanceId: this.instanceId,
|
|
383
|
+
status: "active"
|
|
384
|
+
}));
|
|
385
|
+
let s = await this.options.pull(n, {
|
|
386
|
+
lastFinishedSyncStart: r?.start,
|
|
387
|
+
lastFinishedSyncEnd: r?.end
|
|
388
|
+
});
|
|
389
|
+
await this.syncWithData(e, s);
|
|
390
|
+
};
|
|
391
|
+
await (t?.force ? s() : this.getSyncQueue(e).add(s)).catch(async (e) => {
|
|
392
|
+
throw o != null && (this.options.onError && this.options.onError(n, e), await this.syncOperations.updateOne({ id: o }, { $set: {
|
|
393
|
+
status: "error",
|
|
394
|
+
end: Date.now(),
|
|
395
|
+
error: e.stack || e.message
|
|
396
|
+
} })), e;
|
|
397
|
+
}), o != null && (await this.syncOperations.removeMany({
|
|
398
|
+
id: { $ne: o },
|
|
399
|
+
collectionName: e,
|
|
400
|
+
$or: [{ end: { $lte: a } }, { status: "active" }]
|
|
401
|
+
}), await this.syncOperations.updateOne({ id: o }, { $set: {
|
|
402
|
+
status: "done",
|
|
403
|
+
end: Date.now()
|
|
404
|
+
} }));
|
|
405
|
+
}
|
|
406
|
+
async pushChanges(e) {
|
|
407
|
+
await this.sync(e, { onlyWithChanges: !0 });
|
|
408
|
+
}
|
|
409
|
+
async syncWithData(e, t) {
|
|
410
|
+
if (this.isDisposed) return;
|
|
411
|
+
let { collection: n, options: r } = this.getCollectionProperties(e), i = Date.now(), a = await this.syncOperations.findOne({
|
|
412
|
+
collectionName: e,
|
|
413
|
+
status: "done"
|
|
414
|
+
}, {
|
|
415
|
+
sort: { end: -1 },
|
|
416
|
+
reactive: !1,
|
|
417
|
+
async: !0
|
|
418
|
+
}), o = await this.snapshots.findOne({ collectionName: e }, {
|
|
419
|
+
sort: { time: -1 },
|
|
420
|
+
reactive: !1,
|
|
421
|
+
async: !0
|
|
422
|
+
}), s = await this.changes.find({
|
|
423
|
+
collectionName: e,
|
|
424
|
+
time: { $lte: i }
|
|
425
|
+
}, {
|
|
426
|
+
sort: { time: 1 },
|
|
427
|
+
reactive: !1,
|
|
428
|
+
async: !0
|
|
429
|
+
}).fetch();
|
|
430
|
+
await p({
|
|
431
|
+
changes: s,
|
|
432
|
+
lastSnapshot: o?.items,
|
|
433
|
+
data: t,
|
|
434
|
+
pull: () => this.options.pull(r, {
|
|
435
|
+
lastFinishedSyncStart: a?.start,
|
|
436
|
+
lastFinishedSyncEnd: a?.end
|
|
437
|
+
}),
|
|
438
|
+
push: (e) => this.options.push(r, {
|
|
439
|
+
changes: e,
|
|
440
|
+
rawChanges: s
|
|
441
|
+
}),
|
|
442
|
+
insert: async (t) => {
|
|
443
|
+
this.remoteChanges.push({
|
|
444
|
+
collectionName: e,
|
|
445
|
+
type: "insert",
|
|
446
|
+
data: t
|
|
447
|
+
}, {
|
|
448
|
+
collectionName: e,
|
|
449
|
+
type: "update",
|
|
450
|
+
data: {
|
|
451
|
+
id: t.id,
|
|
452
|
+
modifier: { $set: t }
|
|
453
|
+
}
|
|
454
|
+
}), await n.replaceOne({ id: t.id }, t, { upsert: !0 });
|
|
455
|
+
},
|
|
456
|
+
update: async (t, r) => {
|
|
457
|
+
this.remoteChanges.push({
|
|
458
|
+
collectionName: e,
|
|
459
|
+
type: "insert",
|
|
460
|
+
data: {
|
|
461
|
+
id: t,
|
|
462
|
+
...r.$set
|
|
463
|
+
}
|
|
464
|
+
}, {
|
|
465
|
+
collectionName: e,
|
|
466
|
+
type: "update",
|
|
467
|
+
data: {
|
|
468
|
+
id: t,
|
|
469
|
+
modifier: r
|
|
470
|
+
}
|
|
471
|
+
}), await n.updateOne({ id: t }, {
|
|
472
|
+
...r,
|
|
473
|
+
$setOnInsert: { id: t }
|
|
474
|
+
}, { upsert: !0 });
|
|
475
|
+
},
|
|
476
|
+
remove: async (t) => {
|
|
477
|
+
await n.find({ id: t }, {
|
|
478
|
+
reactive: !1,
|
|
479
|
+
async: !0
|
|
480
|
+
}).count() > 0 && (this.remoteChanges.push({
|
|
481
|
+
collectionName: e,
|
|
482
|
+
type: "remove",
|
|
483
|
+
data: t
|
|
484
|
+
}), await n.removeOne({ id: t }));
|
|
485
|
+
},
|
|
486
|
+
batch: async (e) => n.batch(async () => {
|
|
487
|
+
await e();
|
|
488
|
+
})
|
|
489
|
+
}).then(async (t) => {
|
|
490
|
+
if (this.isDisposed) return;
|
|
491
|
+
if (await this.snapshots.removeMany({
|
|
492
|
+
collectionName: e,
|
|
493
|
+
time: { $lte: i }
|
|
494
|
+
}), await this.changes.removeMany({
|
|
495
|
+
collectionName: e,
|
|
496
|
+
id: { $in: s.map((e) => e.id) }
|
|
497
|
+
}), await this.snapshots.insert({
|
|
498
|
+
time: i,
|
|
499
|
+
collectionName: e,
|
|
500
|
+
items: t
|
|
501
|
+
}), await new Promise((e) => {
|
|
502
|
+
setTimeout(e, 0);
|
|
503
|
+
}), await this.changes.find({ collectionName: e }, {
|
|
504
|
+
reactive: !1,
|
|
505
|
+
async: !0
|
|
506
|
+
}).count() > 0) {
|
|
507
|
+
await this.sync(e, {
|
|
508
|
+
force: !0,
|
|
509
|
+
onlyWithChanges: !0
|
|
510
|
+
});
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
let r = await n.find({ id: { $nin: t.map((e) => e.id) } }, {
|
|
514
|
+
reactive: !1,
|
|
515
|
+
async: !0
|
|
516
|
+
}).map((e) => e.id);
|
|
517
|
+
await n.batch(async () => {
|
|
518
|
+
await Promise.all(t.map(async (t) => {
|
|
519
|
+
this.remoteChanges.push({
|
|
520
|
+
collectionName: e,
|
|
521
|
+
type: "insert",
|
|
522
|
+
data: t
|
|
523
|
+
}, {
|
|
524
|
+
collectionName: e,
|
|
525
|
+
type: "update",
|
|
526
|
+
data: {
|
|
527
|
+
id: t.id,
|
|
528
|
+
modifier: { $set: t }
|
|
529
|
+
}
|
|
530
|
+
}), await n.replaceOne({ id: t.id }, t, { upsert: !0 });
|
|
531
|
+
})), await Promise.all(r.map(async (e) => {
|
|
532
|
+
await n.removeOne({ id: e });
|
|
533
|
+
}));
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
}
|
|
605
537
|
};
|
|
606
|
-
//#
|
|
538
|
+
//#endregion
|
|
539
|
+
export { m as SyncManager };
|
|
540
|
+
|
|
541
|
+
//# sourceMappingURL=index.mjs.map
|