@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/index.mjs CHANGED
@@ -1,23 +1,18 @@
1
- var E = Object.defineProperty;
2
- var M = (c, e, t) => e in c ? E(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
- var u = (c, e, t) => M(c, typeof e != "symbol" ? e + "" : e, t);
4
- import { isEqual as S, modify as C, randomId as D, Collection as m, createIndex as y } from "@signaldb/core";
5
- function x(c, e, t = {}) {
6
- let s, n;
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 && (n = c.apply(this, h));
12
- }, e), i ? n = c.apply(this, h) : l || (n = null), n;
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 R {
17
- constructor() {
18
- u(this, "queue", []);
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((n) => {
29
- throw s(n), n;
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 N(c, e) {
55
- const t = [], s = Object.keys(c), n = Object.keys(e), o = /* @__PURE__ */ new Set([...s, ...n]);
56
- for (const a of o)
57
- if (e[a] !== c[a])
58
- if (typeof e[a] == "object" && typeof c[a] == "object" && e[a] != null && c[a] != null) {
59
- const r = N(c[a], e[a]);
60
- for (const h of r)
61
- t.push(`${a}.${h}`);
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 g(c, e) {
67
- const t = [], s = [], n = /* @__PURE__ */ new Map(), o = [], a = new Map(c.map((h) => [h.id, h])), r = new Map(e.map((h) => [h.id, h]));
68
- for (const [h, i] of a) {
69
- const l = r.get(h);
70
- l ? S(l, i) || (n.set(l.id, N(i, l)), s.push(l)) : o.push(i);
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 [h, i] of r)
73
- a.has(h) || t.push(i);
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: n,
78
- removed: o
72
+ modifiedFields: o,
73
+ removed: n
79
74
  };
80
75
  }
81
- function P(c, e) {
76
+ function g(l, e) {
82
77
  if (e.items != null)
83
78
  return e.items;
84
- const t = c || [];
79
+ const t = l || [];
85
80
  return e.changes.added.forEach((s) => {
86
- const n = t.findIndex((o) => o.id === s.id);
87
- n === -1 ? t.push(s) : t[n] = s;
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 n = t.findIndex((o) => o.id === s.id);
90
- n === -1 ? t.push(s) : t[n] = s;
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 n = t.findIndex((o) => o.id === s.id);
93
- n !== -1 && t.splice(n, 1);
87
+ const o = t.findIndex((n) => n.id === s.id);
88
+ o !== -1 && t.splice(o, 1);
94
89
  }), t;
95
90
  }
96
- function v(c, e) {
97
- const t = new Map(c.map((s) => [s.id, s]));
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 n = t.get(s.data.id);
103
- t.set(s.data.id, n ? { ...n, ...s.data } : s.data);
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 n = t.get(s.data.id);
106
- t.set(s.data.id, n ? C(n, s.data.modifier) : C({ id: s.data.id }, s.data.modifier));
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 O(c) {
111
- return c.added.length > 0 || c.modified.length > 0 || c.removed.length > 0;
105
+ function v(l) {
106
+ return l.added.length > 0 || l.modified.length > 0 || l.removed.length > 0;
112
107
  }
113
- function b(c, e) {
114
- return O(g(c, e));
108
+ function M(l, e) {
109
+ return v(f(l, e));
115
110
  }
116
- async function I({ changes: c, lastSnapshot: e, data: t, pull: s, push: n, insert: o, update: a, remove: r, batch: h }) {
117
- let i = t, l = e || [], f = P(e, i);
118
- if (c.length > 0) {
119
- const p = v(l, c);
120
- if (b(l, p)) {
121
- const $ = v(f, c), w = g(f, $);
122
- O(w) && (await n(w), i = await s(), f = P(f, i)), l = p;
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 ? g(l, i.items) : i.changes;
126
- return h(() => {
127
- d.added.forEach((p) => o(p)), d.modified.forEach((p) => a(p.id, { $set: p })), d.removed.forEach((p) => r(p.id));
128
- }), f;
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 T {
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.persistenceAdapter] Persistence adapter to use for storing changes, snapshots and sync operations.
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.createPersistenceAdapter("changes"), n = this.createPersistenceAdapter("snapshots"), o = this.createPersistenceAdapter("sync-operations");
162
- this.changes = new m({
163
- name: `${this.options.id}-changes`,
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 m({
168
- name: `${this.options.id}-snapshots`,
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 m({
173
- name: `${this.options.id}-sync-operations`,
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.changes.on("persistence.error", (a) => s == null ? void 0 : s.handler(a)), this.snapshots.on("persistence.error", (a) => n == null ? void 0 : n.handler(a)), this.syncOperations.on("persistence.error", (a) => o == null ? void 0 : o.handler(a)), this.persistenceReady = Promise.all([
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 = x(this.flushScheduledPushes, this.options.debounceTime ?? 100);
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 R()), this.syncQueues.get(e);
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.isReady(),
221
+ readyPromise: e.ready(),
245
222
  syncPaused: !0
246
223
  // always start paused as the autostart will start it
247
224
  });
248
- const s = (o) => {
225
+ const s = (n) => {
249
226
  for (const a of this.remoteChanges)
250
- 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
+ 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
- }, n = (o, a) => {
230
+ }, o = (n, a) => {
254
231
  const r = [...this.remoteChanges];
255
- for (let h = 0; h < r.length; h += 1) {
256
- const i = r[h];
257
- i != null && i.collectionName === o && (i.type === "remove" && i.data !== a || i.data.id === a && (r[h] = null));
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((h) => h != null);
236
+ this.remoteChanges = r.filter((c) => c != null);
260
237
  };
261
- e.on("added", (o) => {
262
- if (s({ collectionName: t.name, type: "insert", data: o })) {
263
- n(t.name, o.id);
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: o
271
- }), !this.getCollectionProperties(t.name).syncPaused && this.schedulePush(t.name);
272
- }), e.on("changed", ({ id: o }, a) => {
273
- const r = { id: o, modifier: a };
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
- n(t.name, o);
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
- }), !this.getCollectionProperties(t.name).syncPaused && this.schedulePush(t.name);
284
- }), e.on("removed", ({ id: o }) => {
285
- if (s({ collectionName: t.name, type: "remove", data: o })) {
286
- n(t.name, o);
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: o
294
- }), !this.getCollectionProperties(t.name).syncPaused && this.schedulePush(t.name);
295
- }), this.options.autostart && this.startSync(t.name).catch((o) => {
296
- this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, o);
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 (n) => {
326
- if (n == null)
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 o = Date.now(), a = this.syncOperations.insert({
330
- start: o,
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, n).then(() => {
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: o } },
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
- * @returns True if the collection is currently beeing synced, false otherwise.
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
- return this.syncOperations.findOne({
391
+ isSyncing(e, t) {
392
+ const s = this.syncOperations.findOne({
403
393
  ...e ? { collectionName: e } : {},
404
394
  status: "active"
405
- }, { fields: { status: 1 } }) != null;
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.persistenceReady;
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: n } = this.getCollectionProperties(e);
426
- await n;
427
- const o = this.syncOperations.find({
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 h = async () => {
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 != null && t.onlyWithChanges && this.changes.find({
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
- o || (r = this.syncOperations.insert({
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 l = await this.options.pull(s, {
461
- lastFinishedSyncStart: i == null ? void 0 : i.start,
462
- lastFinishedSyncEnd: i == null ? void 0 : i.end
454
+ const h = await this.options.pull(s, {
455
+ lastFinishedSyncStart: i?.start,
456
+ lastFinishedSyncEnd: i?.end
463
457
  });
464
- await this.syncWithData(e, l);
458
+ await this.syncWithData(e, h);
465
459
  };
466
- await (t != null && t.force ? h() : this.getSyncQueue(e).add(h)).catch((i) => {
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: n } = this.getCollectionProperties(e), o = Date.now(), a = this.syncOperations.findOne({
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
- }), r = this.snapshots.findOne({
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
- }), h = this.changes.find({
496
+ reactive: !1,
497
+ async: !0
498
+ }), c = await this.changes.find({
503
499
  collectionName: e,
504
- time: { $lte: o }
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 I({
510
- changes: h,
511
- lastSnapshot: r == null ? void 0 : r.items,
506
+ await F({
507
+ changes: c,
508
+ lastSnapshot: r?.items,
512
509
  data: t,
513
- pull: () => this.options.pull(n, {
514
- lastFinishedSyncStart: a == null ? void 0 : a.start,
515
- lastFinishedSyncEnd: a == null ? void 0 : a.end
510
+ pull: () => this.options.pull(o, {
511
+ lastFinishedSyncStart: a?.start,
512
+ lastFinishedSyncEnd: a?.end
516
513
  }),
517
- push: (i) => this.options.push(n, {
514
+ push: (i) => this.options.push(o, {
518
515
  changes: i,
519
- rawChanges: h
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, l) => {
529
+ update: async (i, h) => {
533
530
  this.remoteChanges.push({
534
531
  collectionName: e,
535
532
  type: "insert",
536
- data: { id: i, ...l.$set }
533
+ data: { id: i, ...h.$set }
537
534
  }, {
538
535
  collectionName: e,
539
536
  type: "update",
540
- data: { id: i, modifier: l }
541
- }), s.updateOne({ id: i }, {
542
- ...l,
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.findOne({
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
- s.batch(() => {
557
- i();
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: o }
564
- }), this.changes.removeMany({
558
+ time: { $lte: n }
559
+ }), await this.changes.removeMany({
565
560
  collectionName: e,
566
- id: { $in: h.map((d) => d.id) }
567
- }), this.snapshots.insert({
568
- time: o,
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 f = s.find({
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.forEach((d) => {
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
- }), f.forEach((d) => {
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
- T as SyncManager
602
+ b as SyncManager
607
603
  };
608
604
  //# sourceMappingURL=index.mjs.map