@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/SyncManager.d.ts +49 -6
- package/dist/index.mjs +301 -209
- 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,19 +1,19 @@
|
|
|
1
1
|
var E = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var u = (
|
|
4
|
-
import { isEqual as
|
|
5
|
-
function
|
|
6
|
-
let
|
|
7
|
-
const { leading:
|
|
8
|
-
function
|
|
9
|
-
const
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
}, e),
|
|
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
|
|
14
|
+
return r;
|
|
15
15
|
}
|
|
16
|
-
class
|
|
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,
|
|
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, 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 [
|
|
61
|
-
|
|
62
|
-
return { added: t, modified:
|
|
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
|
|
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(N(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 v(c) {
|
|
99
|
+
return c.added.length > 0 || c.modified.length > 0 || c.removed.length > 0;
|
|
100
100
|
}
|
|
101
|
-
function
|
|
102
|
-
return
|
|
101
|
+
function M(c, e) {
|
|
102
|
+
return v(g(c, e));
|
|
103
103
|
}
|
|
104
|
-
async function
|
|
105
|
-
let
|
|
106
|
-
if (
|
|
107
|
-
const
|
|
108
|
-
if (
|
|
109
|
-
const
|
|
110
|
-
|
|
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
|
|
113
|
+
const h = i.changes == null ? g(d, i.items) : i.changes;
|
|
114
114
|
return l(() => {
|
|
115
|
-
|
|
116
|
-
}),
|
|
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
|
|
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",
|
|
139
|
+
u(this, "instanceId", D());
|
|
140
140
|
u(this, "id");
|
|
141
|
-
u(this, "deboucedPush",
|
|
141
|
+
u(this, "deboucedPush", x((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 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.
|
|
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,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
|
|
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
|
-
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
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
|
|
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:
|
|
427
|
+
{ time: { $lte: o } }
|
|
368
428
|
]
|
|
369
|
-
}, {
|
|
429
|
+
}, {
|
|
430
|
+
sort: { time: 1 },
|
|
431
|
+
reactive: !1
|
|
432
|
+
}).count() === 0)
|
|
370
433
|
return;
|
|
371
|
-
|
|
372
|
-
start:
|
|
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
|
|
378
|
-
lastFinishedSyncStart:
|
|
379
|
-
lastFinishedSyncEnd:
|
|
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,
|
|
444
|
+
await this.syncWithData(e, d);
|
|
382
445
|
};
|
|
383
|
-
await (t != null && t.force ? l() : this.getSyncQueue(e).add(l)).catch((
|
|
384
|
-
throw
|
|
385
|
-
$set: { status: "error", end: Date.now(), error:
|
|
386
|
-
})),
|
|
387
|
-
}),
|
|
388
|
-
id: { $ne:
|
|
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:
|
|
454
|
+
{ end: { $lte: o } },
|
|
392
455
|
{ status: "active" }
|
|
393
456
|
]
|
|
394
|
-
}), this.syncOperations.updateOne({ id:
|
|
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
|
|
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:
|
|
485
|
+
{ time: { $lte: a } }
|
|
412
486
|
]
|
|
413
|
-
}, {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
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: (
|
|
423
|
-
insert: (
|
|
424
|
-
if (
|
|
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:
|
|
429
|
-
}), s.updateOne({ id:
|
|
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:
|
|
436
|
-
}), s.insert(
|
|
514
|
+
data: i
|
|
515
|
+
}), s.insert(i);
|
|
437
516
|
},
|
|
438
|
-
update: (
|
|
439
|
-
if (
|
|
440
|
-
|
|
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:
|
|
445
|
-
}), s.insert(
|
|
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:
|
|
452
|
-
}), s.updateOne({ id:
|
|
532
|
+
data: { id: i, modifier: d }
|
|
533
|
+
}), s.updateOne({ id: i }, d);
|
|
453
534
|
},
|
|
454
|
-
remove: (
|
|
455
|
-
s.findOne({
|
|
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:
|
|
459
|
-
}), s.removeOne({ id:
|
|
541
|
+
data: i
|
|
542
|
+
}), s.removeOne({ id: i }));
|
|
460
543
|
},
|
|
461
|
-
batch: (
|
|
544
|
+
batch: (i) => {
|
|
462
545
|
s.batch(() => {
|
|
463
|
-
|
|
546
|
+
i();
|
|
464
547
|
});
|
|
465
548
|
}
|
|
466
|
-
}).then(async (
|
|
467
|
-
if (this.snapshots.removeMany({
|
|
549
|
+
}).then(async (i) => {
|
|
550
|
+
if (this.snapshots.removeMany({
|
|
468
551
|
collectionName: e,
|
|
469
|
-
|
|
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:
|
|
557
|
+
time: a,
|
|
472
558
|
collectionName: e,
|
|
473
|
-
items:
|
|
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:
|
|
572
|
+
id: { $nin: i.map((h) => h.id) }
|
|
573
|
+
}, {
|
|
574
|
+
reactive: !1
|
|
487
575
|
}).map((h) => h.id);
|
|
488
576
|
s.batch(() => {
|
|
489
|
-
|
|
490
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
601
|
+
q as SyncManager
|
|
510
602
|
};
|
|
511
603
|
//# sourceMappingURL=index.mjs.map
|