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