@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/SyncManager.d.ts +1 -0
- package/dist/applyChanges.d.ts +1 -1
- package/dist/index.mjs +231 -209
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +3 -3
- package/dist/index.umd.js.map +1 -1
- package/dist/utils/debounce.d.ts +2 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
16
|
+
class I {
|
|
14
17
|
constructor() {
|
|
15
|
-
this
|
|
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(
|
|
23
|
-
return new Promise((
|
|
24
|
-
this.queue.push(() =>
|
|
25
|
-
throw
|
|
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
|
|
43
|
-
|
|
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
|
|
51
|
-
const
|
|
52
|
-
for (const [o,
|
|
53
|
-
const
|
|
54
|
-
|
|
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,
|
|
57
|
-
|
|
58
|
-
return { added:
|
|
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
|
|
61
|
-
if (
|
|
62
|
-
return
|
|
63
|
-
const
|
|
64
|
-
return
|
|
65
|
-
const s =
|
|
66
|
-
s
|
|
67
|
-
}),
|
|
68
|
-
const s =
|
|
69
|
-
s
|
|
70
|
-
}),
|
|
71
|
-
const s =
|
|
72
|
-
s !== -1 &&
|
|
73
|
-
}),
|
|
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
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (
|
|
80
|
-
|
|
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
|
|
84
|
-
if (
|
|
85
|
-
|
|
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
|
-
|
|
92
|
+
i ? t.updateOne(n, s.data.modifier) : t.insert(D(n, s.data.modifier));
|
|
89
93
|
});
|
|
90
|
-
})
|
|
94
|
+
});
|
|
95
|
+
const a = t.find().fetch();
|
|
96
|
+
return await t.dispose(), a;
|
|
91
97
|
}
|
|
92
|
-
function
|
|
93
|
-
return
|
|
98
|
+
function N(d) {
|
|
99
|
+
return d.added.length > 0 || d.modified.length > 0 || d.removed.length > 0;
|
|
94
100
|
}
|
|
95
|
-
function
|
|
96
|
-
return
|
|
101
|
+
function x(d, e) {
|
|
102
|
+
return N(w(d, e));
|
|
97
103
|
}
|
|
98
|
-
async function
|
|
99
|
-
let
|
|
100
|
-
if (
|
|
101
|
-
const h =
|
|
102
|
-
if (
|
|
103
|
-
const y =
|
|
104
|
-
C
|
|
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
|
|
108
|
-
return
|
|
109
|
-
|
|
110
|
-
}),
|
|
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
|
|
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(
|
|
124
|
-
|
|
125
|
-
|
|
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)
|
|
128
|
-
|
|
129
|
-
this.changes =
|
|
130
|
-
|
|
131
|
-
|
|
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:
|
|
138
|
-
}), this.
|
|
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 (!
|
|
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 (!
|
|
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(
|
|
164
|
-
var e;
|
|
184
|
+
createPersistenceAdapter(e) {
|
|
165
185
|
if (this.options.persistenceAdapter == null)
|
|
166
186
|
return;
|
|
167
|
-
|
|
168
|
-
let s = () => {
|
|
187
|
+
let t = () => {
|
|
169
188
|
};
|
|
170
189
|
return {
|
|
171
|
-
adapter: this.options.persistenceAdapter(`${
|
|
172
|
-
|
|
190
|
+
adapter: this.options.persistenceAdapter(`${this.id}-${e}`, (s) => {
|
|
191
|
+
t = s;
|
|
173
192
|
}),
|
|
174
|
-
handler: (
|
|
193
|
+
handler: (s) => t(s)
|
|
175
194
|
};
|
|
176
195
|
}
|
|
177
|
-
getSyncQueue(
|
|
178
|
-
return this.syncQueues.get(
|
|
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(
|
|
197
|
-
const
|
|
198
|
-
if (
|
|
199
|
-
throw new Error(`Collection with id '${
|
|
200
|
-
return
|
|
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(
|
|
227
|
+
addCollection(e, t) {
|
|
209
228
|
if (this.isDisposed)
|
|
210
229
|
throw new Error("SyncManager is disposed");
|
|
211
|
-
this.options.registerRemoteChange && this.options.registerRemoteChange(
|
|
212
|
-
if (
|
|
213
|
-
await this.sync(
|
|
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:
|
|
236
|
+
collectionName: t.name,
|
|
218
237
|
instanceId: this.instanceId,
|
|
219
238
|
status: "active"
|
|
220
239
|
});
|
|
221
|
-
await this.syncWithData(
|
|
240
|
+
await this.syncWithData(t.name, n).then(() => {
|
|
222
241
|
this.syncOperations.removeMany({
|
|
223
242
|
id: { $ne: o },
|
|
224
|
-
collectionName:
|
|
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((
|
|
233
|
-
throw this.options.onError && this.options.onError(
|
|
234
|
-
$set: { status: "error", end: Date.now(), error:
|
|
235
|
-
}),
|
|
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(
|
|
239
|
-
const
|
|
257
|
+
}), this.collections.set(t.name, [e, t]);
|
|
258
|
+
const a = (n) => {
|
|
240
259
|
for (const i of this.remoteChanges)
|
|
241
|
-
if (
|
|
260
|
+
if (g(i, n))
|
|
242
261
|
return !0;
|
|
243
262
|
return !1;
|
|
244
|
-
}, s = (
|
|
263
|
+
}, s = (n) => {
|
|
245
264
|
for (let i = 0; i < this.remoteChanges.length; i += 1)
|
|
246
|
-
if (
|
|
265
|
+
if (g(this.remoteChanges[i], n)) {
|
|
247
266
|
this.remoteChanges.splice(i, 1);
|
|
248
267
|
return;
|
|
249
268
|
}
|
|
250
269
|
};
|
|
251
|
-
|
|
252
|
-
if (
|
|
253
|
-
s({ collectionName:
|
|
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:
|
|
276
|
+
collectionName: t.name,
|
|
258
277
|
time: Date.now(),
|
|
259
278
|
type: "insert",
|
|
260
|
-
data:
|
|
261
|
-
}), this.schedulePush(
|
|
262
|
-
}),
|
|
263
|
-
const o = { id:
|
|
264
|
-
if (
|
|
265
|
-
s({ collectionName:
|
|
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:
|
|
288
|
+
collectionName: t.name,
|
|
270
289
|
time: Date.now(),
|
|
271
290
|
type: "update",
|
|
272
291
|
data: o
|
|
273
|
-
}), this.schedulePush(
|
|
274
|
-
}),
|
|
275
|
-
if (
|
|
276
|
-
s({ collectionName:
|
|
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:
|
|
299
|
+
collectionName: t.name,
|
|
281
300
|
time: Date.now(),
|
|
282
301
|
type: "remove",
|
|
283
|
-
data:
|
|
284
|
-
}), this.schedulePush(
|
|
302
|
+
data: n
|
|
303
|
+
}), this.schedulePush(t.name);
|
|
285
304
|
});
|
|
286
305
|
}
|
|
287
|
-
schedulePush(
|
|
288
|
-
this.deboucedPush(
|
|
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
|
|
297
|
-
if (await Promise.all([...this.collections.keys()].map((
|
|
298
|
-
|
|
299
|
-
}))),
|
|
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
|
-
${
|
|
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(
|
|
311
|
-
return this.syncOperations.findOne(
|
|
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(
|
|
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(
|
|
332
|
-
collectionName:
|
|
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((
|
|
338
|
-
setTimeout(
|
|
359
|
+
await new Promise((c) => {
|
|
360
|
+
setTimeout(c, 0);
|
|
339
361
|
});
|
|
340
|
-
const
|
|
341
|
-
const
|
|
342
|
-
if (
|
|
343
|
-
collectionName:
|
|
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
|
-
|
|
371
|
+
n || (o = this.syncOperations.insert({
|
|
350
372
|
start: i,
|
|
351
|
-
collectionName:
|
|
373
|
+
collectionName: e,
|
|
352
374
|
instanceId: this.instanceId,
|
|
353
375
|
status: "active"
|
|
354
376
|
}));
|
|
355
|
-
const
|
|
356
|
-
lastFinishedSyncStart:
|
|
357
|
-
lastFinishedSyncEnd:
|
|
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(
|
|
381
|
+
await this.syncWithData(e, r);
|
|
360
382
|
};
|
|
361
|
-
await (
|
|
362
|
-
throw o != null && (this.options.onError && this.options.onError(s,
|
|
363
|
-
$set: { status: "error", end: Date.now(), error:
|
|
364
|
-
})),
|
|
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:
|
|
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(
|
|
381
|
-
await this.sync(
|
|
402
|
+
async pushChanges(e) {
|
|
403
|
+
await this.sync(e, {
|
|
382
404
|
onlyWithChanges: !0
|
|
383
405
|
});
|
|
384
406
|
}
|
|
385
|
-
async syncWithData(
|
|
386
|
-
const
|
|
387
|
-
collectionName:
|
|
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
|
|
393
|
-
changes:
|
|
394
|
-
lastSnapshot:
|
|
395
|
-
data:
|
|
396
|
-
pull: () => this.options.pull(
|
|
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: (
|
|
401
|
-
insert: (
|
|
402
|
-
if (
|
|
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:
|
|
426
|
+
collectionName: e,
|
|
405
427
|
type: "update",
|
|
406
|
-
data: { id:
|
|
407
|
-
}), s.updateOne({ id:
|
|
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:
|
|
433
|
+
collectionName: e,
|
|
412
434
|
type: "insert",
|
|
413
|
-
data:
|
|
414
|
-
}), s.insert(
|
|
435
|
+
data: r
|
|
436
|
+
}), s.insert(r);
|
|
415
437
|
},
|
|
416
|
-
update: (
|
|
417
|
-
if (
|
|
418
|
-
const
|
|
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:
|
|
442
|
+
collectionName: e,
|
|
421
443
|
type: "insert",
|
|
422
|
-
data:
|
|
423
|
-
}), s.insert(
|
|
444
|
+
data: f
|
|
445
|
+
}), s.insert(f);
|
|
424
446
|
return;
|
|
425
447
|
}
|
|
426
448
|
this.remoteChanges.push({
|
|
427
|
-
collectionName:
|
|
449
|
+
collectionName: e,
|
|
428
450
|
type: "update",
|
|
429
|
-
data: { id:
|
|
430
|
-
}), s.updateOne({ id:
|
|
451
|
+
data: { id: r, modifier: p }
|
|
452
|
+
}), s.updateOne({ id: r }, p);
|
|
431
453
|
},
|
|
432
|
-
remove: (
|
|
433
|
-
s.findOne({ id:
|
|
434
|
-
collectionName:
|
|
454
|
+
remove: (r) => {
|
|
455
|
+
s.findOne({ id: r }) && (this.remoteChanges.push({
|
|
456
|
+
collectionName: e,
|
|
435
457
|
type: "remove",
|
|
436
|
-
data:
|
|
437
|
-
}), s.removeOne({ id:
|
|
458
|
+
data: r
|
|
459
|
+
}), s.removeOne({ id: r }));
|
|
438
460
|
},
|
|
439
|
-
batch: (
|
|
461
|
+
batch: (r) => {
|
|
440
462
|
s.batch(() => {
|
|
441
|
-
|
|
463
|
+
r();
|
|
442
464
|
});
|
|
443
465
|
}
|
|
444
|
-
}).then(async (
|
|
445
|
-
if (this.snapshots.removeMany({ collectionName:
|
|
446
|
-
collectionName:
|
|
447
|
-
id: { $in:
|
|
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:
|
|
451
|
-
items:
|
|
472
|
+
collectionName: e,
|
|
473
|
+
items: r
|
|
452
474
|
}), await new Promise((h) => {
|
|
453
475
|
setTimeout(h, 0);
|
|
454
476
|
}), this.changes.find({
|
|
455
|
-
collectionName:
|
|
477
|
+
collectionName: e
|
|
456
478
|
}).count() > 0) {
|
|
457
|
-
await this.sync(
|
|
479
|
+
await this.sync(e, {
|
|
458
480
|
force: !0,
|
|
459
481
|
onlyWithChanges: !0
|
|
460
482
|
});
|
|
461
483
|
return;
|
|
462
484
|
}
|
|
463
|
-
const
|
|
464
|
-
id: { $nin:
|
|
485
|
+
const f = s.find({
|
|
486
|
+
id: { $nin: r.map((h) => h.id) }
|
|
465
487
|
}).map((h) => h.id);
|
|
466
488
|
s.batch(() => {
|
|
467
|
-
|
|
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:
|
|
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:
|
|
497
|
+
collectionName: e,
|
|
476
498
|
type: "insert",
|
|
477
499
|
data: h
|
|
478
500
|
}), s.insert(h));
|
|
479
|
-
}),
|
|
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
|
-
|
|
509
|
+
T as SyncManager
|
|
488
510
|
};
|
|
489
511
|
//# sourceMappingURL=index.mjs.map
|