@signaldb/sync 1.0.1 → 1.1.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,19 +1,19 @@
1
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;
2
+ var $ = (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) => $(c, typeof e != "symbol" ? e + "" : e, t);
4
+ import { isEqual as m, Collection as y, modify as N, randomId as D } from "@signaldb/core";
5
+ function x(c, e, t = {}) {
6
+ let s, n;
7
+ const { leading: a = !1, trailing: o = !0 } = t;
8
+ function r(...l) {
9
+ const i = a && !s, d = o && !s;
10
+ return s && clearTimeout(s), s = setTimeout(() => {
11
+ s = null, o && !i && (n = c.apply(this, l));
12
+ }, e), i ? n = c.apply(this, l) : d || (n = null), n;
13
13
  }
14
- return o;
14
+ return r;
15
15
  }
16
- class I {
16
+ class S {
17
17
  constructor() {
18
18
  u(this, "queue", []);
19
19
  u(this, "pendingPromise", !1);
@@ -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, l] of a) {
57
+ const i = o.get(r);
58
+ i ? m(i, l) || s.push(i) : n.push(l);
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, l] of o)
61
+ a.has(r) || t.push(l);
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(N(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 v(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 M(c, e) {
102
+ return v(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 I({ changes: c, lastSnapshot: e, data: t, pull: s, push: n, insert: a, update: o, remove: r, batch: l }) {
105
+ let i = t, d = e || [], f = C(e, i);
106
+ if (c.length > 0) {
107
+ const p = await P(d, c);
108
+ if (M(d, p)) {
109
+ const O = await P(f, c), w = g(f, O);
110
+ v(w) && (await n(w), i = await s(), f = C(f, i)), d = p;
111
111
  }
112
112
  }
113
- const f = c.changes == null ? w(r, c.items) : c.changes;
113
+ const h = i.changes == null ? g(d, i.items) : i.changes;
114
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;
115
+ h.added.forEach((p) => a(p)), h.modified.forEach((p) => o(p.id, { $set: p })), h.removed.forEach((p) => r(p.id));
116
+ }), f;
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.
@@ -136,48 +136,33 @@ class T {
136
136
  u(this, "syncQueues", /* @__PURE__ */ new Map());
137
137
  u(this, "persistenceReady");
138
138
  u(this, "isDisposed", !1);
139
- u(this, "instanceId", P());
139
+ u(this, "instanceId", D());
140
140
  u(this, "id");
141
- u(this, "deboucedPush", M((e) => {
141
+ u(this, "deboucedPush", x((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 S()), 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,61 @@ ${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
+ }, {
410
+ reactive: !1
411
+ }).count() > 0, o = Date.now();
412
+ let r = null;
413
+ await new Promise((i) => {
414
+ setTimeout(i, 0);
361
415
  });
362
416
  const l = async () => {
363
- const c = this.syncOperations.findOne({ collectionName: e, status: "done" }, { sort: { end: -1 } });
417
+ const i = this.syncOperations.findOne({
418
+ collectionName: e,
419
+ status: "done"
420
+ }, {
421
+ sort: { end: -1 },
422
+ reactive: !1
423
+ });
364
424
  if (t != null && t.onlyWithChanges && this.changes.find({
365
425
  collectionName: e,
366
426
  $and: [
367
- { time: { $lte: i } }
427
+ { time: { $lte: o } }
368
428
  ]
369
- }, { sort: { time: 1 } }).count() === 0)
429
+ }, {
430
+ sort: { time: 1 },
431
+ reactive: !1
432
+ }).count() === 0)
370
433
  return;
371
- n || (o = this.syncOperations.insert({
372
- start: i,
434
+ a || (r = this.syncOperations.insert({
435
+ start: o,
373
436
  collectionName: e,
374
437
  instanceId: this.instanceId,
375
438
  status: "active"
376
439
  }));
377
- const r = await this.options.pull(s, {
378
- lastFinishedSyncStart: c == null ? void 0 : c.start,
379
- lastFinishedSyncEnd: c == null ? void 0 : c.end
440
+ const d = await this.options.pull(s, {
441
+ lastFinishedSyncStart: i == null ? void 0 : i.start,
442
+ lastFinishedSyncEnd: i == null ? void 0 : i.end
380
443
  });
381
- await this.syncWithData(e, r);
444
+ await this.syncWithData(e, d);
382
445
  };
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 },
446
+ await (t != null && t.force ? l() : this.getSyncQueue(e).add(l)).catch((i) => {
447
+ throw r != null && (this.options.onError && this.options.onError(s, i), this.syncOperations.updateOne({ id: r }, {
448
+ $set: { status: "error", end: Date.now(), error: i.stack || i.message }
449
+ })), i;
450
+ }), r != null && (this.syncOperations.removeMany({
451
+ id: { $ne: r },
389
452
  collectionName: e,
390
453
  $or: [
391
- { end: { $lte: i } },
454
+ { end: { $lte: o } },
392
455
  { status: "active" }
393
456
  ]
394
- }), this.syncOperations.updateOne({ id: o }, {
457
+ }), this.syncOperations.updateOne({ id: r }, {
395
458
  $set: { status: "done", end: Date.now() }
396
459
  }));
397
460
  }
@@ -405,77 +468,100 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
405
468
  });
406
469
  }
407
470
  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({
471
+ const { collection: s, options: n } = this.getCollectionProperties(e), a = Date.now(), o = this.syncOperations.findOne({
472
+ collectionName: e,
473
+ status: "done"
474
+ }, {
475
+ sort: { end: -1 },
476
+ reactive: !1
477
+ }), r = this.snapshots.findOne({
478
+ collectionName: e
479
+ }, {
480
+ sort: { time: -1 },
481
+ reactive: !1
482
+ }), l = this.changes.find({
409
483
  collectionName: e,
410
484
  $and: [
411
- { time: { $lte: i } }
485
+ { time: { $lte: a } }
412
486
  ]
413
- }, { sort: { time: 1 } }).fetch();
414
- await S({
415
- changes: c,
416
- lastSnapshot: l == null ? void 0 : l.items,
487
+ }, {
488
+ sort: { time: 1 },
489
+ reactive: !1
490
+ }).fetch();
491
+ await I({
492
+ changes: l,
493
+ lastSnapshot: r == null ? void 0 : r.items,
417
494
  data: t,
418
495
  pull: () => this.options.pull(n, {
419
496
  lastFinishedSyncStart: o == null ? void 0 : o.start,
420
497
  lastFinishedSyncEnd: o == null ? void 0 : o.end
421
498
  }),
422
- push: (r) => this.options.push(n, { changes: r }),
423
- insert: (r) => {
424
- if (r.id && s.findOne({ id: r.id })) {
499
+ push: (i) => this.options.push(n, { changes: i }),
500
+ insert: (i) => {
501
+ if (i.id && !!s.findOne({
502
+ id: i.id
503
+ }, { reactive: !1 })) {
425
504
  this.remoteChanges.push({
426
505
  collectionName: e,
427
506
  type: "update",
428
- data: { id: r.id, modifier: { $set: r } }
429
- }), s.updateOne({ id: r.id }, { $set: r });
507
+ data: { id: i.id, modifier: { $set: i } }
508
+ }), s.updateOne({ id: i.id }, { $set: i });
430
509
  return;
431
510
  }
432
511
  this.remoteChanges.push({
433
512
  collectionName: e,
434
513
  type: "insert",
435
- data: r
436
- }), s.insert(r);
514
+ data: i
515
+ }), s.insert(i);
437
516
  },
438
- update: (r, p) => {
439
- if (r && !s.findOne({ id: r })) {
440
- const f = { ...p.$set, id: r };
517
+ update: (i, d) => {
518
+ if (i && !s.findOne({
519
+ id: i
520
+ }, { reactive: !1 })) {
521
+ const h = { ...d.$set, id: i };
441
522
  this.remoteChanges.push({
442
523
  collectionName: e,
443
524
  type: "insert",
444
- data: f
445
- }), s.insert(f);
525
+ data: h
526
+ }), s.insert(h);
446
527
  return;
447
528
  }
448
529
  this.remoteChanges.push({
449
530
  collectionName: e,
450
531
  type: "update",
451
- data: { id: r, modifier: p }
452
- }), s.updateOne({ id: r }, p);
532
+ data: { id: i, modifier: d }
533
+ }), s.updateOne({ id: i }, d);
453
534
  },
454
- remove: (r) => {
455
- s.findOne({ id: r }) && (this.remoteChanges.push({
535
+ remove: (i) => {
536
+ s.findOne({
537
+ id: i
538
+ }, { reactive: !1 }) && (this.remoteChanges.push({
456
539
  collectionName: e,
457
540
  type: "remove",
458
- data: r
459
- }), s.removeOne({ id: r }));
541
+ data: i
542
+ }), s.removeOne({ id: i }));
460
543
  },
461
- batch: (r) => {
544
+ batch: (i) => {
462
545
  s.batch(() => {
463
- r();
546
+ i();
464
547
  });
465
548
  }
466
- }).then(async (r) => {
467
- if (this.snapshots.removeMany({ collectionName: e, time: { $lte: i } }), this.changes.removeMany({
549
+ }).then(async (i) => {
550
+ if (this.snapshots.removeMany({
468
551
  collectionName: e,
469
- id: { $in: c.map((h) => h.id) }
552
+ time: { $lte: a }
553
+ }), this.changes.removeMany({
554
+ collectionName: e,
555
+ id: { $in: l.map((h) => h.id) }
470
556
  }), this.snapshots.insert({
471
- time: i,
557
+ time: a,
472
558
  collectionName: e,
473
- items: r
559
+ items: i
474
560
  }), await new Promise((h) => {
475
561
  setTimeout(h, 0);
476
562
  }), this.changes.find({
477
563
  collectionName: e
478
- }).count() > 0) {
564
+ }, { reactive: !1 }).count() > 0) {
479
565
  await this.sync(e, {
480
566
  force: !0,
481
567
  onlyWithChanges: !0
@@ -483,13 +569,19 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
483
569
  return;
484
570
  }
485
571
  const f = s.find({
486
- id: { $nin: r.map((h) => h.id) }
572
+ id: { $nin: i.map((h) => h.id) }
573
+ }, {
574
+ reactive: !1
487
575
  }).map((h) => h.id);
488
576
  s.batch(() => {
489
- r.forEach((h) => {
490
- const y = !!s.findOne({ id: h.id });
577
+ i.forEach((h) => {
578
+ const p = !!s.findOne({
579
+ id: h.id
580
+ }, {
581
+ reactive: !1
582
+ });
491
583
  /* istanbul ignore else -- @preserve */
492
- y ? (this.remoteChanges.push({
584
+ p ? (this.remoteChanges.push({
493
585
  collectionName: e,
494
586
  type: "update",
495
587
  data: { id: h.id, modifier: { $set: h } }
@@ -506,6 +598,6 @@ ${e.map((t) => `${t.id}: ${t.error.message}`).join(`
506
598
  }
507
599
  }
508
600
  export {
509
- T as SyncManager
601
+ q as SyncManager
510
602
  };
511
603
  //# sourceMappingURL=index.mjs.map