Created
January 20, 2026 18:25
-
-
Save hnordt/b243b40b1fab29bd4953a7326f230507 to your computer and use it in GitHub Desktop.
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
| import { render } from "preact"; | |
| import { signal } from "@preact/signals"; | |
| function createEventSource(url: string) { | |
| const source = new EventSource(url); | |
| const status = signal(source.readyState); | |
| function updateStatus() { | |
| status.value = source.readyState; | |
| } | |
| source.addEventListener("open", updateStatus); | |
| source.addEventListener("error", updateStatus); | |
| return { | |
| get status() { | |
| return status.value; | |
| }, | |
| close() { | |
| source.close(); | |
| updateStatus(); | |
| }, | |
| }; | |
| } | |
| const source = createEventSource("/stream"); | |
| function App() { | |
| return ( | |
| <span> | |
| {source.status === EventSource.CONNECTING | |
| ? "🟠" | |
| : source.status === EventSource.OPEN | |
| ? "🟢" | |
| : source.status === EventSource.CLOSED | |
| ? "🔴" | |
| : "⚪️"} | |
| </span> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment