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
  *
@@ -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.
@@ -4427,11 +4425,16 @@ function login(db, hints) {
4427
4425
  const origUserId = currentUser.userId;
4428
4426
  if (currentUser.isLoggedIn && (!hints || (!hints.email && !hints.userId))) {
4429
4427
  const licenseStatus = ((_a = currentUser.license) === null || _a === void 0 ? void 0 : _a.status) || 'ok';
4430
- if (licenseStatus === 'ok' && currentUser.accessToken && (!currentUser.accessTokenExpiration || currentUser.accessTokenExpiration.getTime() > Date.now())) {
4428
+ if (licenseStatus === 'ok' &&
4429
+ currentUser.accessToken &&
4430
+ (!currentUser.accessTokenExpiration ||
4431
+ currentUser.accessTokenExpiration.getTime() > Date.now())) {
4431
4432
  // Already authenticated according to given hints. And license is valid.
4432
4433
  return false;
4433
4434
  }
4434
- if (currentUser.refreshToken && (!currentUser.refreshTokenExpiration || currentUser.refreshTokenExpiration.getTime() > Date.now())) {
4435
+ if (currentUser.refreshToken &&
4436
+ (!currentUser.refreshTokenExpiration ||
4437
+ currentUser.refreshTokenExpiration.getTime() > Date.now())) {
4435
4438
  // Refresh the token
4436
4439
  yield loadAccessToken(db);
4437
4440
  return false;
@@ -4443,7 +4446,8 @@ function login(db, hints) {
4443
4446
  lastLogin: new Date(0),
4444
4447
  });
4445
4448
  yield authenticate(db.cloud.options.databaseUrl, context, db.cloud.options.fetchTokens || otpFetchTokenCallback(db), db.cloud.userInteraction, hints);
4446
- if (origUserId !== UNAUTHORIZED_USER.userId && context.userId !== origUserId) {
4449
+ if (origUserId !== UNAUTHORIZED_USER.userId &&
4450
+ context.userId !== origUserId) {
4447
4451
  // User was logged in before, but now logged in as another user.
4448
4452
  yield logout(db);
4449
4453
  }
@@ -4462,7 +4466,7 @@ function login(db, hints) {
4462
4466
  yield setCurrentUser(db, context);
4463
4467
  // Make sure to resync as the new login will be authorized
4464
4468
  // for new realms.
4465
- triggerSync(db, "pull");
4469
+ triggerSync(db, 'pull');
4466
4470
  return context.userId !== origUserId;
4467
4471
  });
4468
4472
  }
@@ -5408,7 +5412,7 @@ function waitAndReconnectWhenUserDoesSomething(error) {
5408
5412
  yield sleep$1(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
  }
@@ -6299,7 +6303,7 @@ function dexieCloud(dexie) {
6299
6303
  const syncComplete = new Subject();
6300
6304
  dexie.cloud = {
6301
6305
  // @ts-ignore
6302
- version: "4.0.3",
6306
+ version: "4.0.5",
6303
6307
  options: Object.assign({}, DEFAULT_OPTIONS),
6304
6308
  schema: null,
6305
6309
  get currentUserId() {
@@ -6357,10 +6361,8 @@ function dexieCloud(dexie) {
6357
6361
  const syncState = db.cloud.persistedSyncState.value;
6358
6362
  triggerSync(db, purpose);
6359
6363
  if (wait) {
6360
- const newSyncState = yield db.cloud.persistedSyncState
6361
- .pipe(filter((newSyncState) => (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) != null &&
6362
- (!syncState || newSyncState.timestamp > syncState.timestamp)), take(1))
6363
- .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))));
6364
6366
  if (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error) {
6365
6367
  throw new Error(`Sync error: ` + newSyncState.error);
6366
6368
  }
@@ -6371,16 +6373,14 @@ function dexieCloud(dexie) {
6371
6373
  triggerSync(db, purpose);
6372
6374
  if (wait) {
6373
6375
  console.debug('db.cloud.login() is waiting for sync completion...');
6374
- yield from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6376
+ yield firstValueFrom(from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6375
6377
  const syncNeeded = yield isSyncNeeded(db);
6376
6378
  const newSyncState = yield db.getPersistedSyncState();
6377
6379
  if ((newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) !== (syncState === null || syncState === void 0 ? void 0 : syncState.timestamp) &&
6378
6380
  (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error))
6379
6381
  throw new Error(`Sync error: ` + newSyncState.error);
6380
6382
  return syncNeeded;
6381
- })))
6382
- .pipe(filter((isNeeded) => !isNeeded), take(1))
6383
- .toPromise();
6383
+ }))).pipe(filter((isNeeded) => !isNeeded)));
6384
6384
  console.debug('Done waiting for sync completion because we have nothing to push anymore');
6385
6385
  }
6386
6386
  }
@@ -6429,7 +6429,7 @@ function dexieCloud(dexie) {
6429
6429
  const swRegistrations = 'serviceWorker' in navigator
6430
6430
  ? yield navigator.serviceWorker.getRegistrations()
6431
6431
  : [];
6432
- 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* () {
6433
6433
  var _h, _j;
6434
6434
  const { options, schema } = db.cloud;
6435
6435
  const [persistedOptions, persistedSchema, persistedSyncState] = yield Promise.all([
@@ -6502,7 +6502,7 @@ function dexieCloud(dexie) {
6502
6502
  // Let's assign all props as the newPersistedSchems should be what we should be working with.
6503
6503
  Object.assign(schema, newPersistedSchema);
6504
6504
  }
6505
- 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];
6506
6506
  }));
6507
6507
  if (initiallySynced) {
6508
6508
  db.setInitiallySynced(true);
@@ -6522,15 +6522,26 @@ function dexieCloud(dexie) {
6522
6522
  // with things from the database and not just the default values.
6523
6523
  // This is so that when db.open() completes, user should be safe
6524
6524
  // to subscribe to these observables and get actual data.
6525
- yield combineLatest([
6525
+ yield firstValueFrom(combineLatest([
6526
6526
  currentUserEmitter.pipe(skip(1), take(1)),
6527
6527
  db.cloud.persistedSyncState.pipe(skip(1), take(1)),
6528
- ]).toPromise();
6528
+ ]));
6529
6529
  }
6530
6530
  // HERE: If requireAuth, do athentication now.
6531
6531
  let changedUser = false;
6532
+ const user = yield db.getCurrentUser();
6532
6533
  if ((_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.requireAuth) {
6533
- changedUser = yield login(db);
6534
+ if (!user.isLoggedIn) {
6535
+ changedUser = yield login(db);
6536
+ }
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.
6534
6545
  }
6535
6546
  if (localSyncWorker)
6536
6547
  localSyncWorker.stop();
@@ -6576,7 +6587,7 @@ function dexieCloud(dexie) {
6576
6587
  }
6577
6588
  }
6578
6589
  // @ts-ignore
6579
- dexieCloud.version = "4.0.3";
6590
+ dexieCloud.version = "4.0.5";
6580
6591
  Dexie.Cloud = dexieCloud;
6581
6592
 
6582
6593
  // In case the SW lives for a while, let it reuse already opened connections: