@signaldb/sync 1.0.1 → 1.1.0

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,22 +1,22 @@
1
- var E = Object.defineProperty;
2
- var v = (d, e, t) => e in d ? E(d, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : d[e] = t;
3
- var u = (d, e, t) => v(d, typeof e != "symbol" ? e + "" : e, t);
4
- import { isEqual as g, Collection as m, modify as D, randomId as P } from "@signaldb/core";
5
- function M(d, e, t = {}) {
6
- let a, s;
7
- const { leading: n = !1, trailing: i = !0 } = t;
8
- function o(...l) {
9
- const c = n && !a, r = i && !a;
10
- return a && clearTimeout(a), a = setTimeout(() => {
11
- a = null, i && !c && (s = d.apply(this, l));
12
- }, e), c ? s = d.apply(this, l) : r || (s = null), s;
1
+ var N = Object.defineProperty;
2
+ var v = (c, e, t) => e in c ? N(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
+ var l = (c, e, t) => v(c, typeof e != "symbol" ? e + "" : e, t);
4
+ import { isEqual as m, Collection as y, modify as E, randomId as D } from "@signaldb/core";
5
+ function S(c, e, t = {}) {
6
+ let s, n;
7
+ const { leading: a = !1, trailing: o = !0 } = t;
8
+ function r(...d) {
9
+ const i = a && !s, u = o && !s;
10
+ return s && clearTimeout(s), s = setTimeout(() => {
11
+ s = null, o && !i && (n = c.apply(this, d));
12
+ }, e), i ? n = c.apply(this, d) : u || (n = null), n;
13
13
  }
14
- return o;
14
+ return r;
15
15
  }
16
- class I {
16
+ class M {
17
17
  constructor() {
18
- u(this, "queue", []);
19
- u(this, "pendingPromise", !1);
18
+ l(this, "queue", []);
19
+ l(this, "pendingPromise", !1);
20
20
  }
21
21
  /**
22
22
  * Method to add a new promise to the queue and returns a promise that resolves when this task is done
@@ -24,9 +24,9 @@ class I {
24
24
  * @returns Promise that resolves when the task is done
25
25
  */
26
26
  add(e) {
27
- return new Promise((t, a) => {
28
- this.queue.push(() => e().then(t).catch((s) => {
29
- throw a(s), s;
27
+ return new Promise((t, s) => {
28
+ this.queue.push(() => e().then(t).catch((n) => {
29
+ throw s(n), n;
30
30
  })), this.dequeue();
31
31
  });
32
32
  }
@@ -51,71 +51,71 @@ class I {
51
51
  }));
52
52
  }
53
53
  }
54
- function w(d, e) {
55
- const t = [], a = [], s = [], n = new Map(d.map((o) => [o.id, o])), i = new Map(e.map((o) => [o.id, o]));
56
- for (const [o, l] of n) {
57
- const c = i.get(o);
58
- c ? g(c, l) || a.push(c) : s.push(l);
54
+ function g(c, e) {
55
+ const t = [], s = [], n = [], a = new Map(c.map((r) => [r.id, r])), o = new Map(e.map((r) => [r.id, r]));
56
+ for (const [r, d] of a) {
57
+ const i = o.get(r);
58
+ i ? m(i, d) || s.push(i) : n.push(d);
59
59
  }
60
- for (const [o, l] of i)
61
- n.has(o) || t.push(l);
62
- return { added: t, modified: a, removed: s };
60
+ for (const [r, d] of o)
61
+ a.has(r) || t.push(d);
62
+ return { added: t, modified: s, removed: n };
63
63
  }
64
- function O(d, e) {
64
+ function C(c, e) {
65
65
  if (e.items != null)
66
66
  return e.items;
67
- const t = d || [];
68
- return e.changes.added.forEach((a) => {
69
- const s = t.findIndex((n) => n.id === a.id);
70
- s === -1 ? t.push(a) : t[s] = a;
71
- }), e.changes.modified.forEach((a) => {
72
- const s = t.findIndex((n) => n.id === a.id);
73
- s === -1 ? t.push(a) : t[s] = a;
74
- }), e.changes.removed.forEach((a) => {
75
- const s = t.findIndex((n) => n.id === a.id);
76
- s !== -1 && t.splice(s, 1);
67
+ const t = c || [];
68
+ return e.changes.added.forEach((s) => {
69
+ const n = t.findIndex((a) => a.id === s.id);
70
+ n === -1 ? t.push(s) : t[n] = s;
71
+ }), e.changes.modified.forEach((s) => {
72
+ const n = t.findIndex((a) => a.id === s.id);
73
+ n === -1 ? t.push(s) : t[n] = s;
74
+ }), e.changes.removed.forEach((s) => {
75
+ const n = t.findIndex((a) => a.id === s.id);
76
+ n !== -1 && t.splice(n, 1);
77
77
  }), t;
78
78
  }
79
- async function $(d, e) {
80
- const t = new m();
79
+ async function P(c, e) {
80
+ const t = new y();
81
81
  t.batch(() => {
82
- d.forEach((s) => t.insert(s)), e.forEach((s) => {
83
- if (s.type === "remove") {
84
- t.removeOne({ id: s.data });
82
+ c.forEach((n) => t.insert(n)), e.forEach((n) => {
83
+ if (n.type === "remove") {
84
+ t.removeOne({ id: n.data });
85
85
  return;
86
86
  }
87
- const n = { id: s.data.id }, i = t.findOne(n);
88
- if (s.type === "insert") {
89
- i ? t.updateOne(n, { $set: s.data }) : t.insert(s.data);
87
+ const a = { id: n.data.id }, o = t.findOne(a);
88
+ if (n.type === "insert") {
89
+ o ? t.updateOne(a, { $set: n.data }) : t.insert(n.data);
90
90
  return;
91
91
  }
92
- i ? t.updateOne(n, s.data.modifier) : t.insert(D(n, s.data.modifier));
92
+ o ? t.updateOne(a, n.data.modifier) : t.insert(E(a, n.data.modifier));
93
93
  });
94
94
  });
95
- const a = t.find().fetch();
96
- return await t.dispose(), a;
95
+ const s = t.find().fetch();
96
+ return await t.dispose(), s;
97
97
  }
98
- function N(d) {
99
- return d.added.length > 0 || d.modified.length > 0 || d.removed.length > 0;
98
+ function O(c) {
99
+ return c.added.length > 0 || c.modified.length > 0 || c.removed.length > 0;
100
100
  }
101
- function x(d, e) {
102
- return N(w(d, e));
101
+ function I(c, e) {
102
+ return O(g(c, e));
103
103
  }
104
- async function S({ changes: d, lastSnapshot: e, data: t, pull: a, push: s, insert: n, update: i, remove: o, batch: l }) {
105
- let c = t, r = e || [], p = O(e, c);
106
- if (d.length > 0) {
107
- const h = await $(r, d);
108
- if (x(r, h)) {
109
- const y = await $(p, d), C = w(p, y);
110
- N(C) && (await s(C), c = await a(), p = O(p, c)), r = h;
104
+ async function x({ changes: c, lastSnapshot: e, data: t, pull: s, push: n, insert: a, update: o, remove: r, batch: d }) {
105
+ let i = t, u = e || [], p = C(e, i);
106
+ if (c.length > 0) {
107
+ const f = await P(u, c);
108
+ if (I(u, f)) {
109
+ const $ = await P(p, c), w = g(p, $);
110
+ O(w) && (await n(w), i = await s(), p = C(p, i)), u = f;
111
111
  }
112
112
  }
113
- const f = c.changes == null ? w(r, c.items) : c.changes;
114
- return l(() => {
115
- f.added.forEach((h) => n(h)), f.modified.forEach((h) => i(h.id, { $set: h })), f.removed.forEach((h) => o(h.id));
113
+ const h = i.changes == null ? g(u, i.items) : i.changes;
114
+ return d(() => {
115
+ h.added.forEach((f) => a(f)), h.modified.forEach((f) => o(f.id, { $set: f })), h.removed.forEach((f) => r(f.id));
116
116
  }), p;
117
117
  }
118
- class T {
118
+ class q {
119
119
  /**
120
120
  * @param options Collection options
121
121
  * @param options.pull Function to pull data from remote source.
@@ -127,57 +127,42 @@ class T {
127
127
  * @param [options.onError] Function to handle errors that occur async during syncing.
128
128
  */
129
129
  constructor(e) {
130
- u(this, "options");
131
- u(this, "collections", /* @__PURE__ */ new Map());
132
- u(this, "changes");
133
- u(this, "snapshots");
134
- u(this, "syncOperations");
135
- u(this, "remoteChanges", []);
136
- u(this, "syncQueues", /* @__PURE__ */ new Map());
137
- u(this, "persistenceReady");
138
- u(this, "isDisposed", !1);
139
- u(this, "instanceId", P());
140
- u(this, "id");
141
- u(this, "deboucedPush", M((e) => {
130
+ l(this, "options");
131
+ l(this, "collections", /* @__PURE__ */ new Map());
132
+ l(this, "changes");
133
+ l(this, "snapshots");
134
+ l(this, "syncOperations");
135
+ l(this, "remoteChanges", []);
136
+ l(this, "syncQueues", /* @__PURE__ */ new Map());
137
+ l(this, "persistenceReady");
138
+ l(this, "isDisposed", !1);
139
+ l(this, "instanceId", D());
140
+ l(this, "id");
141
+ l(this, "deboucedPush", S((e) => {
142
142
  this.pushChanges(e).catch(() => {
143
143
  });
144
144
  }, 100));
145
- this.options = e, this.id = this.options.id || "default-sync-manager";
146
- const { reactivity: t } = this.options, a = this.createPersistenceAdapter("changes"), s = this.createPersistenceAdapter("snapshots"), n = this.createPersistenceAdapter("sync-operations");
147
- this.changes = new m({
145
+ this.options = {
146
+ autostart: !0,
147
+ ...e
148
+ }, this.id = this.options.id || "default-sync-manager";
149
+ const { reactivity: t } = this.options, s = this.createPersistenceAdapter("changes"), n = this.createPersistenceAdapter("snapshots"), a = this.createPersistenceAdapter("sync-operations");
150
+ this.changes = new y({
148
151
  name: `${this.options.id}-changes`,
149
- persistence: a == null ? void 0 : a.adapter,
152
+ persistence: s == null ? void 0 : s.adapter,
150
153
  reactivity: t
151
- }), this.snapshots = new m({
154
+ }), this.snapshots = new y({
152
155
  name: `${this.options.id}-snapshots`,
153
- persistence: s == null ? void 0 : s.adapter,
156
+ persistence: n == null ? void 0 : n.adapter,
154
157
  reactivity: t
155
- }), this.syncOperations = new m({
158
+ }), this.syncOperations = new y({
156
159
  name: `${this.options.id}-sync-operations`,
157
- persistence: n == null ? void 0 : n.adapter,
160
+ persistence: a == null ? void 0 : a.adapter,
158
161
  reactivity: t
159
- }), this.changes.on("persistence.error", (i) => a == null ? void 0 : a.handler(i)), this.snapshots.on("persistence.error", (i) => s == null ? void 0 : s.handler(i)), this.syncOperations.on("persistence.error", (i) => n == null ? void 0 : n.handler(i)), this.persistenceReady = Promise.all([
160
- new Promise((i, o) => {
161
- if (!a) {
162
- i();
163
- return;
164
- }
165
- this.syncOperations.once("persistence.error", o), this.syncOperations.once("persistence.init", i);
166
- }),
167
- new Promise((i, o) => {
168
- if (!s) {
169
- i();
170
- return;
171
- }
172
- this.changes.once("persistence.error", o), this.changes.once("persistence.init", i);
173
- }),
174
- new Promise((i, o) => {
175
- if (!n) {
176
- i();
177
- return;
178
- }
179
- this.snapshots.once("persistence.error", o), this.snapshots.once("persistence.init", i);
180
- })
162
+ }), this.changes.on("persistence.error", (o) => s == null ? void 0 : s.handler(o)), this.snapshots.on("persistence.error", (o) => n == null ? void 0 : n.handler(o)), this.syncOperations.on("persistence.error", (o) => a == null ? void 0 : a.handler(o)), this.persistenceReady = Promise.all([
163
+ this.syncOperations.isReady(),
164
+ this.changes.isReady(),
165
+ this.snapshots.isReady()
181
166
  ]).then(() => {
182
167
  }), this.changes.setMaxListeners(1e3), this.snapshots.setMaxListeners(1e3), this.syncOperations.setMaxListeners(1e3);
183
168
  }
@@ -187,14 +172,14 @@ class T {
187
172
  let t = () => {
188
173
  };
189
174
  return {
190
- adapter: this.options.persistenceAdapter(`${this.id}-${e}`, (s) => {
191
- t = s;
175
+ adapter: this.options.persistenceAdapter(`${this.id}-${e}`, (n) => {
176
+ t = n;
192
177
  }),
193
- handler: (s) => t(s)
178
+ handler: (n) => t(n)
194
179
  };
195
180
  }
196
181
  getSyncQueue(e) {
197
- return this.syncQueues.get(e) == null && this.syncQueues.set(e, new I()), this.syncQueues.get(e);
182
+ return this.syncQueues.get(e) == null && this.syncQueues.set(e, new M()), this.syncQueues.get(e);
198
183
  }
199
184
  /**
200
185
  * Clears all internal data structures
@@ -208,11 +193,22 @@ class T {
208
193
  }
209
194
  /**
210
195
  * Gets a collection with it's options by name
196
+ * @deprecated Use getCollectionProperties instead.
211
197
  * @param name Name of the collection
212
198
  * @throws Will throw an error if the name wasn't found
213
199
  * @returns Tuple of collection and options
214
200
  */
215
201
  getCollection(e) {
202
+ const { collection: t, options: s } = this.getCollectionProperties(e);
203
+ return [t, s];
204
+ }
205
+ /**
206
+ * Gets collection options by name
207
+ * @param name Name of the collection
208
+ * @throws Will throw an error if the name wasn't found
209
+ * @returns An object of all properties of the collection
210
+ */
211
+ getCollectionProperties(e) {
216
212
  const t = this.collections.get(e);
217
213
  if (t == null)
218
214
  throw new Error(`Collection with id '${e}' not found`);
@@ -227,85 +223,139 @@ class T {
227
223
  addCollection(e, t) {
228
224
  if (this.isDisposed)
229
225
  throw new Error("SyncManager is disposed");
230
- this.options.registerRemoteChange && this.options.registerRemoteChange(t, async (n) => {
231
- if (n == null)
232
- await this.sync(t.name);
233
- else {
234
- const i = Date.now(), o = this.syncOperations.insert({
235
- start: i,
236
- collectionName: t.name,
237
- instanceId: this.instanceId,
238
- status: "active"
239
- });
240
- await this.syncWithData(t.name, n).then(() => {
241
- this.syncOperations.removeMany({
242
- id: { $ne: o },
243
- collectionName: t.name,
244
- $or: [
245
- { end: { $lte: i } },
246
- { status: "active" }
247
- ]
248
- }), this.syncOperations.updateOne({ id: o }, {
249
- $set: { status: "done", end: Date.now() }
250
- });
251
- }).catch((l) => {
252
- throw this.options.onError && this.options.onError(t, l), this.syncOperations.updateOne({ id: o }, {
253
- $set: { status: "error", end: Date.now(), error: l.stack || l.message }
254
- }), l;
255
- });
256
- }
257
- }), this.collections.set(t.name, [e, t]);
258
- const a = (n) => {
259
- for (const i of this.remoteChanges)
260
- if (g(i, n))
226
+ this.collections.set(t.name, {
227
+ collection: e,
228
+ options: t,
229
+ readyPromise: e.isReady(),
230
+ syncPaused: !0
231
+ // always start paused as the autostart will start it
232
+ });
233
+ const s = (a) => {
234
+ for (const o of this.remoteChanges)
235
+ if (m(o, a))
261
236
  return !0;
262
237
  return !1;
263
- }, s = (n) => {
264
- for (let i = 0; i < this.remoteChanges.length; i += 1)
265
- if (g(this.remoteChanges[i], n)) {
266
- this.remoteChanges.splice(i, 1);
238
+ }, n = (a) => {
239
+ for (let o = 0; o < this.remoteChanges.length; o += 1)
240
+ if (m(this.remoteChanges[o], a)) {
241
+ this.remoteChanges.splice(o, 1);
267
242
  return;
268
243
  }
269
244
  };
270
- e.on("added", (n) => {
271
- if (a({ collectionName: t.name, type: "insert", data: n })) {
272
- s({ collectionName: t.name, type: "insert", data: n });
245
+ e.on("added", (a) => {
246
+ if (s({ collectionName: t.name, type: "insert", data: a })) {
247
+ n({ collectionName: t.name, type: "insert", data: a });
273
248
  return;
274
249
  }
275
250
  this.changes.insert({
276
251
  collectionName: t.name,
277
252
  time: Date.now(),
278
253
  type: "insert",
279
- data: n
280
- }), this.schedulePush(t.name);
281
- }), e.on("changed", ({ id: n }, i) => {
282
- const o = { id: n, modifier: i };
283
- if (a({ collectionName: t.name, type: "update", data: o })) {
284
- s({ collectionName: t.name, type: "update", data: o });
254
+ data: a
255
+ }), !this.getCollectionProperties(t.name).syncPaused && this.schedulePush(t.name);
256
+ }), e.on("changed", ({ id: a }, o) => {
257
+ const r = { id: a, modifier: o };
258
+ if (s({ collectionName: t.name, type: "update", data: r })) {
259
+ n({ collectionName: t.name, type: "update", data: r });
285
260
  return;
286
261
  }
287
262
  this.changes.insert({
288
263
  collectionName: t.name,
289
264
  time: Date.now(),
290
265
  type: "update",
291
- data: o
292
- }), this.schedulePush(t.name);
293
- }), e.on("removed", ({ id: n }) => {
294
- if (a({ collectionName: t.name, type: "remove", data: n })) {
295
- s({ collectionName: t.name, type: "remove", data: n });
266
+ data: r
267
+ }), !this.getCollectionProperties(t.name).syncPaused && this.schedulePush(t.name);
268
+ }), e.on("removed", ({ id: a }) => {
269
+ if (s({ collectionName: t.name, type: "remove", data: a })) {
270
+ n({ collectionName: t.name, type: "remove", data: a });
296
271
  return;
297
272
  }
298
273
  this.changes.insert({
299
274
  collectionName: t.name,
300
275
  time: Date.now(),
301
276
  type: "remove",
302
- data: n
303
- }), this.schedulePush(t.name);
277
+ data: a
278
+ }), !this.getCollectionProperties(t.name).syncPaused && this.schedulePush(t.name);
279
+ }), this.options.autostart && this.startSync(t.name).catch((a) => {
280
+ this.options.onError && this.options.onError(this.getCollectionProperties(t.name).options, a);
304
281
  });
305
282
  }
306
283
  schedulePush(e) {
307
284
  this.deboucedPush(e);
308
285
  }
286
+ /**
287
+ * Setup all collections to be synced with remote changes
288
+ * and enable automatic pushing changes to the remote source.
289
+ */
290
+ async startAll() {
291
+ await Promise.all([...this.collections.keys()].map((e) => this.startSync(e)));
292
+ }
293
+ /**
294
+ * Setup a collection to be synced with remote changes
295
+ * and enable automatic pushing changes to the remote source.
296
+ * @param name Name of the collection
297
+ */
298
+ async startSync(e) {
299
+ const t = this.getCollectionProperties(e);
300
+ if (!t.syncPaused)
301
+ return;
302
+ this.schedulePush(e);
303
+ const s = this.options.registerRemoteChange ? await this.options.registerRemoteChange(t.options, async (n) => {
304
+ if (n == null)
305
+ await this.sync(e);
306
+ else {
307
+ const a = Date.now(), o = this.syncOperations.insert({
308
+ start: a,
309
+ collectionName: e,
310
+ instanceId: this.instanceId,
311
+ status: "active"
312
+ });
313
+ await this.syncWithData(e, n).then(() => {
314
+ this.syncOperations.removeMany({
315
+ id: { $ne: o },
316
+ collectionName: e,
317
+ $or: [
318
+ { end: { $lte: a } },
319
+ { status: "active" }
320
+ ]
321
+ }), this.syncOperations.updateOne({ id: o }, {
322
+ $set: { status: "done", end: Date.now() }
323
+ });
324
+ }).catch((r) => {
325
+ throw this.options.onError && this.options.onError(this.getCollectionProperties(e).options, r), this.syncOperations.updateOne({ id: o }, {
326
+ $set: { status: "error", end: Date.now(), error: r.stack || r.message }
327
+ }), r;
328
+ });
329
+ }
330
+ }) : void 0;
331
+ this.collections.set(e, {
332
+ ...t,
333
+ syncPaused: !1,
334
+ cleanupFunction: s
335
+ });
336
+ }
337
+ /**
338
+ * Pauses the sync process for all collections.
339
+ * This means that the collections will not be synced with remote changes
340
+ * and changes will not automatically be pushed to the remote source.
341
+ */
342
+ async pauseAll() {
343
+ await Promise.all([...this.collections.keys()].map((e) => this.pauseSync(e)));
344
+ }
345
+ /**
346
+ * Pauses the sync process for a collection.
347
+ * This means that the collection will not be synced with remote changes
348
+ * and changes will not automatically be pushed to the remote source.
349
+ * @param name Name of the collection
350
+ */
351
+ async pauseSync(e) {
352
+ const t = this.getCollectionProperties(e);
353
+ t.syncPaused || (t.cleanupFunction && await t.cleanupFunction(), this.collections.set(e, {
354
+ ...t,
355
+ cleanupFunction: void 0,
356
+ syncPaused: !0
357
+ }));
358
+ }
309
359
  /**
310
360
  * Starts the sync process for all collections
311
361
  */
@@ -313,8 +363,8 @@ class T {
313
363
  if (this.isDisposed)
314
364
  throw new Error("SyncManager is disposed");
315
365
  const e = [];
316
- if (await Promise.all([...this.collections.keys()].map((t) => this.sync(t).catch((a) => {
317
- e.push({ id: t, error: a });
366
+ if (await Promise.all([...this.collections.keys()].map((t) => this.sync(t).catch((s) => {
367
+ e.push({ id: t, error: s });
318
368
  }))), e.length > 0)
319
369
  throw new Error(`Error while syncing collections:
320
370
  ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
@@ -350,48 +400,50 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
350
400
  if (this.isDisposed)
351
401
  throw new Error("SyncManager is disposed");
352
402
  await this.isReady();
353
- const s = this.getCollection(e)[1], n = this.syncOperations.find({
403
+ const { options: s, readyPromise: n } = this.getCollectionProperties(e);
404
+ await n;
405
+ const a = this.syncOperations.find({
354
406
  collectionName: e,
355
407
  instanceId: this.instanceId,
356
408
  status: "active"
357
- }).count() > 0, i = Date.now();
358
- let o = null;
359
- await new Promise((c) => {
360
- setTimeout(c, 0);
409
+ }).count() > 0, o = Date.now();
410
+ let r = null;
411
+ await new Promise((i) => {
412
+ setTimeout(i, 0);
361
413
  });
362
- const l = async () => {
363
- const c = this.syncOperations.findOne({ collectionName: e, status: "done" }, { sort: { end: -1 } });
414
+ const d = async () => {
415
+ const i = this.syncOperations.findOne({ collectionName: e, status: "done" }, { sort: { end: -1 } });
364
416
  if (t != null && t.onlyWithChanges && this.changes.find({
365
417
  collectionName: e,
366
418
  $and: [
367
- { time: { $lte: i } }
419
+ { time: { $lte: o } }
368
420
  ]
369
421
  }, { sort: { time: 1 } }).count() === 0)
370
422
  return;
371
- n || (o = this.syncOperations.insert({
372
- start: i,
423
+ a || (r = this.syncOperations.insert({
424
+ start: o,
373
425
  collectionName: e,
374
426
  instanceId: this.instanceId,
375
427
  status: "active"
376
428
  }));
377
- const r = await this.options.pull(s, {
378
- lastFinishedSyncStart: c == null ? void 0 : c.start,
379
- lastFinishedSyncEnd: c == null ? void 0 : c.end
429
+ const u = await this.options.pull(s, {
430
+ lastFinishedSyncStart: i == null ? void 0 : i.start,
431
+ lastFinishedSyncEnd: i == null ? void 0 : i.end
380
432
  });
381
- await this.syncWithData(e, r);
433
+ await this.syncWithData(e, u);
382
434
  };
383
- await (t != null && t.force ? l() : this.getSyncQueue(e).add(l)).catch((c) => {
384
- throw o != null && (this.options.onError && this.options.onError(s, c), this.syncOperations.updateOne({ id: o }, {
385
- $set: { status: "error", end: Date.now(), error: c.stack || c.message }
386
- })), c;
387
- }), o != null && (this.syncOperations.removeMany({
388
- id: { $ne: o },
435
+ await (t != null && t.force ? d() : this.getSyncQueue(e).add(d)).catch((i) => {
436
+ throw r != null && (this.options.onError && this.options.onError(s, i), this.syncOperations.updateOne({ id: r }, {
437
+ $set: { status: "error", end: Date.now(), error: i.stack || i.message }
438
+ })), i;
439
+ }), r != null && (this.syncOperations.removeMany({
440
+ id: { $ne: r },
389
441
  collectionName: e,
390
442
  $or: [
391
- { end: { $lte: i } },
443
+ { end: { $lte: o } },
392
444
  { status: "active" }
393
445
  ]
394
- }), this.syncOperations.updateOne({ id: o }, {
446
+ }), this.syncOperations.updateOne({ id: r }, {
395
447
  $set: { status: "done", end: Date.now() }
396
448
  }));
397
449
  }
@@ -405,72 +457,72 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
405
457
  });
406
458
  }
407
459
  async syncWithData(e, t) {
408
- const a = this.getCollection(e), [s, n] = a, i = Date.now(), o = this.syncOperations.findOne({ collectionName: e, status: "done" }, { sort: { end: -1 } }), l = this.snapshots.findOne({ collectionName: e }, { sort: { time: -1 } }), c = this.changes.find({
460
+ const { collection: s, options: n } = this.getCollectionProperties(e), a = Date.now(), o = this.syncOperations.findOne({ collectionName: e, status: "done" }, { sort: { end: -1 } }), r = this.snapshots.findOne({ collectionName: e }, { sort: { time: -1 } }), d = this.changes.find({
409
461
  collectionName: e,
410
462
  $and: [
411
- { time: { $lte: i } }
463
+ { time: { $lte: a } }
412
464
  ]
413
465
  }, { sort: { time: 1 } }).fetch();
414
- await S({
415
- changes: c,
416
- lastSnapshot: l == null ? void 0 : l.items,
466
+ await x({
467
+ changes: d,
468
+ lastSnapshot: r == null ? void 0 : r.items,
417
469
  data: t,
418
470
  pull: () => this.options.pull(n, {
419
471
  lastFinishedSyncStart: o == null ? void 0 : o.start,
420
472
  lastFinishedSyncEnd: o == null ? void 0 : o.end
421
473
  }),
422
- push: (r) => this.options.push(n, { changes: r }),
423
- insert: (r) => {
424
- if (r.id && s.findOne({ id: r.id })) {
474
+ push: (i) => this.options.push(n, { changes: i }),
475
+ insert: (i) => {
476
+ if (i.id && s.findOne({ id: i.id })) {
425
477
  this.remoteChanges.push({
426
478
  collectionName: e,
427
479
  type: "update",
428
- data: { id: r.id, modifier: { $set: r } }
429
- }), s.updateOne({ id: r.id }, { $set: r });
480
+ data: { id: i.id, modifier: { $set: i } }
481
+ }), s.updateOne({ id: i.id }, { $set: i });
430
482
  return;
431
483
  }
432
484
  this.remoteChanges.push({
433
485
  collectionName: e,
434
486
  type: "insert",
435
- data: r
436
- }), s.insert(r);
487
+ data: i
488
+ }), s.insert(i);
437
489
  },
438
- update: (r, p) => {
439
- if (r && !s.findOne({ id: r })) {
440
- const f = { ...p.$set, id: r };
490
+ update: (i, u) => {
491
+ if (i && !s.findOne({ id: i })) {
492
+ const p = { ...u.$set, id: i };
441
493
  this.remoteChanges.push({
442
494
  collectionName: e,
443
495
  type: "insert",
444
- data: f
445
- }), s.insert(f);
496
+ data: p
497
+ }), s.insert(p);
446
498
  return;
447
499
  }
448
500
  this.remoteChanges.push({
449
501
  collectionName: e,
450
502
  type: "update",
451
- data: { id: r, modifier: p }
452
- }), s.updateOne({ id: r }, p);
503
+ data: { id: i, modifier: u }
504
+ }), s.updateOne({ id: i }, u);
453
505
  },
454
- remove: (r) => {
455
- s.findOne({ id: r }) && (this.remoteChanges.push({
506
+ remove: (i) => {
507
+ s.findOne({ id: i }) && (this.remoteChanges.push({
456
508
  collectionName: e,
457
509
  type: "remove",
458
- data: r
459
- }), s.removeOne({ id: r }));
510
+ data: i
511
+ }), s.removeOne({ id: i }));
460
512
  },
461
- batch: (r) => {
513
+ batch: (i) => {
462
514
  s.batch(() => {
463
- r();
515
+ i();
464
516
  });
465
517
  }
466
- }).then(async (r) => {
467
- if (this.snapshots.removeMany({ collectionName: e, time: { $lte: i } }), this.changes.removeMany({
518
+ }).then(async (i) => {
519
+ if (this.snapshots.removeMany({ collectionName: e, time: { $lte: a } }), this.changes.removeMany({
468
520
  collectionName: e,
469
- id: { $in: c.map((h) => h.id) }
521
+ id: { $in: d.map((h) => h.id) }
470
522
  }), this.snapshots.insert({
471
- time: i,
523
+ time: a,
472
524
  collectionName: e,
473
- items: r
525
+ items: i
474
526
  }), await new Promise((h) => {
475
527
  setTimeout(h, 0);
476
528
  }), this.changes.find({
@@ -482,14 +534,14 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
482
534
  });
483
535
  return;
484
536
  }
485
- const f = s.find({
486
- id: { $nin: r.map((h) => h.id) }
537
+ const p = s.find({
538
+ id: { $nin: i.map((h) => h.id) }
487
539
  }).map((h) => h.id);
488
540
  s.batch(() => {
489
- r.forEach((h) => {
490
- const y = !!s.findOne({ id: h.id });
541
+ i.forEach((h) => {
542
+ const f = !!s.findOne({ id: h.id });
491
543
  /* istanbul ignore else -- @preserve */
492
- y ? (this.remoteChanges.push({
544
+ f ? (this.remoteChanges.push({
493
545
  collectionName: e,
494
546
  type: "update",
495
547
  data: { id: h.id, modifier: { $set: h } }
@@ -498,7 +550,7 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
498
550
  type: "insert",
499
551
  data: h
500
552
  }), s.insert(h));
501
- }), f.forEach((h) => {
553
+ }), p.forEach((h) => {
502
554
  s.removeOne({ id: h });
503
555
  });
504
556
  });
@@ -506,6 +558,6 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
506
558
  }
507
559
  }
508
560
  export {
509
- T as SyncManager
561
+ q as SyncManager
510
562
  };
511
563
  //# sourceMappingURL=index.mjs.map