@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/SyncManager.d.ts +49 -6
- package/dist/index.mjs +274 -222
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +2 -2
- package/dist/index.umd.js.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
var
|
|
2
|
-
var v = (
|
|
3
|
-
var
|
|
4
|
-
import { isEqual as
|
|
5
|
-
function
|
|
6
|
-
let
|
|
7
|
-
const { leading:
|
|
8
|
-
function
|
|
9
|
-
const
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
}, e),
|
|
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
|
|
14
|
+
return r;
|
|
15
15
|
}
|
|
16
|
-
class
|
|
16
|
+
class M {
|
|
17
17
|
constructor() {
|
|
18
|
-
|
|
19
|
-
|
|
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,
|
|
28
|
-
this.queue.push(() => e().then(t).catch((
|
|
29
|
-
throw
|
|
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
|
|
55
|
-
const t = [],
|
|
56
|
-
for (const [
|
|
57
|
-
const
|
|
58
|
-
|
|
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 [
|
|
61
|
-
|
|
62
|
-
return { added: t, modified:
|
|
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
|
|
64
|
+
function C(c, e) {
|
|
65
65
|
if (e.items != null)
|
|
66
66
|
return e.items;
|
|
67
|
-
const t =
|
|
68
|
-
return e.changes.added.forEach((
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
}), e.changes.modified.forEach((
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
}), e.changes.removed.forEach((
|
|
75
|
-
const
|
|
76
|
-
|
|
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
|
|
80
|
-
const t = new
|
|
79
|
+
async function P(c, e) {
|
|
80
|
+
const t = new y();
|
|
81
81
|
t.batch(() => {
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
t.removeOne({ id:
|
|
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
|
|
88
|
-
if (
|
|
89
|
-
|
|
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
|
-
|
|
92
|
+
o ? t.updateOne(a, n.data.modifier) : t.insert(E(a, n.data.modifier));
|
|
93
93
|
});
|
|
94
94
|
});
|
|
95
|
-
const
|
|
96
|
-
return await t.dispose(),
|
|
95
|
+
const s = t.find().fetch();
|
|
96
|
+
return await t.dispose(), s;
|
|
97
97
|
}
|
|
98
|
-
function
|
|
99
|
-
return
|
|
98
|
+
function O(c) {
|
|
99
|
+
return c.added.length > 0 || c.modified.length > 0 || c.removed.length > 0;
|
|
100
100
|
}
|
|
101
|
-
function
|
|
102
|
-
return
|
|
101
|
+
function I(c, e) {
|
|
102
|
+
return O(g(c, e));
|
|
103
103
|
}
|
|
104
|
-
async function
|
|
105
|
-
let
|
|
106
|
-
if (
|
|
107
|
-
const
|
|
108
|
-
if (
|
|
109
|
-
const
|
|
110
|
-
|
|
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
|
|
114
|
-
return
|
|
115
|
-
|
|
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
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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 =
|
|
146
|
-
|
|
147
|
-
|
|
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:
|
|
152
|
+
persistence: s == null ? void 0 : s.adapter,
|
|
150
153
|
reactivity: t
|
|
151
|
-
}), this.snapshots = new
|
|
154
|
+
}), this.snapshots = new y({
|
|
152
155
|
name: `${this.options.id}-snapshots`,
|
|
153
|
-
persistence:
|
|
156
|
+
persistence: n == null ? void 0 : n.adapter,
|
|
154
157
|
reactivity: t
|
|
155
|
-
}), this.syncOperations = new
|
|
158
|
+
}), this.syncOperations = new y({
|
|
156
159
|
name: `${this.options.id}-sync-operations`,
|
|
157
|
-
persistence:
|
|
160
|
+
persistence: a == null ? void 0 : a.adapter,
|
|
158
161
|
reactivity: t
|
|
159
|
-
}), this.changes.on("persistence.error", (
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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}`, (
|
|
191
|
-
t =
|
|
175
|
+
adapter: this.options.persistenceAdapter(`${this.id}-${e}`, (n) => {
|
|
176
|
+
t = n;
|
|
192
177
|
}),
|
|
193
|
-
handler: (
|
|
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
|
|
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.
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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
|
-
},
|
|
264
|
-
for (let
|
|
265
|
-
if (
|
|
266
|
-
this.remoteChanges.splice(
|
|
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", (
|
|
271
|
-
if (
|
|
272
|
-
|
|
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:
|
|
280
|
-
}), this.schedulePush(t.name);
|
|
281
|
-
}), e.on("changed", ({ id:
|
|
282
|
-
const
|
|
283
|
-
if (
|
|
284
|
-
|
|
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:
|
|
292
|
-
}), this.schedulePush(t.name);
|
|
293
|
-
}), e.on("removed", ({ id:
|
|
294
|
-
if (
|
|
295
|
-
|
|
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:
|
|
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((
|
|
317
|
-
e.push({ id: t, error:
|
|
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
|
|
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,
|
|
358
|
-
let
|
|
359
|
-
await new Promise((
|
|
360
|
-
setTimeout(
|
|
409
|
+
}).count() > 0, o = Date.now();
|
|
410
|
+
let r = null;
|
|
411
|
+
await new Promise((i) => {
|
|
412
|
+
setTimeout(i, 0);
|
|
361
413
|
});
|
|
362
|
-
const
|
|
363
|
-
const
|
|
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:
|
|
419
|
+
{ time: { $lte: o } }
|
|
368
420
|
]
|
|
369
421
|
}, { sort: { time: 1 } }).count() === 0)
|
|
370
422
|
return;
|
|
371
|
-
|
|
372
|
-
start:
|
|
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
|
|
378
|
-
lastFinishedSyncStart:
|
|
379
|
-
lastFinishedSyncEnd:
|
|
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,
|
|
433
|
+
await this.syncWithData(e, u);
|
|
382
434
|
};
|
|
383
|
-
await (t != null && t.force ?
|
|
384
|
-
throw
|
|
385
|
-
$set: { status: "error", end: Date.now(), error:
|
|
386
|
-
})),
|
|
387
|
-
}),
|
|
388
|
-
id: { $ne:
|
|
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:
|
|
443
|
+
{ end: { $lte: o } },
|
|
392
444
|
{ status: "active" }
|
|
393
445
|
]
|
|
394
|
-
}), this.syncOperations.updateOne({ id:
|
|
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
|
|
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:
|
|
463
|
+
{ time: { $lte: a } }
|
|
412
464
|
]
|
|
413
465
|
}, { sort: { time: 1 } }).fetch();
|
|
414
|
-
await
|
|
415
|
-
changes:
|
|
416
|
-
lastSnapshot:
|
|
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: (
|
|
423
|
-
insert: (
|
|
424
|
-
if (
|
|
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:
|
|
429
|
-
}), s.updateOne({ id:
|
|
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:
|
|
436
|
-
}), s.insert(
|
|
487
|
+
data: i
|
|
488
|
+
}), s.insert(i);
|
|
437
489
|
},
|
|
438
|
-
update: (
|
|
439
|
-
if (
|
|
440
|
-
const
|
|
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:
|
|
445
|
-
}), s.insert(
|
|
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:
|
|
452
|
-
}), s.updateOne({ id:
|
|
503
|
+
data: { id: i, modifier: u }
|
|
504
|
+
}), s.updateOne({ id: i }, u);
|
|
453
505
|
},
|
|
454
|
-
remove: (
|
|
455
|
-
s.findOne({ id:
|
|
506
|
+
remove: (i) => {
|
|
507
|
+
s.findOne({ id: i }) && (this.remoteChanges.push({
|
|
456
508
|
collectionName: e,
|
|
457
509
|
type: "remove",
|
|
458
|
-
data:
|
|
459
|
-
}), s.removeOne({ id:
|
|
510
|
+
data: i
|
|
511
|
+
}), s.removeOne({ id: i }));
|
|
460
512
|
},
|
|
461
|
-
batch: (
|
|
513
|
+
batch: (i) => {
|
|
462
514
|
s.batch(() => {
|
|
463
|
-
|
|
515
|
+
i();
|
|
464
516
|
});
|
|
465
517
|
}
|
|
466
|
-
}).then(async (
|
|
467
|
-
if (this.snapshots.removeMany({ collectionName: e, time: { $lte:
|
|
518
|
+
}).then(async (i) => {
|
|
519
|
+
if (this.snapshots.removeMany({ collectionName: e, time: { $lte: a } }), this.changes.removeMany({
|
|
468
520
|
collectionName: e,
|
|
469
|
-
id: { $in:
|
|
521
|
+
id: { $in: d.map((h) => h.id) }
|
|
470
522
|
}), this.snapshots.insert({
|
|
471
|
-
time:
|
|
523
|
+
time: a,
|
|
472
524
|
collectionName: e,
|
|
473
|
-
items:
|
|
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
|
|
486
|
-
id: { $nin:
|
|
537
|
+
const p = s.find({
|
|
538
|
+
id: { $nin: i.map((h) => h.id) }
|
|
487
539
|
}).map((h) => h.id);
|
|
488
540
|
s.batch(() => {
|
|
489
|
-
|
|
490
|
-
const
|
|
541
|
+
i.forEach((h) => {
|
|
542
|
+
const f = !!s.findOne({ id: h.id });
|
|
491
543
|
/* istanbul ignore else -- @preserve */
|
|
492
|
-
|
|
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
|
-
}),
|
|
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
|
-
|
|
561
|
+
q as SyncManager
|
|
510
562
|
};
|
|
511
563
|
//# sourceMappingURL=index.mjs.map
|