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.
Files changed (39) hide show
  1. package/dist/modern/dexie-cloud-addon.js +96 -104
  2. package/dist/modern/dexie-cloud-addon.js.map +1 -1
  3. package/dist/modern/dexie-cloud-addon.min.js +1 -1
  4. package/dist/modern/dexie-cloud-addon.min.js.map +1 -1
  5. package/dist/modern/service-worker.js +95 -103
  6. package/dist/modern/service-worker.js.map +1 -1
  7. package/dist/modern/service-worker.min.js +1 -1
  8. package/dist/modern/service-worker.min.js.map +1 -1
  9. package/dist/module-es5/dexie-cloud-addon.js +135 -150
  10. package/dist/module-es5/dexie-cloud-addon.js.map +1 -1
  11. package/dist/module-es5/dexie-cloud-addon.min.js +1 -1
  12. package/dist/module-es5/dexie-cloud-addon.min.js.map +1 -1
  13. package/dist/umd/dexie-cloud-addon.js +135 -150
  14. package/dist/umd/dexie-cloud-addon.js.map +1 -1
  15. package/dist/umd/dexie-cloud-addon.min.js +1 -1
  16. package/dist/umd/dexie-cloud-addon.min.js.map +1 -1
  17. package/dist/umd/service-worker.js +94 -102
  18. package/dist/umd/service-worker.js.map +1 -1
  19. package/dist/umd/service-worker.min.js +1 -1
  20. package/dist/umd/service-worker.min.js.map +1 -1
  21. package/dist/umd-modern/dexie-cloud-addon.js +92 -100
  22. package/dist/umd-modern/dexie-cloud-addon.js.map +1 -1
  23. package/package.json +1 -1
  24. package/dist/types/DexieCloudEntity.d.ts +0 -8
  25. package/dist/types/WebSocketStatus.d.ts +0 -1
  26. package/dist/types/createMyMembersObservable.d.ts +0 -14
  27. package/dist/types/currentUserObservable.d.ts +0 -3
  28. package/dist/types/helpers/BroadcastedLocalEvent.d.ts +0 -8
  29. package/dist/types/helpers/visibleState.d.ts +0 -1
  30. package/dist/types/permissionsLookup.d.ts +0 -9
  31. package/dist/types/permissionsLookupObservable.d.ts +0 -14
  32. package/dist/types/sync/globalizePrivateIds.d.ts +0 -4
  33. package/dist/types/sync/syncServerToClientOnly.d.ts +0 -3
  34. package/dist/types/types/CloudConnectionStatus.d.ts +0 -0
  35. package/dist/types/types/ConnectionStatus.d.ts +0 -0
  36. package/dist/types/types/LoginState.d.ts +0 -41
  37. package/dist/types/types/SyncConnectionStatus.d.ts +0 -1
  38. package/dist/types/types/SyncFlowStatus.d.ts +0 -6
  39. package/dist/types/types/SyncStatus.d.ts +0 -6
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * ==========================================================================
10
10
  *
11
- * Version 4.0.0-beta.16, Fri Apr 01 2022
11
+ * Version 4.0.0-beta.17, Fri Apr 08 2022
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -17,7 +17,7 @@
17
17
  */
18
18
 
19
19
  import Dexie, { cmp, liveQuery } from 'dexie';
20
- import { Observable as Observable$1, BehaviorSubject, Subject, from as from$1, fromEvent, of, merge, Subscription as Subscription$1, throwError, combineLatest, map as map$1, share, timer as timer$1, switchMap as switchMap$1 } from 'rxjs';
20
+ 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';
21
21
 
22
22
  //@ts-check
23
23
  const randomFillSync = crypto.getRandomValues;
@@ -4660,107 +4660,10 @@ function overrideParseStoresSpec(origFunc, dexie) {
4660
4660
  };
4661
4661
  }
4662
4662
 
4663
- const SECONDS = 1000;
4664
- const MINUTES = 60 * SECONDS;
4665
-
4666
- const myId = randomString(16);
4667
-
4668
- const GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
4669
- const GUARDED_JOB_TIMEOUT = 1 * MINUTES;
4670
- async function performGuardedJob(db, jobName, jobsTableName, job, { awaitRemoteJob } = {}) {
4671
- // Start working.
4672
- //
4673
- // Check if someone else is working on this already.
4674
- //
4675
- const jobsTable = db.table(jobsTableName);
4676
- async function aquireLock() {
4677
- const gotTheLock = await db.transaction('rw!', jobsTableName, async () => {
4678
- const currentWork = await jobsTable.get(jobName);
4679
- if (!currentWork) {
4680
- // No one else is working. Let's record that we are.
4681
- await jobsTable.add({
4682
- nodeId: myId,
4683
- started: new Date(),
4684
- heartbeat: new Date()
4685
- }, jobName);
4686
- return true;
4687
- }
4688
- else if (currentWork.heartbeat.getTime() <
4689
- Date.now() - GUARDED_JOB_TIMEOUT) {
4690
- 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!`);
4691
- // Now, take over!
4692
- await jobsTable.put({
4693
- nodeId: myId,
4694
- started: new Date(),
4695
- heartbeat: new Date()
4696
- }, jobName);
4697
- return true;
4698
- }
4699
- return false;
4700
- });
4701
- if (gotTheLock)
4702
- return true;
4703
- // Someone else took the job.
4704
- if (awaitRemoteJob) {
4705
- try {
4706
- const jobDoneObservable = from$1(liveQuery(() => jobsTable.get(jobName))).pipe(timeout(GUARDED_JOB_TIMEOUT), filter((job) => !job)); // Wait til job is not there anymore.
4707
- await jobDoneObservable.toPromise();
4708
- return false;
4709
- }
4710
- catch (err) {
4711
- if (err.name !== 'TimeoutError') {
4712
- throw err;
4713
- }
4714
- // Timeout stopped us! Try aquire the lock now.
4715
- // It will likely succeed this time unless
4716
- // another client took it.
4717
- return await aquireLock();
4718
- }
4719
- }
4720
- return false;
4721
- }
4722
- if (await aquireLock()) {
4723
- // We own the lock entry and can do our job undisturbed.
4724
- // We're not within a transaction, but these type of locks
4725
- // spans over transactions.
4726
- // Start our heart beat during the job.
4727
- // Use setInterval to make sure we are updating heartbeat even during long-lived fetch calls.
4728
- const heartbeat = setInterval(() => {
4729
- jobsTable.update(jobName, (job) => {
4730
- if (job.nodeId === myId) {
4731
- job.heartbeat = new Date();
4732
- }
4733
- });
4734
- }, GUARDED_JOB_HEARTBEAT);
4735
- try {
4736
- return await job();
4737
- }
4738
- finally {
4739
- // Stop heartbeat
4740
- clearInterval(heartbeat);
4741
- // Remove the persisted job state:
4742
- await db.transaction('rw!', jobsTableName, async () => {
4743
- const currentWork = await jobsTable.get(jobName);
4744
- if (currentWork && currentWork.nodeId === myId) {
4745
- jobsTable.delete(jobName);
4746
- }
4747
- });
4748
- }
4749
- }
4750
- }
4751
-
4752
4663
  async function performInitialSync(db, cloudOptions, cloudSchema) {
4753
- console.debug("Performing initial sync");
4754
- await performGuardedJob(db, 'initialSync', '$jobs', async () => {
4755
- // Even though caller has already checked it,
4756
- // Do check again (now within a transaction) that we really do not have a sync state:
4757
- const syncState = await db.getPersistedSyncState();
4758
- if (!syncState?.initiallySynced) {
4759
- await sync(db, cloudOptions, cloudSchema, { isInitialSync: true });
4760
- }
4761
- }, { awaitRemoteJob: true } // Don't return until the job is done!
4762
- );
4763
- console.debug("Done initial sync");
4664
+ console.debug('Performing initial sync');
4665
+ await sync(db, cloudOptions, cloudSchema, { isInitialSync: true });
4666
+ console.debug('Done initial sync');
4764
4667
  }
4765
4668
 
4766
4669
  const USER_INACTIVITY_TIMEOUT = 180000; // 3 minutes
@@ -5114,6 +5017,95 @@ async function isSyncNeeded(db) {
5114
5017
  : false;
5115
5018
  }
5116
5019
 
5020
+ const SECONDS = 1000;
5021
+ const MINUTES = 60 * SECONDS;
5022
+
5023
+ const myId = randomString(16);
5024
+
5025
+ const GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
5026
+ const GUARDED_JOB_TIMEOUT = 1 * MINUTES;
5027
+ async function performGuardedJob(db, jobName, jobsTableName, job, { awaitRemoteJob } = {}) {
5028
+ // Start working.
5029
+ //
5030
+ // Check if someone else is working on this already.
5031
+ //
5032
+ const jobsTable = db.table(jobsTableName);
5033
+ async function aquireLock() {
5034
+ const gotTheLock = await db.transaction('rw!', jobsTableName, async () => {
5035
+ const currentWork = await jobsTable.get(jobName);
5036
+ if (!currentWork) {
5037
+ // No one else is working. Let's record that we are.
5038
+ await jobsTable.add({
5039
+ nodeId: myId,
5040
+ started: new Date(),
5041
+ heartbeat: new Date()
5042
+ }, jobName);
5043
+ return true;
5044
+ }
5045
+ else if (currentWork.heartbeat.getTime() <
5046
+ Date.now() - GUARDED_JOB_TIMEOUT) {
5047
+ 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!`);
5048
+ // Now, take over!
5049
+ await jobsTable.put({
5050
+ nodeId: myId,
5051
+ started: new Date(),
5052
+ heartbeat: new Date()
5053
+ }, jobName);
5054
+ return true;
5055
+ }
5056
+ return false;
5057
+ });
5058
+ if (gotTheLock)
5059
+ return true;
5060
+ // Someone else took the job.
5061
+ if (awaitRemoteJob) {
5062
+ try {
5063
+ const jobDoneObservable = from$1(liveQuery(() => jobsTable.get(jobName))).pipe(timeout(GUARDED_JOB_TIMEOUT), filter((job) => !job)); // Wait til job is not there anymore.
5064
+ await jobDoneObservable.toPromise();
5065
+ return false;
5066
+ }
5067
+ catch (err) {
5068
+ if (err.name !== 'TimeoutError') {
5069
+ throw err;
5070
+ }
5071
+ // Timeout stopped us! Try aquire the lock now.
5072
+ // It will likely succeed this time unless
5073
+ // another client took it.
5074
+ return await aquireLock();
5075
+ }
5076
+ }
5077
+ return false;
5078
+ }
5079
+ if (await aquireLock()) {
5080
+ // We own the lock entry and can do our job undisturbed.
5081
+ // We're not within a transaction, but these type of locks
5082
+ // spans over transactions.
5083
+ // Start our heart beat during the job.
5084
+ // Use setInterval to make sure we are updating heartbeat even during long-lived fetch calls.
5085
+ const heartbeat = setInterval(() => {
5086
+ jobsTable.update(jobName, (job) => {
5087
+ if (job.nodeId === myId) {
5088
+ job.heartbeat = new Date();
5089
+ }
5090
+ });
5091
+ }, GUARDED_JOB_HEARTBEAT);
5092
+ try {
5093
+ return await job();
5094
+ }
5095
+ finally {
5096
+ // Stop heartbeat
5097
+ clearInterval(heartbeat);
5098
+ // Remove the persisted job state:
5099
+ await db.transaction('rw!', jobsTableName, async () => {
5100
+ const currentWork = await jobsTable.get(jobName);
5101
+ if (currentWork && currentWork.nodeId === myId) {
5102
+ await jobsTable.delete(jobName);
5103
+ }
5104
+ });
5105
+ }
5106
+ }
5107
+ }
5108
+
5117
5109
  const ongoingSyncs = new WeakMap();
5118
5110
  function syncIfPossible(db, cloudOptions, cloudSchema, options) {
5119
5111
  const ongoing = ongoingSyncs.get(db);
@@ -5812,7 +5804,7 @@ function dexieCloud(dexie) {
5812
5804
  currentUserEmitter.next(UNAUTHORIZED_USER);
5813
5805
  });
5814
5806
  dexie.cloud = {
5815
- version: '4.0.0-beta.16',
5807
+ version: '4.0.0-beta.17',
5816
5808
  options: { ...DEFAULT_OPTIONS },
5817
5809
  schema: null,
5818
5810
  serverState: null,
@@ -6054,7 +6046,7 @@ function dexieCloud(dexie) {
6054
6046
  }
6055
6047
  }
6056
6048
  }
6057
- dexieCloud.version = '4.0.0-beta.16';
6049
+ dexieCloud.version = '4.0.0-beta.17';
6058
6050
  Dexie.Cloud = dexieCloud;
6059
6051
 
6060
6052
  export { dexieCloud as default, dexieCloud, getTiedObjectId, getTiedRealmId };