Skip to content

Commit

Permalink
Rewrite deprecated .toPromise()
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed May 3, 2024
1 parent 2dabd7d commit 944e3fc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
46 changes: 21 additions & 25 deletions addons/dexie-cloud/src/dexie-cloud-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
DBRealmMember,
getDbNameFromDbUrl,
} from 'dexie-cloud-common';
import { BehaviorSubject, combineLatest, from, fromEvent, Subject } from 'rxjs';
import { BehaviorSubject, combineLatest, firstValueFrom, from, fromEvent, Subject } from 'rxjs';
import { filter, map, skip, startWith, switchMap, take } from 'rxjs/operators';
import { login } from './authentication/login';
import { UNAUTHORIZED_USER } from './authentication/UNAUTHORIZED_USER';
Expand Down Expand Up @@ -174,16 +174,15 @@ export function dexieCloud(dexie: Dexie) {
const syncState = db.cloud.persistedSyncState.value;
triggerSync(db, purpose);
if (wait) {
const newSyncState = await db.cloud.persistedSyncState
.pipe(
const newSyncState = await firstValueFrom(
db.cloud.persistedSyncState.pipe(
filter(
(newSyncState) =>
newSyncState?.timestamp != null &&
(!syncState || newSyncState.timestamp > syncState.timestamp!)
),
take(1)
)
)
.toPromise();
);
if (newSyncState?.error) {
throw new Error(`Sync error: ` + newSyncState.error);
}
Expand All @@ -193,23 +192,20 @@ export function dexieCloud(dexie: Dexie) {
triggerSync(db, purpose);
if (wait) {
console.debug('db.cloud.login() is waiting for sync completion...');
await from(
liveQuery(async () => {
const syncNeeded = await isSyncNeeded(db);
const newSyncState = await db.getPersistedSyncState();
if (
newSyncState?.timestamp !== syncState?.timestamp &&
newSyncState?.error
)
throw new Error(`Sync error: ` + newSyncState.error);
return syncNeeded;
})
)
.pipe(
filter((isNeeded) => !isNeeded),
take(1)
)
.toPromise();
await firstValueFrom(
from(
liveQuery(async () => {
const syncNeeded = await isSyncNeeded(db);
const newSyncState = await db.getPersistedSyncState();
if (
newSyncState?.timestamp !== syncState?.timestamp &&
newSyncState?.error
)
throw new Error(`Sync error: ` + newSyncState.error);
return syncNeeded;
})
).pipe(filter((isNeeded) => !isNeeded))
);
console.debug(
'Done waiting for sync completion because we have nothing to push anymore'
);
Expand Down Expand Up @@ -393,10 +389,10 @@ export function dexieCloud(dexie: Dexie) {
// with things from the database and not just the default values.
// This is so that when db.open() completes, user should be safe
// to subscribe to these observables and get actual data.
await combineLatest([
await firstValueFrom(combineLatest([
currentUserEmitter.pipe(skip(1), take(1)),
db.cloud.persistedSyncState.pipe(skip(1), take(1)),
]).toPromise();
]));
}

// HERE: If requireAuth, do athentication now.
Expand Down
4 changes: 2 additions & 2 deletions addons/dexie-cloud/src/sync/connectWebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BehaviorSubject, from, Observable, of, throwError } from 'rxjs';
import { BehaviorSubject, firstValueFrom, from, Observable, of, throwError } from 'rxjs';
import {
catchError,
debounceTime,
Expand Down Expand Up @@ -39,7 +39,7 @@ async function waitAndReconnectWhenUserDoesSomething(error: Error) {
await sleep(3000);
// Wait til user does something (move mouse, tap, scroll, click etc)
console.debug('waiting for someone to do something');
await userDoesSomething.pipe(take(1)).toPromise();
await firstValueFrom(userDoesSomething);
console.debug('someone did something!');
}

Expand Down
11 changes: 5 additions & 6 deletions addons/dexie-cloud/src/sync/messagesFromServerQueue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { filter, take } from 'rxjs/operators';
import { DexieCloudDB } from '../db/DexieCloudDB';
import { WSConnectionMsg } from '../WSObservable';
Expand Down Expand Up @@ -73,12 +73,11 @@ export function MessagesFromServerConsumer(db: DexieCloudDB) {
// If the sync worker or service worker is syncing, wait 'til thei're done.
// It's no need to have two channels at the same time - even though it wouldnt
// be a problem - this is an optimization.
await db.cloud.syncState
.pipe(
filter(({ phase }) => phase === 'in-sync' || phase === 'error'),
take(1)
await firstValueFrom(
db.cloud.syncState.pipe(
filter(({ phase }) => phase === 'in-sync' || phase === 'error')
)
.toPromise();
);
console.debug('processing msg', msg);
const persistedSyncState = db.cloud.persistedSyncState.value;
//syncState.
Expand Down

0 comments on commit 944e3fc

Please sign in to comment.