Port application from Next.js to Nuxt
Nuxt is based on Vue.js and I find their building blocks to be much neater compared to the React based Next.js.
This commit is contained in:
parent
8c8b561f1a
commit
250ca9a1ac
45 changed files with 662 additions and 1358 deletions
|
@ -1,50 +0,0 @@
|
|||
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