Skip to content

Commit

Permalink
fix(toolbar): Use shape sub notifier in toolbar (#1286)
Browse files Browse the repository at this point in the history
Remove the `setInterval` call used to monitor shapes in the toolbar and
use the new notifier method instead.
  • Loading branch information
msfstef committed May 23, 2024
1 parent ff08923 commit 6d4fb3d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-candles-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@electric-sql/debug-toolbar": patch
---

Use shape sync status notifier for updating shapes tab.
4 changes: 4 additions & 0 deletions components/toolbar/src/api/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface ToolbarInterface {
toggleSatelliteStatus(name: string): Promise<void>

getSatelliteShapeSubscriptions(name: string): DebugShape[]
subscribeToSatelliteShapeSubscriptions(
name: string,
callback: (shapes: DebugShape[]) => void,
): UnsubscribeFunction

resetDb(dbName: string): Promise<void>
queryDb(dbName: string, statement: Statement): Promise<Row[]>
Expand Down
11 changes: 11 additions & 0 deletions components/toolbar/src/api/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ export class Toolbar implements ToolbarInterface {
)
}

subscribeToSatelliteShapeSubscriptions(
name: string,
callback: (shapes: DebugShape[]) => void,
): UnsubscribeFunction {
const sat = this.getSatellite(name)
callback(this.getSatelliteShapeSubscriptions(name))
return sat.notifier.subscribeToShapeSubscriptionSyncStatusChanges(() =>
callback(this.getSatelliteShapeSubscriptions(name)),
)
}

resetDb(dbName: string): Promise<void> {
const DBDeleteRequest = window.indexedDB.deleteDatabase(dbName)
DBDeleteRequest.onsuccess = () =>
Expand Down
10 changes: 4 additions & 6 deletions components/toolbar/src/tabs/ShapesTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ export default function ShapesTab({
)

useEffect(() => {
// TODO: need notifier API for shape status
// periodically refresh shape subscriptions
const interval = setInterval(
() => setShapes(api.getSatelliteShapeSubscriptions(dbName)),
500,
const unsubscribe = api.subscribeToSatelliteShapeSubscriptions(
dbName,
setShapes,
)
return () => clearInterval(interval)
return unsubscribe
}, [dbName, api])

if (shapes.length === 0) {
Expand Down

0 comments on commit 6d4fb3d

Please sign in to comment.