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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Dexie, { cmp, liveQuery } from 'dexie';
|
|
2
|
-
import { Observable as Observable$1, BehaviorSubject, Subject,
|
|
2
|
+
import { Observable as Observable$1, BehaviorSubject, Subject, fromEvent, of, merge, Subscription as Subscription$1, from as from$1, throwError, combineLatest, map as map$1, share, timer as timer$1, switchMap as switchMap$1 } from 'rxjs';
|
|
3
3
|
|
|
4
4
|
const UNAUTHORIZED_USER = {
|
|
5
5
|
userId: "unauthorized",
|
|
@@ -4642,107 +4642,10 @@ function overrideParseStoresSpec(origFunc, dexie) {
|
|
|
4642
4642
|
};
|
|
4643
4643
|
}
|
|
4644
4644
|
|
|
4645
|
-
const SECONDS = 1000;
|
|
4646
|
-
const MINUTES = 60 * SECONDS;
|
|
4647
|
-
|
|
4648
|
-
const myId = randomString$1(16);
|
|
4649
|
-
|
|
4650
|
-
const GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
|
|
4651
|
-
const GUARDED_JOB_TIMEOUT = 1 * MINUTES;
|
|
4652
|
-
async function performGuardedJob(db, jobName, jobsTableName, job, { awaitRemoteJob } = {}) {
|
|
4653
|
-
// Start working.
|
|
4654
|
-
//
|
|
4655
|
-
// Check if someone else is working on this already.
|
|
4656
|
-
//
|
|
4657
|
-
const jobsTable = db.table(jobsTableName);
|
|
4658
|
-
async function aquireLock() {
|
|
4659
|
-
const gotTheLock = await db.transaction('rw!', jobsTableName, async () => {
|
|
4660
|
-
const currentWork = await jobsTable.get(jobName);
|
|
4661
|
-
if (!currentWork) {
|
|
4662
|
-
// No one else is working. Let's record that we are.
|
|
4663
|
-
await jobsTable.add({
|
|
4664
|
-
nodeId: myId,
|
|
4665
|
-
started: new Date(),
|
|
4666
|
-
heartbeat: new Date()
|
|
4667
|
-
}, jobName);
|
|
4668
|
-
return true;
|
|
4669
|
-
}
|
|
4670
|
-
else if (currentWork.heartbeat.getTime() <
|
|
4671
|
-
Date.now() - GUARDED_JOB_TIMEOUT) {
|
|
4672
|
-
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!`);
|
|
4673
|
-
// Now, take over!
|
|
4674
|
-
await jobsTable.put({
|
|
4675
|
-
nodeId: myId,
|
|
4676
|
-
started: new Date(),
|
|
4677
|
-
heartbeat: new Date()
|
|
4678
|
-
}, jobName);
|
|
4679
|
-
return true;
|
|
4680
|
-
}
|
|
4681
|
-
return false;
|
|
4682
|
-
});
|
|
4683
|
-
if (gotTheLock)
|
|
4684
|
-
return true;
|
|
4685
|
-
// Someone else took the job.
|
|
4686
|
-
if (awaitRemoteJob) {
|
|
4687
|
-
try {
|
|
4688
|
-
const jobDoneObservable = from$1(liveQuery(() => jobsTable.get(jobName))).pipe(timeout(GUARDED_JOB_TIMEOUT), filter((job) => !job)); // Wait til job is not there anymore.
|
|
4689
|
-
await jobDoneObservable.toPromise();
|
|
4690
|
-
return false;
|
|
4691
|
-
}
|
|
4692
|
-
catch (err) {
|
|
4693
|
-
if (err.name !== 'TimeoutError') {
|
|
4694
|
-
throw err;
|
|
4695
|
-
}
|
|
4696
|
-
// Timeout stopped us! Try aquire the lock now.
|
|
4697
|
-
// It will likely succeed this time unless
|
|
4698
|
-
// another client took it.
|
|
4699
|
-
return await aquireLock();
|
|
4700
|
-
}
|
|
4701
|
-
}
|
|
4702
|
-
return false;
|
|
4703
|
-
}
|
|
4704
|
-
if (await aquireLock()) {
|
|
4705
|
-
// We own the lock entry and can do our job undisturbed.
|
|
4706
|
-
// We're not within a transaction, but these type of locks
|
|
4707
|
-
// spans over transactions.
|
|
4708
|
-
// Start our heart beat during the job.
|
|
4709
|
-
// Use setInterval to make sure we are updating heartbeat even during long-lived fetch calls.
|
|
4710
|
-
const heartbeat = setInterval(() => {
|
|
4711
|
-
jobsTable.update(jobName, (job) => {
|
|
4712
|
-
if (job.nodeId === myId) {
|
|
4713
|
-
job.heartbeat = new Date();
|
|
4714
|
-
}
|
|
4715
|
-
});
|
|
4716
|
-
}, GUARDED_JOB_HEARTBEAT);
|
|
4717
|
-
try {
|
|
4718
|
-
return await job();
|
|
4719
|
-
}
|
|
4720
|
-
finally {
|
|
4721
|
-
// Stop heartbeat
|
|
4722
|
-
clearInterval(heartbeat);
|
|
4723
|
-
// Remove the persisted job state:
|
|
4724
|
-
await db.transaction('rw!', jobsTableName, async () => {
|
|
4725
|
-
const currentWork = await jobsTable.get(jobName);
|
|
4726
|
-
if (currentWork && currentWork.nodeId === myId) {
|
|
4727
|
-
jobsTable.delete(jobName);
|
|
4728
|
-
}
|
|
4729
|
-
});
|
|
4730
|
-
}
|
|
4731
|
-
}
|
|
4732
|
-
}
|
|
4733
|
-
|
|
4734
4645
|
async function performInitialSync(db, cloudOptions, cloudSchema) {
|
|
4735
|
-
console.debug(
|
|
4736
|
-
await
|
|
4737
|
-
|
|
4738
|
-
// Do check again (now within a transaction) that we really do not have a sync state:
|
|
4739
|
-
const syncState = await db.getPersistedSyncState();
|
|
4740
|
-
if (!syncState?.initiallySynced) {
|
|
4741
|
-
await sync(db, cloudOptions, cloudSchema, { isInitialSync: true });
|
|
4742
|
-
}
|
|
4743
|
-
}, { awaitRemoteJob: true } // Don't return until the job is done!
|
|
4744
|
-
);
|
|
4745
|
-
console.debug("Done initial sync");
|
|
4646
|
+
console.debug('Performing initial sync');
|
|
4647
|
+
await sync(db, cloudOptions, cloudSchema, { isInitialSync: true });
|
|
4648
|
+
console.debug('Done initial sync');
|
|
4746
4649
|
}
|
|
4747
4650
|
|
|
4748
4651
|
const USER_INACTIVITY_TIMEOUT = 180000; // 3 minutes
|
|
@@ -5096,6 +4999,95 @@ async function isSyncNeeded(db) {
|
|
|
5096
4999
|
: false;
|
|
5097
5000
|
}
|
|
5098
5001
|
|
|
5002
|
+
const SECONDS = 1000;
|
|
5003
|
+
const MINUTES = 60 * SECONDS;
|
|
5004
|
+
|
|
5005
|
+
const myId = randomString$1(16);
|
|
5006
|
+
|
|
5007
|
+
const GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
|
|
5008
|
+
const GUARDED_JOB_TIMEOUT = 1 * MINUTES;
|
|
5009
|
+
async function performGuardedJob(db, jobName, jobsTableName, job, { awaitRemoteJob } = {}) {
|
|
5010
|
+
// Start working.
|
|
5011
|
+
//
|
|
5012
|
+
// Check if someone else is working on this already.
|
|
5013
|
+
//
|
|
5014
|
+
const jobsTable = db.table(jobsTableName);
|
|
5015
|
+
async function aquireLock() {
|
|
5016
|
+
const gotTheLock = await db.transaction('rw!', jobsTableName, async () => {
|
|
5017
|
+
const currentWork = await jobsTable.get(jobName);
|
|
5018
|
+
if (!currentWork) {
|
|
5019
|
+
// No one else is working. Let's record that we are.
|
|
5020
|
+
await jobsTable.add({
|
|
5021
|
+
nodeId: myId,
|
|
5022
|
+
started: new Date(),
|
|
5023
|
+
heartbeat: new Date()
|
|
5024
|
+
}, jobName);
|
|
5025
|
+
return true;
|
|
5026
|
+
}
|
|
5027
|
+
else if (currentWork.heartbeat.getTime() <
|
|
5028
|
+
Date.now() - GUARDED_JOB_TIMEOUT) {
|
|
5029
|
+
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!`);
|
|
5030
|
+
// Now, take over!
|
|
5031
|
+
await jobsTable.put({
|
|
5032
|
+
nodeId: myId,
|
|
5033
|
+
started: new Date(),
|
|
5034
|
+
heartbeat: new Date()
|
|
5035
|
+
}, jobName);
|
|
5036
|
+
return true;
|
|
5037
|
+
}
|
|
5038
|
+
return false;
|
|
5039
|
+
});
|
|
5040
|
+
if (gotTheLock)
|
|
5041
|
+
return true;
|
|
5042
|
+
// Someone else took the job.
|
|
5043
|
+
if (awaitRemoteJob) {
|
|
5044
|
+
try {
|
|
5045
|
+
const jobDoneObservable = from$1(liveQuery(() => jobsTable.get(jobName))).pipe(timeout(GUARDED_JOB_TIMEOUT), filter((job) => !job)); // Wait til job is not there anymore.
|
|
5046
|
+
await jobDoneObservable.toPromise();
|
|
5047
|
+
return false;
|
|
5048
|
+
}
|
|
5049
|
+
catch (err) {
|
|
5050
|
+
if (err.name !== 'TimeoutError') {
|
|
5051
|
+
throw err;
|
|
5052
|
+
}
|
|
5053
|
+
// Timeout stopped us! Try aquire the lock now.
|
|
5054
|
+
// It will likely succeed this time unless
|
|
5055
|
+
// another client took it.
|
|
5056
|
+
return await aquireLock();
|
|
5057
|
+
}
|
|
5058
|
+
}
|
|
5059
|
+
return false;
|
|
5060
|
+
}
|
|
5061
|
+
if (await aquireLock()) {
|
|
5062
|
+
// We own the lock entry and can do our job undisturbed.
|
|
5063
|
+
// We're not within a transaction, but these type of locks
|
|
5064
|
+
// spans over transactions.
|
|
5065
|
+
// Start our heart beat during the job.
|
|
5066
|
+
// Use setInterval to make sure we are updating heartbeat even during long-lived fetch calls.
|
|
5067
|
+
const heartbeat = setInterval(() => {
|
|
5068
|
+
jobsTable.update(jobName, (job) => {
|
|
5069
|
+
if (job.nodeId === myId) {
|
|
5070
|
+
job.heartbeat = new Date();
|
|
5071
|
+
}
|
|
5072
|
+
});
|
|
5073
|
+
}, GUARDED_JOB_HEARTBEAT);
|
|
5074
|
+
try {
|
|
5075
|
+
return await job();
|
|
5076
|
+
}
|
|
5077
|
+
finally {
|
|
5078
|
+
// Stop heartbeat
|
|
5079
|
+
clearInterval(heartbeat);
|
|
5080
|
+
// Remove the persisted job state:
|
|
5081
|
+
await db.transaction('rw!', jobsTableName, async () => {
|
|
5082
|
+
const currentWork = await jobsTable.get(jobName);
|
|
5083
|
+
if (currentWork && currentWork.nodeId === myId) {
|
|
5084
|
+
await jobsTable.delete(jobName);
|
|
5085
|
+
}
|
|
5086
|
+
});
|
|
5087
|
+
}
|
|
5088
|
+
}
|
|
5089
|
+
}
|
|
5090
|
+
|
|
5099
5091
|
const ongoingSyncs = new WeakMap();
|
|
5100
5092
|
function syncIfPossible(db, cloudOptions, cloudSchema, options) {
|
|
5101
5093
|
const ongoing = ongoingSyncs.get(db);
|
|
@@ -5787,7 +5779,7 @@ function dexieCloud(dexie) {
|
|
|
5787
5779
|
currentUserEmitter.next(UNAUTHORIZED_USER);
|
|
5788
5780
|
});
|
|
5789
5781
|
dexie.cloud = {
|
|
5790
|
-
version: '4.0.0-beta.
|
|
5782
|
+
version: '4.0.0-beta.17',
|
|
5791
5783
|
options: { ...DEFAULT_OPTIONS },
|
|
5792
5784
|
schema: null,
|
|
5793
5785
|
serverState: null,
|
|
@@ -6029,7 +6021,7 @@ function dexieCloud(dexie) {
|
|
|
6029
6021
|
}
|
|
6030
6022
|
}
|
|
6031
6023
|
}
|
|
6032
|
-
dexieCloud.version = '4.0.0-beta.
|
|
6024
|
+
dexieCloud.version = '4.0.0-beta.17';
|
|
6033
6025
|
Dexie.Cloud = dexieCloud;
|
|
6034
6026
|
|
|
6035
6027
|
// In case the SW lives for a while, let it reuse already opened connections:
|