Skip to content

Commit 6707966

Browse files
authored
Diagnostics app reconnect (#624)
1 parent ddc0bd1 commit 6707966

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

.changeset/silent-lions-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/diagnostics-app': patch
3+
---
4+
5+
Fix reconnecting after changing credentials.

tools/diagnostics-app/src/app/views/layout.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
4343
const powerSync = usePowerSync();
4444
const navigate = useNavigate();
4545

46-
const [syncStatus, setSyncStatus] = React.useState(sync.syncStatus);
46+
const [syncStatus, setSyncStatus] = React.useState(sync?.syncStatus);
4747
const [syncError, setSyncError] = React.useState<Error | null>(null);
4848
const { title } = useNavigationPanel();
4949

@@ -101,13 +101,13 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
101101

102102
// Cannot use `useStatus()`, since we're not using the default sync implementation.
103103
React.useEffect(() => {
104-
const l = sync.registerListener({
104+
const l = sync?.registerListener({
105105
statusChanged: (status) => {
106106
setSyncStatus(status);
107107
setSyncError(status.dataFlowStatus.downloadError ?? null);
108108
}
109109
});
110-
return () => l();
110+
return () => l?.();
111111
}, []);
112112

113113
const drawerWidth = 320;

tools/diagnostics-app/src/app/views/sync-diagnostics.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default function SyncDiagnosticsPage() {
8282
// Similar to db.currentState.hasSynced, but synchronized to the onChange events
8383
const { synced_at } = await db.get<{ synced_at: string | null }>('SELECT powersync_last_synced_at() as synced_at');
8484
setlastSyncedAt(synced_at ? new Date(synced_at + 'Z') : null);
85-
if (synced_at != null && !sync.syncStatus.dataFlowStatus.downloading) {
85+
if (synced_at != null && !sync?.syncStatus.dataFlowStatus.downloading) {
8686
// These are potentially expensive queries - do not run during initial sync
8787
const bucketRows = await db.getAll(BUCKETS_QUERY);
8888
const tableRows = await db.getAll(TABLES_QUERY);

tools/diagnostics-app/src/library/powersync/ConnectionManager.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,9 @@ export const db = new PowerSyncDatabase({
4444

4545
export const connector = new TokenConnector();
4646

47-
const remote = new WebRemote(connector);
4847
const adapter = new RecordingStorageAdapter(db.database, schemaManager);
4948

50-
const syncOptions: WebStreamingSyncImplementationOptions = {
51-
adapter,
52-
remote,
53-
uploadCrud: async () => {
54-
// No-op
55-
},
56-
identifier: 'diagnostics'
57-
};
58-
export const sync = new WebStreamingSyncImplementation(syncOptions);
49+
export let sync: WebStreamingSyncImplementation | undefined;
5950

6051
export interface SyncErrorListener extends BaseListener {
6152
lastErrorUpdated?: ((error: Error) => void) | undefined;
@@ -67,28 +58,39 @@ if (connector.hasCredentials()) {
6758

6859
export async function connect() {
6960
const params = getParams();
61+
await sync?.disconnect();
62+
const remote = new WebRemote(connector);
63+
const syncOptions: WebStreamingSyncImplementationOptions = {
64+
adapter,
65+
remote,
66+
uploadCrud: async () => {
67+
// No-op
68+
},
69+
identifier: 'diagnostics'
70+
};
71+
sync = new WebStreamingSyncImplementation(syncOptions);
7072
await sync.connect({ params });
7173
if (!sync.syncStatus.connected) {
7274
const error = sync.syncStatus.dataFlowStatus.downloadError ?? new Error('Failed to connect');
7375
// Disconnect but don't wait for it
74-
sync.disconnect();
76+
await sync.disconnect();
7577
throw error;
7678
}
7779
}
7880

7981
export async function clearData() {
80-
await sync.disconnect();
82+
await sync?.disconnect();
8183
await db.disconnectAndClear();
8284
await schemaManager.clear();
8385
await schemaManager.refreshSchema(db.database);
8486
if (connector.hasCredentials()) {
8587
const params = getParams();
86-
await sync.connect({ params });
88+
await sync?.connect({ params });
8789
}
8890
}
8991

9092
export async function disconnect() {
91-
await sync.disconnect();
93+
await sync?.disconnect();
9294
}
9395

9496
export async function signOut() {

0 commit comments

Comments
 (0)