Skip to content

Commit d7e77fd

Browse files
authored
fix: bind setTimeout to globalThis in SupabaseBroadcastAdapter for browser compatibility (#402)
# Fix setTimeout context binding issue in SupabaseBroadcastAdapter This PR fixes a browser compatibility issue in the `SupabaseBroadcastAdapter` class where `setTimeout` was losing its context binding when used in browser environments. The issue would cause an error: `"'setTimeout' called on an object that does not implement interface Window"`. The fix: - Properly binds `setTimeout` to the global context using `setTimeout.bind(globalThis)` in the constructor - Adds comprehensive tests to verify the fix works correctly This issue only manifested in browser environments, not in Node.js, which explains why our existing tests didn't catch it. I've added a test that simulates the browser's strict context requirements for `setTimeout`. The PR also includes a planning document for implementing browser-based testing to catch similar issues in the future.
1 parent 5dc5cfc commit d7e77fd

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@pgflow/client': patch
3+
---
4+
5+
Fix setTimeout context binding issue in SupabaseBroadcastAdapter for browser compatibility

pkgs/client/src/lib/SupabaseBroadcastAdapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class SupabaseBroadcastAdapter implements IFlowRealtime {
4242
this.#supabase = supabase;
4343
this.#reconnectionDelay = opts.reconnectDelayMs ?? 2000;
4444
this.#stabilizationDelay = opts.stabilizationDelayMs ?? 300;
45-
this.#schedule = opts.schedule ?? setTimeout;
45+
this.#schedule = opts.schedule ?? setTimeout.bind(globalThis);
4646
}
4747

4848
/**

0 commit comments

Comments
 (0)