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
  *
@@ -4209,9 +4209,7 @@ function MessagesFromServerConsumer(db) {
4209
4209
  // If the sync worker or service worker is syncing, wait 'til thei're done.
4210
4210
  // It's no need to have two channels at the same time - even though it wouldnt
4211
4211
  // be a problem - this is an optimization.
4212
- yield db.cloud.syncState
4213
- .pipe(filter(({ phase }) => phase === 'in-sync' || phase === 'error'), take(1))
4214
- .toPromise();
4212
+ yield firstValueFrom(db.cloud.syncState.pipe(filter(({ phase }) => phase === 'in-sync' || phase === 'error')));
4215
4213
  console.debug('processing msg', msg);
4216
4214
  const persistedSyncState = db.cloud.persistedSyncState.value;
4217
4215
  //syncState.
@@ -5414,7 +5412,7 @@ function waitAndReconnectWhenUserDoesSomething(error) {
5414
5412
  yield sleep(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
  }
@@ -6312,7 +6310,7 @@ function dexieCloud(dexie) {
6312
6310
  const syncComplete = new Subject();
6313
6311
  dexie.cloud = {
6314
6312
  // @ts-ignore
6315
- version: "4.0.4",
6313
+ version: "4.0.5",
6316
6314
  options: Object.assign({}, DEFAULT_OPTIONS),
6317
6315
  schema: null,
6318
6316
  get currentUserId() {
@@ -6370,10 +6368,8 @@ function dexieCloud(dexie) {
6370
6368
  const syncState = db.cloud.persistedSyncState.value;
6371
6369
  triggerSync(db, purpose);
6372
6370
  if (wait) {
6373
- const newSyncState = yield db.cloud.persistedSyncState
6374
- .pipe(filter((newSyncState) => (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) != null &&
6375
- (!syncState || newSyncState.timestamp > syncState.timestamp)), take(1))
6376
- .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))));
6377
6373
  if (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error) {
6378
6374
  throw new Error(`Sync error: ` + newSyncState.error);
6379
6375
  }
@@ -6384,16 +6380,14 @@ function dexieCloud(dexie) {
6384
6380
  triggerSync(db, purpose);
6385
6381
  if (wait) {
6386
6382
  console.debug('db.cloud.login() is waiting for sync completion...');
6387
- yield from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6383
+ yield firstValueFrom(from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6388
6384
  const syncNeeded = yield isSyncNeeded(db);
6389
6385
  const newSyncState = yield db.getPersistedSyncState();
6390
6386
  if ((newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) !== (syncState === null || syncState === void 0 ? void 0 : syncState.timestamp) &&
6391
6387
  (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error))
6392
6388
  throw new Error(`Sync error: ` + newSyncState.error);
6393
6389
  return syncNeeded;
6394
- })))
6395
- .pipe(filter((isNeeded) => !isNeeded), take(1))
6396
- .toPromise();
6390
+ }))).pipe(filter((isNeeded) => !isNeeded)));
6397
6391
  console.debug('Done waiting for sync completion because we have nothing to push anymore');
6398
6392
  }
6399
6393
  }
@@ -6442,7 +6436,7 @@ function dexieCloud(dexie) {
6442
6436
  const swRegistrations = 'serviceWorker' in navigator
6443
6437
  ? yield navigator.serviceWorker.getRegistrations()
6444
6438
  : [];
6445
- 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* () {
6446
6440
  var _h, _j;
6447
6441
  const { options, schema } = db.cloud;
6448
6442
  const [persistedOptions, persistedSchema, persistedSyncState] = yield Promise.all([
@@ -6515,7 +6509,7 @@ function dexieCloud(dexie) {
6515
6509
  // Let's assign all props as the newPersistedSchems should be what we should be working with.
6516
6510
  Object.assign(schema, newPersistedSchema);
6517
6511
  }
6518
- 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];
6519
6513
  }));
6520
6514
  if (initiallySynced) {
6521
6515
  db.setInitiallySynced(true);
@@ -6535,19 +6529,27 @@ function dexieCloud(dexie) {
6535
6529
  // with things from the database and not just the default values.
6536
6530
  // This is so that when db.open() completes, user should be safe
6537
6531
  // to subscribe to these observables and get actual data.
6538
- yield combineLatest([
6532
+ yield firstValueFrom(combineLatest([
6539
6533
  currentUserEmitter.pipe(skip(1), take(1)),
6540
6534
  db.cloud.persistedSyncState.pipe(skip(1), take(1)),
6541
- ]).toPromise();
6535
+ ]));
6542
6536
  }
6543
6537
  // HERE: If requireAuth, do athentication now.
6544
6538
  let changedUser = false;
6539
+ const user = yield db.getCurrentUser();
6545
6540
  if ((_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.requireAuth) {
6546
- const user = yield db.getCurrentUser();
6547
6541
  if (!user.isLoggedIn) {
6548
6542
  changedUser = yield login(db);
6549
6543
  }
6550
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.
6552
+ }
6551
6553
  if (localSyncWorker)
6552
6554
  localSyncWorker.stop();
6553
6555
  localSyncWorker = null;
@@ -6592,7 +6594,7 @@ function dexieCloud(dexie) {
6592
6594
  }
6593
6595
  }
6594
6596
  // @ts-ignore
6595
- dexieCloud.version = "4.0.4";
6597
+ dexieCloud.version = "4.0.5";
6596
6598
  Dexie.Cloud = dexieCloud;
6597
6599
 
6598
6600
  export { dexieCloud as default, dexieCloud, getTiedObjectId, getTiedRealmId, resolveText };