dexie-cloud-addon 4.0.4 → 4.0.6

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 25 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.
@@ -4710,6 +4708,10 @@ function createImplicitPropSetterMiddleware(db) {
4710
4708
  }
4711
4709
  const trans = req.trans;
4712
4710
  if ((_b = (_a = db.cloud.schema) === null || _a === void 0 ? void 0 : _a[tableName]) === null || _b === void 0 ? void 0 : _b.markedForSync) {
4711
+ if (trans.mode === 'versionchange') {
4712
+ // Don't mutate tables marked for sync in versionchange transactions.
4713
+ return Promise.reject(new Dexie.UpgradeError(`Dexie Cloud Addon: Cannot upgrade or populate synced table "${tableName}". See https://dexie.org/cloud/docs/best-practices`));
4714
+ }
4713
4715
  if (req.type === 'add' || req.type === 'put') {
4714
4716
  if (tableName === 'members') {
4715
4717
  for (const member of req.values) {
@@ -5414,7 +5416,7 @@ function waitAndReconnectWhenUserDoesSomething(error) {
5414
5416
  yield sleep(3000);
5415
5417
  // Wait til user does something (move mouse, tap, scroll, click etc)
5416
5418
  console.debug('waiting for someone to do something');
5417
- yield userDoesSomething.pipe(take(1)).toPromise();
5419
+ yield firstValueFrom(userDoesSomething);
5418
5420
  console.debug('someone did something!');
5419
5421
  });
5420
5422
  }
@@ -6312,7 +6314,7 @@ function dexieCloud(dexie) {
6312
6314
  const syncComplete = new Subject();
6313
6315
  dexie.cloud = {
6314
6316
  // @ts-ignore
6315
- version: "4.0.4",
6317
+ version: "4.0.5",
6316
6318
  options: Object.assign({}, DEFAULT_OPTIONS),
6317
6319
  schema: null,
6318
6320
  get currentUserId() {
@@ -6370,10 +6372,8 @@ function dexieCloud(dexie) {
6370
6372
  const syncState = db.cloud.persistedSyncState.value;
6371
6373
  triggerSync(db, purpose);
6372
6374
  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();
6375
+ const newSyncState = yield firstValueFrom(db.cloud.persistedSyncState.pipe(filter((newSyncState) => (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) != null &&
6376
+ (!syncState || newSyncState.timestamp > syncState.timestamp))));
6377
6377
  if (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error) {
6378
6378
  throw new Error(`Sync error: ` + newSyncState.error);
6379
6379
  }
@@ -6384,16 +6384,14 @@ function dexieCloud(dexie) {
6384
6384
  triggerSync(db, purpose);
6385
6385
  if (wait) {
6386
6386
  console.debug('db.cloud.login() is waiting for sync completion...');
6387
- yield from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6387
+ yield firstValueFrom(from$1(liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6388
6388
  const syncNeeded = yield isSyncNeeded(db);
6389
6389
  const newSyncState = yield db.getPersistedSyncState();
6390
6390
  if ((newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.timestamp) !== (syncState === null || syncState === void 0 ? void 0 : syncState.timestamp) &&
6391
6391
  (newSyncState === null || newSyncState === void 0 ? void 0 : newSyncState.error))
6392
6392
  throw new Error(`Sync error: ` + newSyncState.error);
6393
6393
  return syncNeeded;
6394
- })))
6395
- .pipe(filter((isNeeded) => !isNeeded), take(1))
6396
- .toPromise();
6394
+ }))).pipe(filter((isNeeded) => !isNeeded)));
6397
6395
  console.debug('Done waiting for sync completion because we have nothing to push anymore');
6398
6396
  }
6399
6397
  }
@@ -6442,7 +6440,7 @@ function dexieCloud(dexie) {
6442
6440
  const swRegistrations = 'serviceWorker' in navigator
6443
6441
  ? yield navigator.serviceWorker.getRegistrations()
6444
6442
  : [];
6445
- const initiallySynced = yield db.transaction('rw', db.$syncState, () => __awaiter(this, void 0, void 0, function* () {
6443
+ const [initiallySynced, lastSyncedRealms] = yield db.transaction('rw', db.$syncState, () => __awaiter(this, void 0, void 0, function* () {
6446
6444
  var _h, _j;
6447
6445
  const { options, schema } = db.cloud;
6448
6446
  const [persistedOptions, persistedSchema, persistedSyncState] = yield Promise.all([
@@ -6515,7 +6513,7 @@ function dexieCloud(dexie) {
6515
6513
  // Let's assign all props as the newPersistedSchems should be what we should be working with.
6516
6514
  Object.assign(schema, newPersistedSchema);
6517
6515
  }
6518
- return persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.initiallySynced;
6516
+ return [persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.initiallySynced, persistedSyncState === null || persistedSyncState === void 0 ? void 0 : persistedSyncState.realms];
6519
6517
  }));
6520
6518
  if (initiallySynced) {
6521
6519
  db.setInitiallySynced(true);
@@ -6535,19 +6533,27 @@ function dexieCloud(dexie) {
6535
6533
  // with things from the database and not just the default values.
6536
6534
  // This is so that when db.open() completes, user should be safe
6537
6535
  // to subscribe to these observables and get actual data.
6538
- yield combineLatest([
6536
+ yield firstValueFrom(combineLatest([
6539
6537
  currentUserEmitter.pipe(skip(1), take(1)),
6540
6538
  db.cloud.persistedSyncState.pipe(skip(1), take(1)),
6541
- ]).toPromise();
6539
+ ]));
6542
6540
  }
6543
6541
  // HERE: If requireAuth, do athentication now.
6544
6542
  let changedUser = false;
6543
+ const user = yield db.getCurrentUser();
6545
6544
  if ((_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.requireAuth) {
6546
- const user = yield db.getCurrentUser();
6547
6545
  if (!user.isLoggedIn) {
6548
6546
  changedUser = yield login(db);
6549
6547
  }
6550
6548
  }
6549
+ if (user.isLoggedIn && (!lastSyncedRealms || !lastSyncedRealms.includes(user.userId))) {
6550
+ // User has been logged in but this is not reflected in the sync state.
6551
+ // This can happen if page is reloaded after login but before the sync call following
6552
+ // the login was complete.
6553
+ // The user is to be viewed as changed becuase current syncState does not reflect the presence
6554
+ // of the logged-in user.
6555
+ changedUser = true; // Set changedUser to true to trigger a pull-sync later down.
6556
+ }
6551
6557
  if (localSyncWorker)
6552
6558
  localSyncWorker.stop();
6553
6559
  localSyncWorker = null;
@@ -6592,7 +6598,7 @@ function dexieCloud(dexie) {
6592
6598
  }
6593
6599
  }
6594
6600
  // @ts-ignore
6595
- dexieCloud.version = "4.0.4";
6601
+ dexieCloud.version = "4.0.5";
6596
6602
  Dexie.Cloud = dexieCloud;
6597
6603
 
6598
6604
  export { dexieCloud as default, dexieCloud, getTiedObjectId, getTiedRealmId, resolveText };