Add runtime SSE effect stream support

This commit is contained in:
slhx agent
2026-05-10 21:05:14 +02:00
parent c4e02db5f8
commit b53a7ba449
+21
View File
@@ -8,6 +8,7 @@
const pending = new WeakMap(); const pending = new WeakMap();
const queues = new WeakMap(); const queues = new WeakMap();
const timers = new WeakMap(); const timers = new WeakMap();
const sseSources = new WeakMap();
const atomStores = new WeakMap(); const atomStores = new WeakMap();
function roots() { function roots() {
@@ -402,6 +403,25 @@
}); });
} }
function bindSse(root) {
const url = root.getAttribute("data-slhx-sse");
if (!url || sseSources.has(root) || typeof EventSource === "undefined") return;
const source = new EventSource(new URL(url, location.href).href);
source.addEventListener("slhx", (event) => applySseMessage(root, event));
source.addEventListener("message", (event) => applySseMessage(root, event));
source.addEventListener("error", () => emit(root, "slhx:sse-error", url));
sseSources.set(root, source);
}
function applySseMessage(root, event) {
try {
const bytes = base64UrlBytes(event.data);
applyBatch(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength), root);
} catch (error) {
emit(root, "slhx:error", String(error));
}
}
function duration(value) { function duration(value) {
if (!value) return 0; if (!value) return 0;
const match = String(value).trim().match(/^(\d+)(ms|s)?$/); const match = String(value).trim().match(/^(\d+)(ms|s)?$/);
@@ -523,6 +543,7 @@
roots().forEach((root) => { roots().forEach((root) => {
bootstrapState(root); bootstrapState(root);
bindRoot(root); bindRoot(root);
bindSse(root);
}); });
history.replaceState(history.state || { slhx: true }, "", location.href); history.replaceState(history.state || { slhx: true }, "", location.href);
} }