Skip to content

Diagnostics app reconnect #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-lions-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/diagnostics-app': patch
---

Fix reconnecting after changing credentials.
6 changes: 3 additions & 3 deletions tools/diagnostics-app/src/app/views/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
const powerSync = usePowerSync();
const navigate = useNavigate();

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

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

// Cannot use `useStatus()`, since we're not using the default sync implementation.
React.useEffect(() => {
const l = sync.registerListener({
const l = sync?.registerListener({
statusChanged: (status) => {
setSyncStatus(status);
setSyncError(status.dataFlowStatus.downloadError ?? null);
}
});
return () => l();
return () => l?.();
}, []);

const drawerWidth = 320;
Expand Down
2 changes: 1 addition & 1 deletion tools/diagnostics-app/src/app/views/sync-diagnostics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default function SyncDiagnosticsPage() {
// Similar to db.currentState.hasSynced, but synchronized to the onChange events
const { synced_at } = await db.get<{ synced_at: string | null }>('SELECT powersync_last_synced_at() as synced_at');
setlastSyncedAt(synced_at ? new Date(synced_at + 'Z') : null);
if (synced_at != null && !sync.syncStatus.dataFlowStatus.downloading) {
if (synced_at != null && !sync?.syncStatus.dataFlowStatus.downloading) {
// These are potentially expensive queries - do not run during initial sync
const bucketRows = await db.getAll(BUCKETS_QUERY);
const tableRows = await db.getAll(TABLES_QUERY);
Expand Down
30 changes: 16 additions & 14 deletions tools/diagnostics-app/src/library/powersync/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,9 @@ export const db = new PowerSyncDatabase({

export const connector = new TokenConnector();

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

const syncOptions: WebStreamingSyncImplementationOptions = {
adapter,
remote,
uploadCrud: async () => {
// No-op
},
identifier: 'diagnostics'
};
export const sync = new WebStreamingSyncImplementation(syncOptions);
export let sync: WebStreamingSyncImplementation | undefined;

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

export async function connect() {
const params = getParams();
await sync?.disconnect();
const remote = new WebRemote(connector);
const syncOptions: WebStreamingSyncImplementationOptions = {
adapter,
remote,
uploadCrud: async () => {
// No-op
},
identifier: 'diagnostics'
};
sync = new WebStreamingSyncImplementation(syncOptions);
await sync.connect({ params });
if (!sync.syncStatus.connected) {
const error = sync.syncStatus.dataFlowStatus.downloadError ?? new Error('Failed to connect');
// Disconnect but don't wait for it
sync.disconnect();
await sync.disconnect();
throw error;
}
}

export async function clearData() {
await sync.disconnect();
await sync?.disconnect();
await db.disconnectAndClear();
await schemaManager.clear();
await schemaManager.refreshSchema(db.database);
if (connector.hasCredentials()) {
const params = getParams();
await sync.connect({ params });
await sync?.connect({ params });
}
}

export async function disconnect() {
await sync.disconnect();
await sync?.disconnect();
}

export async function signOut() {
Expand Down