@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/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
- import { isEqual as O, modify as g, randomId as $, DefaultDataAdapter as N, Collection as y } from "@signaldb/core";
2
- function E(h, e, t = {}) {
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: o = !1, trailing: a = !0 } = t;
5
- function c(...r) {
6
- const i = o && !s, l = a && !s;
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, a && !i && (n = h.apply(this, r));
9
- }, e), i ? n = h.apply(this, r) : l || (n = null), 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 c;
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(h, e) {
50
- const t = [], s = Object.keys(h), n = Object.keys(e), o = /* @__PURE__ */ new Set([...s, ...n]);
51
- for (const a of o)
52
- if (e[a] !== h[a])
53
- if (typeof e[a] == "object" && typeof h[a] == "object" && e[a] != null && h[a] != null) {
54
- const c = C(h[a], e[a]);
55
- for (const r of c)
56
- t.push(`${a}.${r}`);
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(a);
58
+ t.push(o);
59
59
  return t;
60
60
  }
61
- function f(h, e) {
62
- const t = [], s = [], n = /* @__PURE__ */ new Map(), o = [], a = new Map(h.map((r) => [r.id, r])), c = new Map(e.map((r) => [r.id, r]));
63
- for (const [r, i] of a) {
64
- const l = c.get(r);
65
- l ? O(l, i) || (n.set(l.id, C(i, l)), s.push(l)) : o.push(i);
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 [r, i] of c)
68
- a.has(r) || t.push(i);
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: o
73
+ removed: a
74
74
  };
75
75
  }
76
- function w(h, e) {
76
+ function g(l, e) {
77
77
  if (e.items != null)
78
78
  return e.items;
79
- const t = h || [];
79
+ const t = l || [];
80
80
  return e.changes.added.forEach((s) => {
81
- const n = t.findIndex((o) => o.id === s.id);
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((o) => o.id === s.id);
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((o) => o.id === s.id);
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(h, e) {
92
- const t = new Map(h.map((s) => [s.id, s]));
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 ? g(n, s.data.modifier) : g({ id: s.data.id }, s.data.modifier));
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(h) {
106
- return h.added.length > 0 || h.modified.length > 0 || h.removed.length > 0;
105
+ function v(l) {
106
+ return l.added.length > 0 || l.modified.length > 0 || l.removed.length > 0;
107
107
  }
108
- function M(h, e) {
109
- return v(f(h, e));
108
+ function M(l, e) {
109
+ return v(f(l, e));
110
110
  }
111
- async function F({ changes: h, lastSnapshot: e, data: t, pull: s, push: n, insert: o, update: a, remove: c, batch: r }) {
112
- let i = t, l = e || [], p = w(e, i);
113
- if (h.length > 0) {
114
- const u = P(l, h);
115
- if (M(l, u)) {
116
- const S = P(p, h), m = f(p, S);
117
- v(m) && (await n(m), i = await s(), p = w(p, i)), l = u;
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(l, i.items) : i.changes;
121
- return await r(async () => {
122
- await Promise.all(d.added.map((u) => o(u))), await Promise.all(d.modified.map((u) => a(u.id, { $set: u }))), await Promise.all(d.removed.map((u) => c(u.id)));
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
- persistenceReady;
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.createStorageAdapter("changes"), n = this.createStorageAdapter("snapshots"), o = this.createStorageAdapter("sync-operations"), a = new N({
157
- storage: (c) => {
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`, a, {
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`, a, {
163
+ }), this.syncOperations = new y(`${this.options.id}-sync-operations`, s, {
183
164
  indices: ["collectionName", "status"],
184
165
  reactivity: t
185
- }), this.persistenceReady = Promise.all([
186
- this.syncOperations.isReady(),
187
- this.changes.isReady(),
188
- this.snapshots.isReady()
189
- ]).then(() => {
190
- }), this.changes.setMaxListeners(1e3), this.snapshots.setMaxListeners(1e3), this.syncOperations.setMaxListeners(1e3), this.debouncedFlush = E(this.flushScheduledPushes, this.options.debounceTime ?? 100);
191
- }
192
- createStorageAdapter(e) {
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 = (o) => {
257
- for (const a of this.remoteChanges)
258
- if (a != null && a.collectionName === o.collectionName && a.type === o.type && !(o.type === "remove" && a.data !== o.data) && a.data.id === o.data.id)
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 = (o, a) => {
262
- const c = [...this.remoteChanges];
263
- for (let r = 0; r < c.length; r += 1) {
264
- const i = c[r];
265
- i != null && i.collectionName === o && (i.type === "remove" && i.data !== a || i.data.id === a && (c[r] = null));
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 = c.filter((r) => r != null);
238
+ this.remoteChanges = r.filter((c) => c != null);
268
239
  };
269
- e.on("added", (o) => {
270
- if (s({ collectionName: t.name, type: "insert", data: o })) {
271
- n(t.name, o.id);
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: o
249
+ data: a
279
250
  }).then(() => {
280
251
  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);
252
+ }).catch((o) => {
253
+ this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, o);
283
254
  });
284
- }), e.on("changed", ({ id: o }, a) => {
285
- const c = { id: o, modifier: a };
286
- if (s({ collectionName: t.name, type: "update", data: c })) {
287
- n(t.name, o);
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: c
265
+ data: r
295
266
  }).then(() => {
296
267
  this.getCollectionProperties(t.name).syncPaused || this.schedulePush(t.name);
297
- }).catch((r) => {
298
- this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, r);
268
+ }).catch((c) => {
269
+ this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, c);
299
270
  });
300
- }), e.on("removed", ({ id: o }) => {
301
- if (s({ collectionName: t.name, type: "remove", data: o })) {
302
- n(t.name, o);
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: o
280
+ data: a
310
281
  }).then(() => {
311
282
  this.getCollectionProperties(t.name).syncPaused || this.schedulePush(t.name);
312
- }).catch((a) => {
313
- this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, a);
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((o) => {
316
- this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, o);
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 o = Date.now(), a = await this.syncOperations.insert({
350
- start: o,
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: a },
328
+ id: { $ne: o },
358
329
  collectionName: e,
359
330
  $or: [
360
- { end: { $lte: o } },
331
+ { end: { $lte: a } },
361
332
  { status: "active" }
362
333
  ]
363
- }), await this.syncOperations.updateOne({ id: a }, {
334
+ }), await this.syncOperations.updateOne({ id: o }, {
364
335
  $set: { status: "done", end: Date.now() }
365
336
  });
366
- }).catch(async (c) => {
367
- throw this.options.onError && this.options.onError(this.getCollectionProperties(e).options, c), await this.syncOperations.updateOne({ id: a }, {
368
- $set: { status: "error", end: Date.now(), error: c.stack || c.message }
369
- }), c;
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
- * @returns True if the collection is currently beeing synced, false otherwise.
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
- return this.syncOperations.findOne({
393
+ isSyncing(e, t) {
394
+ const s = this.syncOperations.findOne({
423
395
  ...e ? { collectionName: e } : {},
424
396
  status: "active"
425
- }, { fields: { status: 1 } }) != null;
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.persistenceReady;
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 o = this.syncOperations.find({
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
- }).count() > 0, a = Date.now();
454
- let c = null;
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 r = async () => {
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: a }
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
- o || (c = await this.syncOperations.insert({
475
- start: a,
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 l = await this.options.pull(s, {
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, l);
460
+ await this.syncWithData(e, h);
485
461
  };
486
- await (t?.force ? r() : this.getSyncQueue(e).add(r)).catch(async (i) => {
487
- throw c != null && (this.options.onError && this.options.onError(s, i), await this.syncOperations.updateOne({ id: c }, {
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
- }), c != null && (await this.syncOperations.removeMany({
491
- id: { $ne: c },
466
+ }), r != null && (await this.syncOperations.removeMany({
467
+ id: { $ne: r },
492
468
  collectionName: e,
493
469
  $or: [
494
- { end: { $lte: a } },
470
+ { end: { $lte: o } },
495
471
  { status: "active" }
496
472
  ]
497
- }), await this.syncOperations.updateOne({ id: c }, {
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), o = Date.now(), a = this.syncOperations.findOne({
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
- }), c = this.snapshots.findOne({
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
- }), r = this.changes.find({
498
+ reactive: !1,
499
+ async: !0
500
+ }), c = await this.changes.find({
523
501
  collectionName: e,
524
- time: { $lte: o }
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: r,
531
- lastSnapshot: c?.items,
509
+ changes: c,
510
+ lastSnapshot: r?.items,
532
511
  data: t,
533
512
  pull: () => this.options.pull(n, {
534
- lastFinishedSyncStart: a?.start,
535
- lastFinishedSyncEnd: a?.end
513
+ lastFinishedSyncStart: o?.start,
514
+ lastFinishedSyncEnd: o?.end
536
515
  }),
537
516
  push: (i) => this.options.push(n, {
538
517
  changes: i,
539
- rawChanges: r
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, l) => {
531
+ update: async (i, h) => {
553
532
  this.remoteChanges.push({
554
533
  collectionName: e,
555
534
  type: "insert",
556
- data: { id: i, ...l.$set }
535
+ data: { id: i, ...h.$set }
557
536
  }, {
558
537
  collectionName: e,
559
538
  type: "update",
560
- data: { id: i, modifier: l }
539
+ data: { id: i, modifier: h }
561
540
  }), await s.updateOne({ id: i }, {
562
- ...l,
541
+ ...h,
563
542
  $setOnInsert: { id: i }
564
543
  }, { upsert: !0 });
565
544
  },
566
545
  remove: async (i) => {
567
- s.findOne({
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: o }
560
+ time: { $lte: a }
582
561
  }), await this.changes.removeMany({
583
562
  collectionName: e,
584
- id: { $in: r.map((d) => d.id) }
563
+ id: { $in: c.map((d) => d.id) }
585
564
  }), await this.snapshots.insert({
586
- time: o,
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) => {