Skip to content

Show it a video you love. Get a new video in the same style. An AI crew (Claude Code or Codex) plans, builds and reviews it with you.

Reload the project when the event stream reconnects

heojunfo0d0cbf1921a7
diff --git a/app/server/index.ts b/app/server/index.ts
index 80a6e70..8503385 100644
--- a/app/server/index.ts
+++ b/app/server/index.ts
@@ -108,6 +108,7 @@ app.post('/api/projects/:id/cancel', (req, res) => { if (guard(res, req.params.i
app.get('/api/projects/:id/events', (req, res) => {
const { id } = req.params; if (!guard(res, id)) return;
res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' });
+ res.flushHeaders(); // send them now, not with the first event or ping: the browser's 'open' (the UI reloads on it) waits for them
const send = (jid: string, ev: J.BusEvent) => { if (jid === id) res.write(`data: ${JSON.stringify(ev.type === 'job' ? { type: 'job', stage: ev.job.stage } : ev)}\n\n`); };
J.bus.on('job', send);
const ping = setInterval(() => res.write(': ping\n\n'), 20000);
diff --git a/app/web/src/Project.tsx b/app/web/src/Project.tsx
index f6289e6..508c663 100644
--- a/app/web/src/Project.tsx
+++ b/app/web/src/Project.tsx
@@ -34,7 +34,9 @@ export function Project({ id }: { id: string }) {
let t: ReturnType<typeof setTimeout> | undefined, alive = true;
const load = () => api.project(id).then((d) => { if (alive) { setS(d as SnapshotView); setV((x) => x + 1); } }).catch(() => {});
load();
- const off = api.events(id, () => { clearTimeout(t); t = setTimeout(load, 350); });
+ // also reload when the stream (re)opens: whatever changed while it was down (a restart marking the job interrupted) arrives no other way
+ const soon = () => { clearTimeout(t); t = setTimeout(load, 350); };
+ const off = api.events(id, soon, soon);
return () => { alive = false; off(); clearTimeout(t); };
}, [id]);
useEffect(() => { const k = (e: KeyboardEvent) => e.key === 'Escape' && setZoom(null); addEventListener('keydown', k); return () => removeEventListener('keydown', k); }, []);
diff --git a/app/web/src/api.ts b/app/web/src/api.ts
index ad756ab..adff40f 100644
--- a/app/web/src/api.ts
+++ b/app/web/src/api.ts
@@ -20,7 +20,8 @@ export const api = {
accept: (id: string) => fetch(`/api/projects/${id}/accept`, { method: 'POST' }).then(j<Ok>),
resume: (id: string) => fetch(`/api/projects/${id}/resume`, { method: 'POST' }).then(j<Ok>),
cancel: (id: string) => fetch(`/api/projects/${id}/cancel`, { method: 'POST' }).then(j<Ok>),
- events: (id: string, fn: (ev: ServerEvent) => void) => { const es = new EventSource(`/api/projects/${id}/events`); es.onmessage = (m: MessageEvent<string>) => fn(JSON.parse(m.data) as ServerEvent); return () => es.close(); },
+ // onOpen also runs when the browser reconnects on its own (server restart, laptop sleep): events from the gap are not resent
+ events: (id: string, fn: (ev: ServerEvent) => void, onOpen?: () => void) => { const es = new EventSource(`/api/projects/${id}/events`); es.onmessage = (m: MessageEvent<string>) => fn(JSON.parse(m.data) as ServerEvent); if (onOpen) es.onopen = onOpen; return () => es.close(); },
// encode each path segment ('#' would start the fragment, '%' makes a malformed URI); agents on Windows may write '\' as separator
file: (id: string, p: string, bust?: number) => `/files/${id}/${p.split(/[\\/]/).map(encodeURIComponent).join('/')}${bust ? `?v=${bust}` : ''}`,
};