Move all code to old/ to prepare for Nuxt rewrite
This commit is contained in:
parent
6007f4caeb
commit
51ff27c569
33 changed files with 0 additions and 1 deletions
50
old/app/api/events/streams.ts
Normal file
50
old/app/api/events/streams.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { Schedule } from "@/app/schedule/types";
|
||||
|
||||
function sendMessage(
|
||||
stream: WritableStream<string>,
|
||||
message: string,
|
||||
) {
|
||||
const writer = stream.getWriter();
|
||||
writer.ready
|
||||
.then(() => writer.write(message))
|
||||
.catch(console.error)
|
||||
.finally(() => writer.releaseLock())
|
||||
;
|
||||
}
|
||||
|
||||
declare global {
|
||||
var streams: Set<WritableStream<string>>;
|
||||
}
|
||||
global.streams = global.streams ?? new Set<WritableStream<string>>();
|
||||
|
||||
let keepaliveInterval: ReturnType<typeof setInterval> | null = null
|
||||
export function addStream(stream: WritableStream<string>) {
|
||||
if (streams.size === 0) {
|
||||
console.log("Starting keepalive")
|
||||
keepaliveInterval = setInterval(sendKeepalive, 4000)
|
||||
}
|
||||
streams.add(stream);
|
||||
}
|
||||
export function deleteStream(stream: WritableStream<string>) {
|
||||
streams.delete(stream);
|
||||
if (streams.size === 0) {
|
||||
console.log("Ending keepalive")
|
||||
clearInterval(keepaliveInterval!);
|
||||
}
|
||||
}
|
||||
|
||||
export function broadcastUpdate(schedule: Schedule) {
|
||||
const id = Date.now();
|
||||
const data = JSON.stringify(schedule);
|
||||
const message = `id: ${id}\nevent: update\ndata: ${data}\n\n`
|
||||
console.log(`broadcasting update from ${process.pid} to ${streams.size} clients`);
|
||||
for (const stream of streams) {
|
||||
sendMessage(stream, message);
|
||||
}
|
||||
}
|
||||
|
||||
function sendKeepalive() {
|
||||
for (const stream of streams) {
|
||||
sendMessage(stream, "data: keepalive\n\n");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue