const api = "https://api.vane.xyz/api";
const uuid = "7f9c2e4a-1b3d-4c5e-8f6a-2d9b0c1e3f5a";
const terminal = new Set(["COMPLETED", "ERROR", "TIMED_OUT"]);
const get = () => fetch(`${api}/v1/orders/${uuid}`).then((r) => r.json());
let order = await get(); // state first; the stream won't repeat it
render(order);
if (!terminal.has(order.status)) {
const stream = new EventSource(`${api}/orders/${uuid}/status-stream`); // no /v1
const sync = async () => {
order = await get();
render(order);
if (terminal.has(order.status)) stream.close();
};
stream.onopen = sync; // runs on first connect and after every reconnect
stream.addEventListener("orders:status-update", sync); // named event, so onmessage never fires
}