dexie-cloud-addon 4.0.3 → 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.3, Sat Apr 13 2024
11
+ * Version 4.0.5, Sat May 04 2024
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -2628,11 +2628,16 @@ function login(db, hints) {
2628
2628
  const origUserId = currentUser.userId;
2629
2629
  if (currentUser.isLoggedIn && (!hints || (!hints.email && !hints.userId))) {
2630
2630
  const licenseStatus = ((_a = currentUser.license) === null || _a === void 0 ? void 0 : _a.status) || 'ok';
2631
- if (licenseStatus === 'ok' && currentUser.accessToken && (!currentUser.accessTokenExpiration || currentUser.accessTokenExpiration.getTime() > Date.now())) {
2631
+ if (licenseStatus === 'ok' &&
2632
+ currentUser.accessToken &&
2633
+ (!currentUser.accessTokenExpiration ||
2634
+ currentUser.accessTokenExpiration.getTime() > Date.now())) {
2632
2635
  // Already authenticated according to given hints. And license is valid.
2633
2636
  return false;
2634
2637
  }
2635
- if (currentUser.refreshToken && (!currentUser.refreshTokenExpiration || currentUser.refreshTokenExpiration.getTime() > Date.now())) {
2638
+ if (currentUser.refreshToken &&
2639
+ (!currentUser.refreshTokenExpiration ||
2640
+ currentUser.refreshTokenExpiration.getTime() > Date.now())) {
2636
2641
  // Refresh the token
2637
2642
  yield loadAccessToken(db);
2638
2643
  return false;
@@ -2644,7 +2649,8 @@ function login(db, hints) {
2644
2649
  lastLogin: new Date(0),
2645
2650
  });
2646
2651
  yield authenticate(db.cloud.options.databaseUrl, context, db.cloud.options.fetchTokens || otpFetchTokenCallback(db), db.cloud.userInteraction, hints);
2647
- if (origUserId !== UNAUTHORIZED_USER.userId && context.userId !== origUserId) {
2652
+ if (origUserId !== UNAUTHORIZED_USER.userId &&
2653
+ context.userId !== origUserId) {
2648
2654
  // User was logged in before, but now logged in as another user.
2649
2655
  yield logout(db);
2650
2656
  }
@@ -2663,7 +2669,7 @@ function login(db, hints) {
2663
2669
  yield setCurrentUser(db, context);
2664
2670
  // Make sure to resync as the new login will be authorized
2665
2671
  // for new realms.
2666
- triggerSync(db, "pull");
2672
+ triggerSync(db, 'pull');
2667
2673
  return context.userId !== origUserId;
2668
2674
  });
2669
2675
  }
@@ -4203,9 +4209,7 @@ function MessagesFromServerConsumer(db) {
4203
4209
  // If the sync worker or service worker is syncing, wait 'til thei're done.
4204
4210
  // It's no need to have two channels at the same time - even though it wouldnt
4205
4211
  // be a problem - this is an optimization.
4206
- yield db.cloud.syncState
4207
- .pipe(filter(({ phase }) => phase === 'in-sync' || phase === 'error'), take(1))
4208
- .toPromise();
4212
+ yield firstValueFrom(db.cloud.syncState.pipe(filter(({ phase }) => phase === 'in-sync' || phase === 'error')));
4209
4213
  console.debug('processing msg', msg);
4210
4214
  const persistedSyncState = db.cloud.persistedSyncState.value;
4211
4215
  //syncState.
@@ -5408,7 +5412,7 @@ function waitAndReconnectWhenUserDoesSomething(error) {
5408
5412
  yield sleep(3000);
5409
5413
  // Wait til user does something (move mouse, tap, scroll, click etc)
5410
5414
  console.debug('waiting for someone to do something');
5411
- yield userDoesSomething.pipe(take(1)).toPromise();
5415
+ yield firstValueFrom(userDoesSomething);
5412
5416
  console.debug('someone did something!');
5413
5417
  });
5414
5418
  }
@@ -6306,7 +6310,7 @@ function dexieCloud(dexie) {
6306
6310
  const syncComplete = new Subject();
6307
6311
  dexie.cloud = {
6308
6312
  // @ts-ignore
6309
- version: "4.0.3",
6313
+ version: "4.0.5",
6310
6314
  options: Object.assign({}, DEFAULT_OPTIONS),
6311
6315
  schema: null,
6312
6316
  get currentUserId() {
@@ -6364,10 +6368,8 @@ function dexieCloud(dexie) {
6364
6368
  const syncState = db.cloud.persistedSyncState.value;
6365
6369
  triggerSync(db, purpose);
6366
6370
  if (wait) {
6367
- const newSyncState = yield db.cloud.persistedSyncState
6368
- .pipe(filter((newSyncState) => (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) != null &&
6369
- (!syncState || newSyncState.timestamp > syncState.timestamp)), take(1))
6370
- .toPromise();
6371
+ const newSyncState = yield firstValueFrom(db.cloud.persistedSyncState.pipe(filter((newSyncState) => (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) != null &&
6372
+ (!syncState || newSyncState.timestamp > syncState.timestamp))));
6371
6373
  if (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error) {
6372
6374
  throw new Error(`Sync error: ` + newSyncState.error);
6373
6375
  }
@@ -6378,16 +6380,14 @@ function dexieCloud(dexie) {
6378
6380
  triggerSync(db, purpose);
6379
6381
  if (wait) {
6380
6382
  console.debug('db.cloud.login() is waiting for sync completion...');
6381
- yield from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6383
+ yield firstValueFrom(from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6382
6384
  const syncNeeded = yield isSyncNeeded(db);
6383
6385
  const newSyncState = yield db.getPersistedSyncState();
6384
6386
  if ((newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) !== (syncState === null || syncState === void 0 ? void 0 : syncState.timestamp) &&
6385
6387
  (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error))
6386
6388
  throw new Error(`Sync error: ` + newSyncState.error);
6387
6389
  return syncNeeded;
6388
- })))
6389
- .pipe(filter((isNeeded) => !isNeeded), take(1))
6390
- .toPromise();
6390
+ }))).pipe(filter((isNeeded) => !isNeeded)));
6391
6391
  console.debug('Done waiting for sync completion because we have nothing to push anymore');
6392
6392
  }
6393
6393
  }
@@ -6436,7 +6436,7 @@ function dexieCloud(dexie) {
6436
6436
  const swRegistrations = 'serviceWorker' in navigator
6437
6437
  ? yield navigator.serviceWorker.getRegistrations()
6438
6438
  : [];
6439
- const initiallySynced = yield db.transaction('rw', db.$syncState, () => __awaiter(this, void 0, void 0, function* () {
6439
+ const [initiallySynced, lastSyncedRealms] = yield db.transaction('rw', db.$syncState, () => __awaiter(this, void 0, void 0, function* () {
6440
6440
  var _h, _j;
6441
6441
  const { options, schema } = db.cloud;
6442
6442
  const [persistedOptions, persistedSchema, persistedSyncState] = yield Promise.all([
@@ -6509,7 +6509,7 @@ function dexieCloud(dexie) {
6509
6509
  // Let's assign all props as the newPersistedSchems should be what we should be working with.
6510
6510
  Object.assign(schema, newPersistedSchema);
6511
6511
  }
6512
- return persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.initiallySynced;
6512
+ return [persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.initiallySynced, persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.realms];
6513
6513
  }));
6514
6514
  if (initiallySynced) {
6515
6515
  db.setInitiallySynced(true);
@@ -6529,15 +6529,26 @@ function dexieCloud(dexie) {
6529
6529
  // with things from the database and not just the default values.
6530
6530
  // This is so that when db.open() completes, user should be safe
6531
6531
  // to subscribe to these observables and get actual data.
6532
- yield combineLatest([
6532
+ yield firstValueFrom(combineLatest([
6533
6533
  currentUserEmitter.pipe(skip(1), take(1)),
6534
6534
  db.cloud.persistedSyncState.pipe(skip(1), take(1)),
6535
- ]).toPromise();
6535
+ ]));
6536
6536
  }
6537
6537
  // HERE: If requireAuth, do athentication now.
6538
6538
  let changedUser = false;
6539
+ const user = yield db.getCurrentUser();
6539
6540
  if ((_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.requireAuth) {
6540
- changedUser = yield login(db);
6541
+ if (!user.isLoggedIn) {
6542
+ changedUser = yield login(db);
6543
+ }
6544
+ }
6545
+ if (user.isLoggedIn && (!lastSyncedRealms || !lastSyncedRealms.includes(user.userId))) {
6546
+ // User has been logged in but this is not reflected in the sync state.
6547
+ // This can happen if page is reloaded after login but before the sync call following
6548
+ // the login was complete.
6549
+ // The user is to be viewed as changed becuase current syncState does not reflect the presence
6550
+ // of the logged-in user.
6551
+ changedUser = true; // Set changedUser to true to trigger a pull-sync later down.
6541
6552
  }
6542
6553
  if (localSyncWorker)
6543
6554
  localSyncWorker.stop();
@@ -6583,7 +6594,7 @@ function dexieCloud(dexie) {
6583
6594
  }
6584
6595
  }
6585
6596
  // @ts-ignore
6586
- dexieCloud.version = "4.0.3";
6597
+ dexieCloud.version = "4.0.5";
6587
6598
  Dexie.Cloud = dexieCloud;
6588
6599
 
6589
6600
  export { dexieCloud as default, dexieCloud, getTiedObjectId, getTiedRealmId, resolveText };