Skip to content

Commit

Permalink
dexie-cloud-addon: Behave when resetting database without reloading p…
Browse files Browse the repository at this point in the history
…age.

* WebSocket wasn't properly closed when closing db with `db.delete();db.open()`
* WebSocket connect attempts were made several times - first try was cancelled by another reconnect.
  • Loading branch information
dfahlander committed Jul 3, 2023
1 parent 124d709 commit c3705a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
9 changes: 6 additions & 3 deletions addons/dexie-cloud/src/helpers/dbOnClosed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import Dexie from "dexie";
*/
export function dbOnClosed(db: Dexie, handler: () => void) {
db.on.close.subscribe(handler);
const origClose = db.close;
db.close = function () {
// @ts-ignore
const origClose = db._close;
// @ts-ignore
db._close = function () {
origClose.call(this);
handler();
};
return () => {
db.on.close.unsubscribe(handler);
db.close = origClose;
// @ts-ignore
db._close = origClose;
};
}
32 changes: 17 additions & 15 deletions addons/dexie-cloud/src/sync/connectWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ export function connectWebSocket(db: DexieCloudDB) {
async ([userLogin, syncState]) =>
[userLogin, await computeRealmSetHash(syncState!)] as const
),
switchMap(([userLogin, realmSetHash]) =>
distinctUntilChanged(([prevUser, prevHash], [currUser, currHash]) => prevUser === currUser && prevHash === currHash ),
switchMap(([userLogin, realmSetHash]) => {
// Let server end query changes from last entry of same client-ID and forward.
// If no new entries, server won't bother the client. If new entries, server sends only those
// and the baseRev of the last from same client-ID.
userLogin
? new WSObservable(
if (userLogin) {
return new WSObservable(
db.cloud.options!.databaseUrl,
db.cloud.persistedSyncState!.value!.serverRevision,
realmSetHash,
Expand All @@ -104,9 +105,10 @@ export function connectWebSocket(db: DexieCloudDB) {
db.cloud.webSocketStatus,
userLogin.accessToken,
userLogin.accessTokenExpiration
)
: from([] as WSConnectionMsg[])
),
);
} else {
return from([] as WSConnectionMsg[]);
}}),
catchError((error) => {
if (error?.name === 'TokenExpiredError') {
console.debug(
Expand Down Expand Up @@ -138,21 +140,21 @@ export function connectWebSocket(db: DexieCloudDB) {
switchMap(() => createObservable())
);
})
);
) as Observable<WSConnectionMsg | null>;
}

return createObservable().subscribe(
(msg) => {
return createObservable().subscribe({
next: (msg) => {
if (msg) {
console.debug('WS got message', msg);
db.messageConsumer.enqueue(msg);
}
},
(error) => {
console.error('Oops! The main observable errored!', error);
error: (error) => {
console.error('WS got error', error);
},
() => {
console.error('Oops! The main observable completed!');
}
);
complete: () => {
console.debug('WS observable completed');
},
});
}

0 comments on commit c3705a7

Please sign in to comment.