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