dexie-cloud-addon 4.0.4 → 4.0.5

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.
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * ==========================================================================
10
10
  *
11
- * Version 4.0.4, Mon Apr 29 2024
11
+ * Version 4.0.5, Sat May 04 2024
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -17,7 +17,7 @@
17
17
  */
18
18
 
19
19
  import Dexie, { PropModification, cmp, liveQuery } from 'dexie';
20
- import { Observable as Observable$1, BehaviorSubject, Subject, firstValueFrom, from as from$1, filter as filter$1, fromEvent, of, merge, Subscription as Subscription$1, throwError, combineLatest, map as map$1, share, timer as timer$1 } from 'rxjs';
20
+ import { Observable as Observable$1, BehaviorSubject, firstValueFrom, Subject, from as from$1, filter as filter$1, fromEvent, of, merge, Subscription as Subscription$1, throwError, combineLatest, map as map$1, share, timer as timer$1 } from 'rxjs';
21
21
 
22
22
  /******************************************************************************
23
23
  Copyright (c) Microsoft Corporation.
@@ -3903,9 +3903,7 @@ function MessagesFromServerConsumer(db) {
3903
3903
  // If the sync worker or service worker is syncing, wait 'til thei're done.
3904
3904
  // It's no need to have two channels at the same time - even though it wouldnt
3905
3905
  // be a problem - this is an optimization.
3906
- yield db.cloud.syncState
3907
- .pipe(filter(({ phase }) => phase === 'in-sync' || phase === 'error'), take(1))
3908
- .toPromise();
3906
+ yield firstValueFrom(db.cloud.syncState.pipe(filter(({ phase }) => phase === 'in-sync' || phase === 'error')));
3909
3907
  console.debug('processing msg', msg);
3910
3908
  const persistedSyncState = db.cloud.persistedSyncState.value;
3911
3909
  //syncState.
@@ -5414,7 +5412,7 @@ function waitAndReconnectWhenUserDoesSomething(error) {
5414
5412
  yield sleep$1(3000);
5415
5413
  // Wait til user does something (move mouse, tap, scroll, click etc)
5416
5414
  console.debug('waiting for someone to do something');
5417
- yield userDoesSomething.pipe(take(1)).toPromise();
5415
+ yield firstValueFrom(userDoesSomething);
5418
5416
  console.debug('someone did something!');
5419
5417
  });
5420
5418
  }
@@ -6305,7 +6303,7 @@ function dexieCloud(dexie) {
6305
6303
  const syncComplete = new Subject();
6306
6304
  dexie.cloud = {
6307
6305
  // @ts-ignore
6308
- version: "4.0.4",
6306
+ version: "4.0.5",
6309
6307
  options: Object.assign({}, DEFAULT_OPTIONS),
6310
6308
  schema: null,
6311
6309
  get currentUserId() {
@@ -6363,10 +6361,8 @@ function dexieCloud(dexie) {
6363
6361
  const syncState = db.cloud.persistedSyncState.value;
6364
6362
  triggerSync(db, purpose);
6365
6363
  if (wait) {
6366
- const newSyncState = yield db.cloud.persistedSyncState
6367
- .pipe(filter((newSyncState) => (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) != null &&
6368
- (!syncState || newSyncState.timestamp > syncState.timestamp)), take(1))
6369
- .toPromise();
6364
+ const newSyncState = yield firstValueFrom(db.cloud.persistedSyncState.pipe(filter((newSyncState) => (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) != null &&
6365
+ (!syncState || newSyncState.timestamp > syncState.timestamp))));
6370
6366
  if (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error) {
6371
6367
  throw new Error(`Sync error: ` + newSyncState.error);
6372
6368
  }
@@ -6377,16 +6373,14 @@ function dexieCloud(dexie) {
6377
6373
  triggerSync(db, purpose);
6378
6374
  if (wait) {
6379
6375
  console.debug('db.cloud.login() is waiting for sync completion...');
6380
- yield from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6376
+ yield firstValueFrom(from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6381
6377
  const syncNeeded = yield isSyncNeeded(db);
6382
6378
  const newSyncState = yield db.getPersistedSyncState();
6383
6379
  if ((newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) !== (syncState === null || syncState === void 0 ? void 0 : syncState.timestamp) &&
6384
6380
  (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error))
6385
6381
  throw new Error(`Sync error: ` + newSyncState.error);
6386
6382
  return syncNeeded;
6387
- })))
6388
- .pipe(filter((isNeeded) => !isNeeded), take(1))
6389
- .toPromise();
6383
+ }))).pipe(filter((isNeeded) => !isNeeded)));
6390
6384
  console.debug('Done waiting for sync completion because we have nothing to push anymore');
6391
6385
  }
6392
6386
  }
@@ -6435,7 +6429,7 @@ function dexieCloud(dexie) {
6435
6429
  const swRegistrations = 'serviceWorker' in navigator
6436
6430
  ? yield navigator.serviceWorker.getRegistrations()
6437
6431
  : [];
6438
- const initiallySynced = yield db.transaction('rw', db.$syncState, () => __awaiter(this, void 0, void 0, function* () {
6432
+ const [initiallySynced, lastSyncedRealms] = yield db.transaction('rw', db.$syncState, () => __awaiter(this, void 0, void 0, function* () {
6439
6433
  var _h, _j;
6440
6434
  const { options, schema } = db.cloud;
6441
6435
  const [persistedOptions, persistedSchema, persistedSyncState] = yield Promise.all([
@@ -6508,7 +6502,7 @@ function dexieCloud(dexie) {
6508
6502
  // Let's assign all props as the newPersistedSchems should be what we should be working with.
6509
6503
  Object.assign(schema, newPersistedSchema);
6510
6504
  }
6511
- return persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.initiallySynced;
6505
+ return [persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.initiallySynced, persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.realms];
6512
6506
  }));
6513
6507
  if (initiallySynced) {
6514
6508
  db.setInitiallySynced(true);
@@ -6528,19 +6522,27 @@ function dexieCloud(dexie) {
6528
6522
  // with things from the database and not just the default values.
6529
6523
  // This is so that when db.open() completes, user should be safe
6530
6524
  // to subscribe to these observables and get actual data.
6531
- yield combineLatest([
6525
+ yield firstValueFrom(combineLatest([
6532
6526
  currentUserEmitter.pipe(skip(1), take(1)),
6533
6527
  db.cloud.persistedSyncState.pipe(skip(1), take(1)),
6534
- ]).toPromise();
6528
+ ]));
6535
6529
  }
6536
6530
  // HERE: If requireAuth, do athentication now.
6537
6531
  let changedUser = false;
6532
+ const user = yield db.getCurrentUser();
6538
6533
  if ((_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.requireAuth) {
6539
- const user = yield db.getCurrentUser();
6540
6534
  if (!user.isLoggedIn) {
6541
6535
  changedUser = yield login(db);
6542
6536
  }
6543
6537
  }
6538
+ if (user.isLoggedIn && (!lastSyncedRealms || !lastSyncedRealms.includes(user.userId))) {
6539
+ // User has been logged in but this is not reflected in the sync state.
6540
+ // This can happen if page is reloaded after login but before the sync call following
6541
+ // the login was complete.
6542
+ // The user is to be viewed as changed becuase current syncState does not reflect the presence
6543
+ // of the logged-in user.
6544
+ changedUser = true; // Set changedUser to true to trigger a pull-sync later down.
6545
+ }
6544
6546
  if (localSyncWorker)
6545
6547
  localSyncWorker.stop();
6546
6548
  localSyncWorker = null;
@@ -6585,7 +6587,7 @@ function dexieCloud(dexie) {
6585
6587
  }
6586
6588
  }
6587
6589
  // @ts-ignore
6588
- dexieCloud.version = "4.0.4";
6590
+ dexieCloud.version = "4.0.5";
6589
6591
  Dexie.Cloud = dexieCloud;
6590
6592
 
6591
6593
  // In case the SW lives for a while, let it reuse already opened connections: