dexie-cloud-addon 4.1.0-beta.44 → 4.1.0-beta.46
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/modern/WSObservable.d.ts +4 -3
- package/dist/modern/dexie-cloud-addon.js +220 -130
- package/dist/modern/dexie-cloud-addon.js.map +1 -1
- package/dist/modern/dexie-cloud-addon.min.js +1 -1
- package/dist/modern/dexie-cloud-addon.min.js.map +1 -1
- package/dist/modern/service-worker.js +206 -124
- package/dist/modern/service-worker.js.map +1 -1
- package/dist/modern/service-worker.min.js +1 -1
- package/dist/modern/service-worker.min.js.map +1 -1
- package/dist/modern/yjs/getUpdatesTable.d.ts +1 -1
- package/dist/umd/WSObservable.d.ts +4 -3
- package/dist/umd/dexie-cloud-addon.js +220 -130
- package/dist/umd/dexie-cloud-addon.js.map +1 -1
- package/dist/umd/dexie-cloud-addon.min.js +1 -1
- package/dist/umd/dexie-cloud-addon.min.js.map +1 -1
- package/dist/umd/service-worker.js +206 -124
- package/dist/umd/service-worker.js.map +1 -1
- package/dist/umd/service-worker.min.js +1 -1
- package/dist/umd/service-worker.min.js.map +1 -1
- package/dist/umd/yjs/getUpdatesTable.d.ts +1 -1
- package/package.json +3 -3
- package/dist/modern/helpers/dbOnClosed.d.ts +0 -2
- package/dist/umd/helpers/dbOnClosed.d.ts +0 -2
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* ==========================================================================
|
|
10
10
|
*
|
|
11
|
-
* Version 4.1.0-beta.
|
|
11
|
+
* Version 4.1.0-beta.46, Thu Apr 03 2025
|
|
12
12
|
*
|
|
13
13
|
* https://dexie.org
|
|
14
14
|
*
|
|
@@ -2802,6 +2802,7 @@
|
|
|
2802
2802
|
break;
|
|
2803
2803
|
case 'u-s':
|
|
2804
2804
|
writeVarUint8Array(encoder, msg.u);
|
|
2805
|
+
writeVarString(encoder, msg.r || '');
|
|
2805
2806
|
break;
|
|
2806
2807
|
}
|
|
2807
2808
|
}
|
|
@@ -3188,7 +3189,8 @@
|
|
|
3188
3189
|
table,
|
|
3189
3190
|
prop,
|
|
3190
3191
|
k,
|
|
3191
|
-
u: readVarUint8Array(decoder)
|
|
3192
|
+
u: readVarUint8Array(decoder),
|
|
3193
|
+
r: (decoder.pos < decoder.arr.length && readVarString(decoder)) || undefined,
|
|
3192
3194
|
};
|
|
3193
3195
|
default:
|
|
3194
3196
|
throw new TypeError(`Unknown message type: ${type}`);
|
|
@@ -4465,6 +4467,7 @@
|
|
|
4465
4467
|
baseRevs,
|
|
4466
4468
|
changes: encodeIdsForServer(db.dx.core.schema, currentUser, changes),
|
|
4467
4469
|
y,
|
|
4470
|
+
dxcv: db.cloud.version
|
|
4468
4471
|
};
|
|
4469
4472
|
console.debug('Sync request', syncRequest);
|
|
4470
4473
|
db.syncStateChangedEvent.next({
|
|
@@ -4607,9 +4610,11 @@
|
|
|
4607
4610
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4608
4611
|
console.debug('Applying server changes', changes, Dexie.currentTransaction);
|
|
4609
4612
|
for (const { table: tableName, muts } of changes) {
|
|
4613
|
+
if (!db.dx._allTables[tableName]) {
|
|
4614
|
+
console.debug(`Server sent changes for table ${tableName} that we don't have. Ignoring.`);
|
|
4615
|
+
continue;
|
|
4616
|
+
}
|
|
4610
4617
|
const table = db.table(tableName);
|
|
4611
|
-
if (!table)
|
|
4612
|
-
continue; // If server sends changes on a table we don't have, ignore it.
|
|
4613
4618
|
const { primaryKey } = table.core.schema;
|
|
4614
4619
|
const keyDecoder = (key) => {
|
|
4615
4620
|
switch (key[0]) {
|
|
@@ -4795,9 +4800,15 @@
|
|
|
4795
4800
|
|
|
4796
4801
|
function getUpdatesTable(db, table, ydocProp) {
|
|
4797
4802
|
var _a, _b, _c;
|
|
4803
|
+
if (!db.dx._allTables[table])
|
|
4804
|
+
return undefined;
|
|
4798
4805
|
const utbl = (_c = (_b = (_a = db.table(table)) === null || _a === void 0 ? void 0 : _a.schema.yProps) === null || _b === void 0 ? void 0 : _b.find(p => p.prop === ydocProp)) === null || _c === void 0 ? void 0 : _c.updatesTable;
|
|
4799
|
-
if (!utbl)
|
|
4800
|
-
|
|
4806
|
+
if (!utbl) {
|
|
4807
|
+
console.debug(`No updatesTable found for ${table}.${ydocProp}`);
|
|
4808
|
+
return undefined;
|
|
4809
|
+
}
|
|
4810
|
+
if (!db.dx._allTables[utbl])
|
|
4811
|
+
return undefined;
|
|
4801
4812
|
return db.table(utbl);
|
|
4802
4813
|
}
|
|
4803
4814
|
|
|
@@ -4808,74 +4819,91 @@
|
|
|
4808
4819
|
let resyncNeeded = false;
|
|
4809
4820
|
let yServerRevision;
|
|
4810
4821
|
for (const m of yMessages) {
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
|
|
4814
|
-
|
|
4815
|
-
|
|
4816
|
-
|
|
4817
|
-
|
|
4818
|
-
|
|
4819
|
-
|
|
4820
|
-
|
|
4821
|
-
|
|
4822
|
-
|
|
4823
|
-
|
|
4824
|
-
|
|
4825
|
-
.
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
break;
|
|
4829
|
-
}
|
|
4830
|
-
case 'u-reject': {
|
|
4831
|
-
// Acces control or constraint rejected the update.
|
|
4832
|
-
// We delete it. It's not going to be sent again.
|
|
4833
|
-
// What's missing is a way to notify consumers, such as Tiptap editor, that the update was rejected.
|
|
4834
|
-
// This is only an issue when the document is open. We could find the open document and
|
|
4835
|
-
// in a perfect world, we should send a reverse update to the open document to undo the change.
|
|
4836
|
-
// See my question in https://discuss.yjs.dev/t/generate-an-inverse-update/2765
|
|
4837
|
-
console.debug(`Y update rejected. Deleting it.`);
|
|
4838
|
-
const utbl = getUpdatesTable(db, m.table, m.prop);
|
|
4839
|
-
// Delete the rejected update and all local updates since (avoid holes in the CRDT)
|
|
4840
|
-
// and destroy it's open document if there is one.
|
|
4841
|
-
const primaryKey = (_a = (yield utbl.get(m.i))) === null || _a === void 0 ? void 0 : _a.k;
|
|
4842
|
-
if (primaryKey != null) {
|
|
4843
|
-
yield db.transaction('rw', utbl, (tx) => {
|
|
4844
|
-
// @ts-ignore
|
|
4845
|
-
tx.idbtrans._rejecting_y_ypdate = true; // Inform ydoc triggers that we delete because of a rejection and not GC
|
|
4846
|
-
return utbl
|
|
4847
|
-
.where('i')
|
|
4848
|
-
.aboveOrEqual(m.i)
|
|
4849
|
-
.filter((u) => Dexie.cmp(u.k, primaryKey) === 0 && ((u.f || 0) & 1) === 1)
|
|
4850
|
-
.delete();
|
|
4851
|
-
});
|
|
4852
|
-
// Destroy active doc
|
|
4853
|
-
const activeDoc = Dexie.DexieYProvider.getDocCache(db.dx).find(m.table, primaryKey, m.prop);
|
|
4854
|
-
if (activeDoc)
|
|
4855
|
-
activeDoc.destroy(); // Destroy the document so that editors don't continue to work on it
|
|
4822
|
+
try {
|
|
4823
|
+
switch (m.type) {
|
|
4824
|
+
case 'u-s': {
|
|
4825
|
+
const utbl = getUpdatesTable(db, m.table, m.prop);
|
|
4826
|
+
if (utbl) {
|
|
4827
|
+
const updateRow = {
|
|
4828
|
+
k: m.k,
|
|
4829
|
+
u: m.u,
|
|
4830
|
+
};
|
|
4831
|
+
if (m.r) {
|
|
4832
|
+
// @ts-ignore
|
|
4833
|
+
updateRow.r = m.r;
|
|
4834
|
+
yServerRevision = m.r;
|
|
4835
|
+
}
|
|
4836
|
+
receivedUntils[utbl.name] = yield utbl.add(updateRow);
|
|
4837
|
+
}
|
|
4838
|
+
break;
|
|
4856
4839
|
}
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4840
|
+
case 'u-ack': {
|
|
4841
|
+
const utbl = getUpdatesTable(db, m.table, m.prop);
|
|
4842
|
+
if (utbl) {
|
|
4843
|
+
yield db.transaction('rw', utbl, (tx) => __awaiter(this, void 0, void 0, function* () {
|
|
4844
|
+
let syncer = (yield tx
|
|
4845
|
+
.table(utbl.name)
|
|
4846
|
+
.get(DEXIE_CLOUD_SYNCER_ID));
|
|
4847
|
+
yield tx.table(utbl.name).put(Object.assign(Object.assign({}, (syncer || { i: DEXIE_CLOUD_SYNCER_ID })), { unsentFrom: Math.max((syncer === null || syncer === void 0 ? void 0 : syncer.unsentFrom) || 1, m.i + 1) }));
|
|
4848
|
+
}));
|
|
4849
|
+
}
|
|
4850
|
+
break;
|
|
4863
4851
|
}
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4852
|
+
case 'u-reject': {
|
|
4853
|
+
// Acces control or constraint rejected the update.
|
|
4854
|
+
// We delete it. It's not going to be sent again.
|
|
4855
|
+
// What's missing is a way to notify consumers, such as Tiptap editor, that the update was rejected.
|
|
4856
|
+
// This is only an issue when the document is open. We could find the open document and
|
|
4857
|
+
// in a perfect world, we should send a reverse update to the open document to undo the change.
|
|
4858
|
+
// See my question in https://discuss.yjs.dev/t/generate-an-inverse-update/2765
|
|
4859
|
+
console.debug(`Y update rejected. Deleting it.`);
|
|
4860
|
+
const utbl = getUpdatesTable(db, m.table, m.prop);
|
|
4861
|
+
if (!utbl)
|
|
4862
|
+
break;
|
|
4863
|
+
// Delete the rejected update and all local updates since (avoid holes in the CRDT)
|
|
4864
|
+
// and destroy it's open document if there is one.
|
|
4865
|
+
const primaryKey = (_a = (yield utbl.get(m.i))) === null || _a === void 0 ? void 0 : _a.k;
|
|
4866
|
+
if (primaryKey != null) {
|
|
4867
|
+
yield db.transaction('rw', utbl, (tx) => {
|
|
4868
|
+
// @ts-ignore
|
|
4869
|
+
tx.idbtrans._rejecting_y_ypdate = true; // Inform ydoc triggers that we delete because of a rejection and not GC
|
|
4870
|
+
return utbl
|
|
4871
|
+
.where('i')
|
|
4872
|
+
.aboveOrEqual(m.i)
|
|
4873
|
+
.filter((u) => Dexie.cmp(u.k, primaryKey) === 0 && ((u.f || 0) & 1) === 1)
|
|
4874
|
+
.delete();
|
|
4875
|
+
});
|
|
4876
|
+
// Destroy active doc
|
|
4877
|
+
const activeDoc = Dexie.DexieYProvider.getDocCache(db.dx).find(m.table, primaryKey, m.prop);
|
|
4878
|
+
if (activeDoc)
|
|
4879
|
+
activeDoc.destroy(); // Destroy the document so that editors don't continue to work on it
|
|
4880
|
+
}
|
|
4881
|
+
break;
|
|
4882
|
+
}
|
|
4883
|
+
case 'in-sync': {
|
|
4884
|
+
const doc = Dexie.DexieYProvider.getDocCache(db.dx).find(m.table, m.k, m.prop);
|
|
4885
|
+
if (doc && !doc.isSynced) {
|
|
4886
|
+
doc.emit('sync', [true]);
|
|
4887
|
+
}
|
|
4888
|
+
break;
|
|
4889
|
+
}
|
|
4890
|
+
case 'y-complete-sync-done': {
|
|
4891
|
+
yServerRevision = m.yServerRev;
|
|
4892
|
+
break;
|
|
4893
|
+
}
|
|
4894
|
+
case 'outdated-server-rev':
|
|
4895
|
+
resyncNeeded = true;
|
|
4896
|
+
break;
|
|
4869
4897
|
}
|
|
4870
|
-
|
|
4871
|
-
|
|
4872
|
-
|
|
4898
|
+
}
|
|
4899
|
+
catch (e) {
|
|
4900
|
+
console.error(`Failed to apply YMessage`, m, e);
|
|
4873
4901
|
}
|
|
4874
4902
|
}
|
|
4875
4903
|
return {
|
|
4876
4904
|
receivedUntils,
|
|
4877
4905
|
resyncNeeded,
|
|
4878
|
-
yServerRevision
|
|
4906
|
+
yServerRevision,
|
|
4879
4907
|
};
|
|
4880
4908
|
});
|
|
4881
4909
|
}
|
|
@@ -4986,7 +5014,9 @@
|
|
|
4986
5014
|
throw new Error(`Protocol error from ${databaseUrl}/y/download`);
|
|
4987
5015
|
}
|
|
4988
5016
|
const yTable = getUpdatesTable(db, currentTable, currentProp);
|
|
4989
|
-
|
|
5017
|
+
if (yTable) {
|
|
5018
|
+
yield yTable.bulkAdd(docsToInsert);
|
|
5019
|
+
}
|
|
4990
5020
|
docsToInsert = [];
|
|
4991
5021
|
}
|
|
4992
5022
|
if (currentRealmId &&
|
|
@@ -5322,12 +5352,12 @@
|
|
|
5322
5352
|
return false; // Not needed anymore
|
|
5323
5353
|
});
|
|
5324
5354
|
}
|
|
5325
|
-
function deleteObjectsFromRemovedRealms(db, res,
|
|
5355
|
+
function deleteObjectsFromRemovedRealms(db, res, syncState) {
|
|
5326
5356
|
return __awaiter(this, void 0, void 0, function* () {
|
|
5327
5357
|
const deletedRealms = new Set();
|
|
5328
5358
|
const rejectedRealms = new Set();
|
|
5329
|
-
const previousRealmSet =
|
|
5330
|
-
const previousInviteRealmSet =
|
|
5359
|
+
const previousRealmSet = syncState ? syncState.realms : [];
|
|
5360
|
+
const previousInviteRealmSet = syncState ? syncState.inviteRealms : [];
|
|
5331
5361
|
const updatedRealmSet = new Set(res.realms);
|
|
5332
5362
|
const updatedTotalRealmSet = new Set(res.realms.concat(res.inviteRealms));
|
|
5333
5363
|
for (const realmId of previousRealmSet) {
|
|
@@ -5369,17 +5399,10 @@
|
|
|
5369
5399
|
}
|
|
5370
5400
|
}
|
|
5371
5401
|
}
|
|
5372
|
-
if (rejectedRealms.size > 0) {
|
|
5373
|
-
// Remove rejected/deleted realms from yDownloadedRealms because of the following use case:
|
|
5374
|
-
// 1. User becomes added to the realm
|
|
5375
|
-
// 2. User syncs and all documents of the realm is downloaded (downloadYDocsFromServer.ts)
|
|
5376
|
-
// 3. User leaves the realm and all docs are deleted locally (built-in-trigger of deleting their rows in this file)
|
|
5377
|
-
// 4. User is yet again added to the realm. At this point, we must make sure the docs are not considered already downloaded.
|
|
5378
|
-
const updateSpec = {};
|
|
5402
|
+
if (rejectedRealms.size > 0 && (syncState === null || syncState === void 0 ? void 0 : syncState.yDownloadedRealms)) {
|
|
5379
5403
|
for (const realmId of rejectedRealms) {
|
|
5380
|
-
|
|
5404
|
+
delete syncState.yDownloadedRealms[realmId];
|
|
5381
5405
|
}
|
|
5382
|
-
yield db.$syncState.update('syncState', updateSpec);
|
|
5383
5406
|
}
|
|
5384
5407
|
});
|
|
5385
5408
|
}
|
|
@@ -6902,13 +6925,13 @@
|
|
|
6902
6925
|
const CLIENT_PING_INTERVAL = 30000;
|
|
6903
6926
|
const FAIL_RETRY_WAIT_TIME = 60000;
|
|
6904
6927
|
class WSObservable extends rxjs.Observable {
|
|
6905
|
-
constructor(db, rev, realmSetHash, clientIdentity, messageProducer, webSocketStatus, user) {
|
|
6906
|
-
super((subscriber) => new WSConnection(db, rev, realmSetHash, clientIdentity, user, subscriber, messageProducer, webSocketStatus));
|
|
6928
|
+
constructor(db, rev, yrev, realmSetHash, clientIdentity, messageProducer, webSocketStatus, user) {
|
|
6929
|
+
super((subscriber) => new WSConnection(db, rev, yrev, realmSetHash, clientIdentity, user, subscriber, messageProducer, webSocketStatus));
|
|
6907
6930
|
}
|
|
6908
6931
|
}
|
|
6909
6932
|
let counter = 0;
|
|
6910
6933
|
class WSConnection extends rxjs.Subscription {
|
|
6911
|
-
constructor(db, rev, realmSetHash, clientIdentity, user, subscriber, messageProducer, webSocketStatus) {
|
|
6934
|
+
constructor(db, rev, yrev, realmSetHash, clientIdentity, user, subscriber, messageProducer, webSocketStatus) {
|
|
6912
6935
|
super(() => this.teardown());
|
|
6913
6936
|
this.id = ++counter;
|
|
6914
6937
|
this.subscriptions = new Set();
|
|
@@ -6917,6 +6940,7 @@
|
|
|
6917
6940
|
this.db = db;
|
|
6918
6941
|
this.databaseUrl = db.cloud.options.databaseUrl;
|
|
6919
6942
|
this.rev = rev;
|
|
6943
|
+
this.yrev = yrev;
|
|
6920
6944
|
this.realmSetHash = realmSetHash;
|
|
6921
6945
|
this.clientIdentity = clientIdentity;
|
|
6922
6946
|
this.user = user;
|
|
@@ -6986,6 +7010,11 @@
|
|
|
6986
7010
|
}
|
|
6987
7011
|
this.webSocketStatus.next('connecting');
|
|
6988
7012
|
this.pinger = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
7013
|
+
// setInterval here causes unnecessary pings when server is proved active anyway.
|
|
7014
|
+
// TODO: Use setTimout() here instead. When triggered, check if we really need to ping.
|
|
7015
|
+
// In case we've had server activity, we don't need to ping. Then schedule then next ping
|
|
7016
|
+
// to the time when we should ping next time (based on lastServerActivity + CLIENT_PING_INTERVAL).
|
|
7017
|
+
// Else, ping now and schedule next ping to CLIENT_PING_INTERVAL from now.
|
|
6989
7018
|
if (this.closed) {
|
|
6990
7019
|
console.debug('pinger check', this.id, 'CLOSED.');
|
|
6991
7020
|
this.teardown();
|
|
@@ -7032,9 +7061,13 @@
|
|
|
7032
7061
|
if (this.subscriber.closed)
|
|
7033
7062
|
return;
|
|
7034
7063
|
searchParams.set('v', '2');
|
|
7035
|
-
|
|
7064
|
+
if (this.rev)
|
|
7065
|
+
searchParams.set('rev', this.rev);
|
|
7066
|
+
if (this.yrev)
|
|
7067
|
+
searchParams.set('yrev', this.yrev);
|
|
7036
7068
|
searchParams.set('realmsHash', this.realmSetHash);
|
|
7037
7069
|
searchParams.set('clientId', this.clientIdentity);
|
|
7070
|
+
searchParams.set('dxcv', this.db.cloud.version);
|
|
7038
7071
|
if (this.user.accessToken) {
|
|
7039
7072
|
searchParams.set('token', this.user.accessToken);
|
|
7040
7073
|
}
|
|
@@ -7071,8 +7104,8 @@
|
|
|
7071
7104
|
}
|
|
7072
7105
|
}
|
|
7073
7106
|
}
|
|
7074
|
-
else if (msg.type === '
|
|
7075
|
-
|
|
7107
|
+
else if (msg.type === 'pong') {
|
|
7108
|
+
// Do nothing
|
|
7076
7109
|
}
|
|
7077
7110
|
else if (msg.type === 'doc-open') {
|
|
7078
7111
|
const docCache = Dexie.DexieYProvider.getDocCache(this.db.dx);
|
|
@@ -7081,11 +7114,26 @@
|
|
|
7081
7114
|
getOpenDocSignal(doc).next(); // Make yHandler reopen the document on server.
|
|
7082
7115
|
}
|
|
7083
7116
|
}
|
|
7084
|
-
else if (msg.type === 'outdated-server-rev' || msg.type === 'y-complete-sync-done') {
|
|
7085
|
-
|
|
7086
|
-
|
|
7117
|
+
else if (msg.type === 'u-ack' || msg.type === 'u-reject' || msg.type === 'u-s' || msg.type === 'in-sync' || msg.type === 'outdated-server-rev' || msg.type === 'y-complete-sync-done') {
|
|
7118
|
+
applyYServerMessages([msg], this.db).then((_a) => __awaiter(this, [_a], void 0, function* ({ resyncNeeded, yServerRevision, receivedUntils }) {
|
|
7119
|
+
if (yServerRevision) {
|
|
7120
|
+
yield this.db.$syncState.update('syncState', { yServerRevision: yServerRevision });
|
|
7121
|
+
}
|
|
7122
|
+
if (msg.type === 'u-s' && receivedUntils) {
|
|
7123
|
+
const utbl = getUpdatesTable(this.db, msg.table, msg.prop);
|
|
7124
|
+
if (utbl) {
|
|
7125
|
+
const receivedUntil = receivedUntils[utbl.name];
|
|
7126
|
+
if (receivedUntil) {
|
|
7127
|
+
yield utbl.update(DEXIE_CLOUD_SYNCER_ID, { receivedUntil });
|
|
7128
|
+
}
|
|
7129
|
+
}
|
|
7130
|
+
}
|
|
7131
|
+
if (resyncNeeded) {
|
|
7132
|
+
yield this.db.cloud.sync({ purpose: 'pull', wait: true });
|
|
7133
|
+
}
|
|
7134
|
+
}));
|
|
7087
7135
|
}
|
|
7088
|
-
else
|
|
7136
|
+
else {
|
|
7089
7137
|
// Forward the request to our subscriber, wich is in messageFromServerQueue.ts (via connectWebSocket's subscribe() at the end!)
|
|
7090
7138
|
this.subscriber.next(msg);
|
|
7091
7139
|
}
|
|
@@ -7218,7 +7266,7 @@
|
|
|
7218
7266
|
// If no new entries, server won't bother the client. If new entries, server sends only those
|
|
7219
7267
|
// and the baseRev of the last from same client-ID.
|
|
7220
7268
|
if (userLogin) {
|
|
7221
|
-
return new WSObservable(db, db.cloud.persistedSyncState.value.serverRevision, realmSetHash, db.cloud.persistedSyncState.value.clientIdentity, messageProducer, db.cloud.webSocketStatus, userLogin);
|
|
7269
|
+
return new WSObservable(db, db.cloud.persistedSyncState.value.serverRevision, db.cloud.persistedSyncState.value.yServerRevision, realmSetHash, db.cloud.persistedSyncState.value.clientIdentity, messageProducer, db.cloud.webSocketStatus, userLogin);
|
|
7222
7270
|
}
|
|
7223
7271
|
else {
|
|
7224
7272
|
return rxjs.from([]);
|
|
@@ -7348,61 +7396,95 @@
|
|
|
7348
7396
|
}
|
|
7349
7397
|
|
|
7350
7398
|
const SECONDS = 1000;
|
|
7351
|
-
const MINUTES = 60 * SECONDS;
|
|
7352
7399
|
|
|
7353
7400
|
function LocalSyncWorker(db, cloudOptions, cloudSchema) {
|
|
7354
7401
|
let localSyncEventSubscription = null;
|
|
7355
|
-
//let syncHandler: ((event: Event) => void) | null = null;
|
|
7356
|
-
//let periodicSyncHandler: ((event: Event) => void) | null = null;
|
|
7357
7402
|
let cancelToken = { cancelled: false };
|
|
7358
|
-
let
|
|
7359
|
-
let
|
|
7360
|
-
function syncAndRetry(
|
|
7403
|
+
let nextRetryTime = 0;
|
|
7404
|
+
let syncStartTime = 0;
|
|
7405
|
+
function syncAndRetry(retryNum = 1) {
|
|
7361
7406
|
// Use setTimeout() to get onto a clean stack and
|
|
7362
7407
|
// break free from possible active transaction:
|
|
7363
7408
|
setTimeout(() => {
|
|
7364
|
-
|
|
7365
|
-
|
|
7366
|
-
const combPurpose = retryPurpose === 'pull' ? 'pull' : purpose;
|
|
7367
|
-
retryHandle = null;
|
|
7368
|
-
retryPurpose = null;
|
|
7409
|
+
const purpose = pullSignalled ? 'pull' : 'push';
|
|
7410
|
+
syncStartTime = Date.now();
|
|
7369
7411
|
syncIfPossible(db, cloudOptions, cloudSchema, {
|
|
7370
7412
|
cancelToken,
|
|
7371
7413
|
retryImmediatelyOnFetchError: true, // workaround for "net::ERR_NETWORK_CHANGED" in chrome.
|
|
7372
|
-
purpose
|
|
7373
|
-
}).
|
|
7374
|
-
|
|
7414
|
+
purpose,
|
|
7415
|
+
}).then(() => {
|
|
7416
|
+
if (cancelToken.cancelled) {
|
|
7417
|
+
stop();
|
|
7418
|
+
}
|
|
7419
|
+
else {
|
|
7420
|
+
if (pullSignalled || pushSignalled) {
|
|
7421
|
+
// If we have signalled for more sync, do it now.
|
|
7422
|
+
pullSignalled = false;
|
|
7423
|
+
pushSignalled = false;
|
|
7424
|
+
return syncAndRetry();
|
|
7425
|
+
}
|
|
7426
|
+
}
|
|
7427
|
+
ongoingSync = false;
|
|
7428
|
+
nextRetryTime = 0;
|
|
7429
|
+
syncStartTime = 0;
|
|
7430
|
+
}).catch((error) => {
|
|
7431
|
+
console.error('error in syncIfPossible()', error);
|
|
7375
7432
|
if (cancelToken.cancelled) {
|
|
7376
7433
|
stop();
|
|
7434
|
+
ongoingSync = false;
|
|
7435
|
+
nextRetryTime = 0;
|
|
7436
|
+
syncStartTime = 0;
|
|
7377
7437
|
}
|
|
7378
|
-
else if (retryNum <
|
|
7379
|
-
// Mimic service worker sync event: retry
|
|
7380
|
-
// * first retry after
|
|
7381
|
-
// * second retry
|
|
7382
|
-
|
|
7383
|
-
|
|
7384
|
-
|
|
7385
|
-
|
|
7386
|
-
|
|
7387
|
-
|
|
7388
|
-
|
|
7438
|
+
else if (retryNum < 5) {
|
|
7439
|
+
// Mimic service worker sync event but a bit more eager: retry 4 times
|
|
7440
|
+
// * first retry after 20 seconds
|
|
7441
|
+
// * second retry 40 seconds later
|
|
7442
|
+
// * third retry 5 minutes later
|
|
7443
|
+
// * last retry 15 minutes later
|
|
7444
|
+
const retryIn = [0, 20, 40, 300, 900][retryNum] * SECONDS;
|
|
7445
|
+
nextRetryTime = Date.now() + retryIn;
|
|
7446
|
+
syncStartTime = 0;
|
|
7447
|
+
setTimeout(() => syncAndRetry(retryNum + 1), retryIn);
|
|
7448
|
+
}
|
|
7449
|
+
else {
|
|
7450
|
+
ongoingSync = false;
|
|
7451
|
+
nextRetryTime = 0;
|
|
7452
|
+
syncStartTime = 0;
|
|
7389
7453
|
}
|
|
7390
7454
|
});
|
|
7391
7455
|
}, 0);
|
|
7392
7456
|
}
|
|
7457
|
+
let pullSignalled = false;
|
|
7458
|
+
let pushSignalled = false;
|
|
7459
|
+
let ongoingSync = false;
|
|
7460
|
+
const consumer = (purpose) => {
|
|
7461
|
+
if (cancelToken.cancelled)
|
|
7462
|
+
return;
|
|
7463
|
+
if (purpose === 'pull') {
|
|
7464
|
+
pullSignalled = true;
|
|
7465
|
+
}
|
|
7466
|
+
if (purpose === 'push') {
|
|
7467
|
+
pushSignalled = true;
|
|
7468
|
+
}
|
|
7469
|
+
if (ongoingSync) {
|
|
7470
|
+
if (nextRetryTime) {
|
|
7471
|
+
console.debug(`Sync is paused until ${new Date(nextRetryTime).toISOString()} due to error in last sync attempt`);
|
|
7472
|
+
}
|
|
7473
|
+
else if (syncStartTime > 0 && Date.now() - syncStartTime > 20 * SECONDS) {
|
|
7474
|
+
console.debug(`An existing sync operation is taking more than 20 seconds. Will resync when done.`);
|
|
7475
|
+
}
|
|
7476
|
+
return;
|
|
7477
|
+
}
|
|
7478
|
+
ongoingSync = true;
|
|
7479
|
+
syncAndRetry();
|
|
7480
|
+
};
|
|
7393
7481
|
const start = () => {
|
|
7394
7482
|
// Sync eagerly whenever a change has happened (+ initially when there's no syncState yet)
|
|
7395
7483
|
// This initial subscribe will also trigger an sync also now.
|
|
7396
7484
|
console.debug('Starting LocalSyncWorker', db.localSyncEvent['id']);
|
|
7397
7485
|
localSyncEventSubscription = db.localSyncEvent.subscribe(({ purpose }) => {
|
|
7398
|
-
|
|
7399
|
-
syncAndRetry(purpose || 'pull');
|
|
7400
|
-
}
|
|
7401
|
-
catch (err) {
|
|
7402
|
-
console.error('What-the....', err);
|
|
7403
|
-
}
|
|
7486
|
+
consumer(purpose || 'pull');
|
|
7404
7487
|
});
|
|
7405
|
-
//setTimeout(()=>db.localSyncEvent.next({}), 5000);
|
|
7406
7488
|
};
|
|
7407
7489
|
const stop = () => {
|
|
7408
7490
|
console.debug('Stopping LocalSyncWorker');
|
|
@@ -8194,7 +8276,7 @@
|
|
|
8194
8276
|
const syncComplete = new rxjs.Subject();
|
|
8195
8277
|
dexie.cloud = {
|
|
8196
8278
|
// @ts-ignore
|
|
8197
|
-
version: "4.1.0-beta.
|
|
8279
|
+
version: "4.1.0-beta.46",
|
|
8198
8280
|
options: Object.assign({}, DEFAULT_OPTIONS),
|
|
8199
8281
|
schema: null,
|
|
8200
8282
|
get currentUserId() {
|
|
@@ -8512,7 +8594,7 @@
|
|
|
8512
8594
|
}
|
|
8513
8595
|
}
|
|
8514
8596
|
// @ts-ignore
|
|
8515
|
-
dexieCloud.version = "4.1.0-beta.
|
|
8597
|
+
dexieCloud.version = "4.1.0-beta.46";
|
|
8516
8598
|
Dexie.Cloud = dexieCloud;
|
|
8517
8599
|
|
|
8518
8600
|
// In case the SW lives for a while, let it reuse already opened connections:
|