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