Created
May 14, 2025 17:31
-
-
Save SgtPooki/02bca1c9c668067e61d544aa498b8cf2 to your computer and use it in GitHub Desktop.
simple script to test support for BroadcastChanel and MessageChannel across node/bun/deno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple feature‑detection helpers | |
* (exported so test code can get 100 % coverage). | |
*/ | |
export const hasBroadcastChannel = (): boolean => | |
typeof globalThis.BroadcastChannel === "function"; | |
export const hasMessageChannel = (): boolean => | |
typeof globalThis.MessageChannel === "function"; | |
/** | |
* Demo / CLI entry‑point | |
*/ | |
export async function runDemo(): Promise<void> { | |
console.log(`BroadcastChannel supported: ${hasBroadcastChannel()}`); | |
console.log(`MessageChannel supported: ${hasMessageChannel()}`); | |
// ─── Send a smoke‑test message through each channel ────────────────────────── | |
if (hasBroadcastChannel()) { | |
const bc = new BroadcastChannel("demo"); | |
bc.onmessage = (e) => console.log("BroadcastChannel received ▶", e.data); | |
bc.postMessage("hello from BroadcastChannel"); | |
// allow the event loop one turn so we actually receive the message | |
await new Promise((r) => setTimeout(r, 0)); | |
bc.close(); | |
} | |
if (hasMessageChannel()) { | |
const { port1, port2 } = new MessageChannel(); | |
port1.onmessage = (e) => console.log("MessageChannel received ▶", e.data); | |
port2.postMessage("hello from MessageChannel"); | |
await new Promise((r) => setTimeout(r, 0)); | |
port1.close(); | |
port2.close(); | |
} | |
} | |
// When run via `deno run channels_support.ts` | |
if (import.meta.main) await runDemo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment