dexie-cloud-addon 4.0.0-beta.16 → 4.0.0-beta.17
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/dexie-cloud-addon.js +96 -104
- 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 +95 -103
- 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/module-es5/dexie-cloud-addon.js +135 -150
- package/dist/module-es5/dexie-cloud-addon.js.map +1 -1
- package/dist/module-es5/dexie-cloud-addon.min.js +1 -1
- package/dist/module-es5/dexie-cloud-addon.min.js.map +1 -1
- package/dist/umd/dexie-cloud-addon.js +135 -150
- 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 +94 -102
- 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-modern/dexie-cloud-addon.js +92 -100
- package/dist/umd-modern/dexie-cloud-addon.js.map +1 -1
- package/package.json +1 -1
- package/dist/types/DexieCloudEntity.d.ts +0 -8
- package/dist/types/WebSocketStatus.d.ts +0 -1
- package/dist/types/createMyMembersObservable.d.ts +0 -14
- package/dist/types/currentUserObservable.d.ts +0 -3
- package/dist/types/helpers/BroadcastedLocalEvent.d.ts +0 -8
- package/dist/types/helpers/visibleState.d.ts +0 -1
- package/dist/types/permissionsLookup.d.ts +0 -9
- package/dist/types/permissionsLookupObservable.d.ts +0 -14
- package/dist/types/sync/globalizePrivateIds.d.ts +0 -4
- package/dist/types/sync/syncServerToClientOnly.d.ts +0 -3
- package/dist/types/types/CloudConnectionStatus.d.ts +0 -0
- package/dist/types/types/ConnectionStatus.d.ts +0 -0
- package/dist/types/types/LoginState.d.ts +0 -41
- package/dist/types/types/SyncConnectionStatus.d.ts +0 -1
- package/dist/types/types/SyncFlowStatus.d.ts +0 -6
- package/dist/types/types/SyncStatus.d.ts +0 -6
|
@@ -4667,107 +4667,10 @@
|
|
|
4667
4667
|
};
|
|
4668
4668
|
}
|
|
4669
4669
|
|
|
4670
|
-
const SECONDS = 1000;
|
|
4671
|
-
const MINUTES = 60 * SECONDS;
|
|
4672
|
-
|
|
4673
|
-
const myId = randomString(16);
|
|
4674
|
-
|
|
4675
|
-
const GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
|
|
4676
|
-
const GUARDED_JOB_TIMEOUT = 1 * MINUTES;
|
|
4677
|
-
async function performGuardedJob(db, jobName, jobsTableName, job, { awaitRemoteJob } = {}) {
|
|
4678
|
-
// Start working.
|
|
4679
|
-
//
|
|
4680
|
-
// Check if someone else is working on this already.
|
|
4681
|
-
//
|
|
4682
|
-
const jobsTable = db.table(jobsTableName);
|
|
4683
|
-
async function aquireLock() {
|
|
4684
|
-
const gotTheLock = await db.transaction('rw!', jobsTableName, async () => {
|
|
4685
|
-
const currentWork = await jobsTable.get(jobName);
|
|
4686
|
-
if (!currentWork) {
|
|
4687
|
-
// No one else is working. Let's record that we are.
|
|
4688
|
-
await jobsTable.add({
|
|
4689
|
-
nodeId: myId,
|
|
4690
|
-
started: new Date(),
|
|
4691
|
-
heartbeat: new Date()
|
|
4692
|
-
}, jobName);
|
|
4693
|
-
return true;
|
|
4694
|
-
}
|
|
4695
|
-
else if (currentWork.heartbeat.getTime() <
|
|
4696
|
-
Date.now() - GUARDED_JOB_TIMEOUT) {
|
|
4697
|
-
console.warn(`Latest ${jobName} worker seem to have died.\n`, `The dead job started:`, currentWork.started, `\n`, `Last heart beat was:`, currentWork.heartbeat, '\n', `We're now taking over!`);
|
|
4698
|
-
// Now, take over!
|
|
4699
|
-
await jobsTable.put({
|
|
4700
|
-
nodeId: myId,
|
|
4701
|
-
started: new Date(),
|
|
4702
|
-
heartbeat: new Date()
|
|
4703
|
-
}, jobName);
|
|
4704
|
-
return true;
|
|
4705
|
-
}
|
|
4706
|
-
return false;
|
|
4707
|
-
});
|
|
4708
|
-
if (gotTheLock)
|
|
4709
|
-
return true;
|
|
4710
|
-
// Someone else took the job.
|
|
4711
|
-
if (awaitRemoteJob) {
|
|
4712
|
-
try {
|
|
4713
|
-
const jobDoneObservable = rxjs.from(Dexie.liveQuery(() => jobsTable.get(jobName))).pipe(timeout(GUARDED_JOB_TIMEOUT), filter((job) => !job)); // Wait til job is not there anymore.
|
|
4714
|
-
await jobDoneObservable.toPromise();
|
|
4715
|
-
return false;
|
|
4716
|
-
}
|
|
4717
|
-
catch (err) {
|
|
4718
|
-
if (err.name !== 'TimeoutError') {
|
|
4719
|
-
throw err;
|
|
4720
|
-
}
|
|
4721
|
-
// Timeout stopped us! Try aquire the lock now.
|
|
4722
|
-
// It will likely succeed this time unless
|
|
4723
|
-
// another client took it.
|
|
4724
|
-
return await aquireLock();
|
|
4725
|
-
}
|
|
4726
|
-
}
|
|
4727
|
-
return false;
|
|
4728
|
-
}
|
|
4729
|
-
if (await aquireLock()) {
|
|
4730
|
-
// We own the lock entry and can do our job undisturbed.
|
|
4731
|
-
// We're not within a transaction, but these type of locks
|
|
4732
|
-
// spans over transactions.
|
|
4733
|
-
// Start our heart beat during the job.
|
|
4734
|
-
// Use setInterval to make sure we are updating heartbeat even during long-lived fetch calls.
|
|
4735
|
-
const heartbeat = setInterval(() => {
|
|
4736
|
-
jobsTable.update(jobName, (job) => {
|
|
4737
|
-
if (job.nodeId === myId) {
|
|
4738
|
-
job.heartbeat = new Date();
|
|
4739
|
-
}
|
|
4740
|
-
});
|
|
4741
|
-
}, GUARDED_JOB_HEARTBEAT);
|
|
4742
|
-
try {
|
|
4743
|
-
return await job();
|
|
4744
|
-
}
|
|
4745
|
-
finally {
|
|
4746
|
-
// Stop heartbeat
|
|
4747
|
-
clearInterval(heartbeat);
|
|
4748
|
-
// Remove the persisted job state:
|
|
4749
|
-
await db.transaction('rw!', jobsTableName, async () => {
|
|
4750
|
-
const currentWork = await jobsTable.get(jobName);
|
|
4751
|
-
if (currentWork && currentWork.nodeId === myId) {
|
|
4752
|
-
jobsTable.delete(jobName);
|
|
4753
|
-
}
|
|
4754
|
-
});
|
|
4755
|
-
}
|
|
4756
|
-
}
|
|
4757
|
-
}
|
|
4758
|
-
|
|
4759
4670
|
async function performInitialSync(db, cloudOptions, cloudSchema) {
|
|
4760
|
-
console.debug(
|
|
4761
|
-
await
|
|
4762
|
-
|
|
4763
|
-
// Do check again (now within a transaction) that we really do not have a sync state:
|
|
4764
|
-
const syncState = await db.getPersistedSyncState();
|
|
4765
|
-
if (!syncState?.initiallySynced) {
|
|
4766
|
-
await sync(db, cloudOptions, cloudSchema, { isInitialSync: true });
|
|
4767
|
-
}
|
|
4768
|
-
}, { awaitRemoteJob: true } // Don't return until the job is done!
|
|
4769
|
-
);
|
|
4770
|
-
console.debug("Done initial sync");
|
|
4671
|
+
console.debug('Performing initial sync');
|
|
4672
|
+
await sync(db, cloudOptions, cloudSchema, { isInitialSync: true });
|
|
4673
|
+
console.debug('Done initial sync');
|
|
4771
4674
|
}
|
|
4772
4675
|
|
|
4773
4676
|
const USER_INACTIVITY_TIMEOUT = 180000; // 3 minutes
|
|
@@ -5121,6 +5024,95 @@
|
|
|
5121
5024
|
: false;
|
|
5122
5025
|
}
|
|
5123
5026
|
|
|
5027
|
+
const SECONDS = 1000;
|
|
5028
|
+
const MINUTES = 60 * SECONDS;
|
|
5029
|
+
|
|
5030
|
+
const myId = randomString(16);
|
|
5031
|
+
|
|
5032
|
+
const GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
|
|
5033
|
+
const GUARDED_JOB_TIMEOUT = 1 * MINUTES;
|
|
5034
|
+
async function performGuardedJob(db, jobName, jobsTableName, job, { awaitRemoteJob } = {}) {
|
|
5035
|
+
// Start working.
|
|
5036
|
+
//
|
|
5037
|
+
// Check if someone else is working on this already.
|
|
5038
|
+
//
|
|
5039
|
+
const jobsTable = db.table(jobsTableName);
|
|
5040
|
+
async function aquireLock() {
|
|
5041
|
+
const gotTheLock = await db.transaction('rw!', jobsTableName, async () => {
|
|
5042
|
+
const currentWork = await jobsTable.get(jobName);
|
|
5043
|
+
if (!currentWork) {
|
|
5044
|
+
// No one else is working. Let's record that we are.
|
|
5045
|
+
await jobsTable.add({
|
|
5046
|
+
nodeId: myId,
|
|
5047
|
+
started: new Date(),
|
|
5048
|
+
heartbeat: new Date()
|
|
5049
|
+
}, jobName);
|
|
5050
|
+
return true;
|
|
5051
|
+
}
|
|
5052
|
+
else if (currentWork.heartbeat.getTime() <
|
|
5053
|
+
Date.now() - GUARDED_JOB_TIMEOUT) {
|
|
5054
|
+
console.warn(`Latest ${jobName} worker seem to have died.\n`, `The dead job started:`, currentWork.started, `\n`, `Last heart beat was:`, currentWork.heartbeat, '\n', `We're now taking over!`);
|
|
5055
|
+
// Now, take over!
|
|
5056
|
+
await jobsTable.put({
|
|
5057
|
+
nodeId: myId,
|
|
5058
|
+
started: new Date(),
|
|
5059
|
+
heartbeat: new Date()
|
|
5060
|
+
}, jobName);
|
|
5061
|
+
return true;
|
|
5062
|
+
}
|
|
5063
|
+
return false;
|
|
5064
|
+
});
|
|
5065
|
+
if (gotTheLock)
|
|
5066
|
+
return true;
|
|
5067
|
+
// Someone else took the job.
|
|
5068
|
+
if (awaitRemoteJob) {
|
|
5069
|
+
try {
|
|
5070
|
+
const jobDoneObservable = rxjs.from(Dexie.liveQuery(() => jobsTable.get(jobName))).pipe(timeout(GUARDED_JOB_TIMEOUT), filter((job) => !job)); // Wait til job is not there anymore.
|
|
5071
|
+
await jobDoneObservable.toPromise();
|
|
5072
|
+
return false;
|
|
5073
|
+
}
|
|
5074
|
+
catch (err) {
|
|
5075
|
+
if (err.name !== 'TimeoutError') {
|
|
5076
|
+
throw err;
|
|
5077
|
+
}
|
|
5078
|
+
// Timeout stopped us! Try aquire the lock now.
|
|
5079
|
+
// It will likely succeed this time unless
|
|
5080
|
+
// another client took it.
|
|
5081
|
+
return await aquireLock();
|
|
5082
|
+
}
|
|
5083
|
+
}
|
|
5084
|
+
return false;
|
|
5085
|
+
}
|
|
5086
|
+
if (await aquireLock()) {
|
|
5087
|
+
// We own the lock entry and can do our job undisturbed.
|
|
5088
|
+
// We're not within a transaction, but these type of locks
|
|
5089
|
+
// spans over transactions.
|
|
5090
|
+
// Start our heart beat during the job.
|
|
5091
|
+
// Use setInterval to make sure we are updating heartbeat even during long-lived fetch calls.
|
|
5092
|
+
const heartbeat = setInterval(() => {
|
|
5093
|
+
jobsTable.update(jobName, (job) => {
|
|
5094
|
+
if (job.nodeId === myId) {
|
|
5095
|
+
job.heartbeat = new Date();
|
|
5096
|
+
}
|
|
5097
|
+
});
|
|
5098
|
+
}, GUARDED_JOB_HEARTBEAT);
|
|
5099
|
+
try {
|
|
5100
|
+
return await job();
|
|
5101
|
+
}
|
|
5102
|
+
finally {
|
|
5103
|
+
// Stop heartbeat
|
|
5104
|
+
clearInterval(heartbeat);
|
|
5105
|
+
// Remove the persisted job state:
|
|
5106
|
+
await db.transaction('rw!', jobsTableName, async () => {
|
|
5107
|
+
const currentWork = await jobsTable.get(jobName);
|
|
5108
|
+
if (currentWork && currentWork.nodeId === myId) {
|
|
5109
|
+
await jobsTable.delete(jobName);
|
|
5110
|
+
}
|
|
5111
|
+
});
|
|
5112
|
+
}
|
|
5113
|
+
}
|
|
5114
|
+
}
|
|
5115
|
+
|
|
5124
5116
|
const ongoingSyncs = new WeakMap();
|
|
5125
5117
|
function syncIfPossible(db, cloudOptions, cloudSchema, options) {
|
|
5126
5118
|
const ongoing = ongoingSyncs.get(db);
|