Skip to content

Instantly share code, notes, and snippets.

@hnordt
Created January 20, 2026 18:25
Show Gist options
  • Select an option

  • Save hnordt/b243b40b1fab29bd4953a7326f230507 to your computer and use it in GitHub Desktop.

Select an option

Save hnordt/b243b40b1fab29bd4953a7326f230507 to your computer and use it in GitHub Desktop.
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