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