My fixing attempt #1
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
settings,
|
||||
submissions,
|
||||
} from "./db/schema";
|
||||
import { sse } from "./utils";
|
||||
|
||||
export async function getCharacters(chars: string[]) {
|
||||
return await db
|
||||
@@ -63,6 +64,7 @@ export async function submitArtifact(formData: FormData) {
|
||||
})
|
||||
.returning({ queue: submissions.queue, id: submissions.id });
|
||||
revalidatePath("/artifact/admin");
|
||||
sse.publish({}, { topic: "artifact-ev", event: "update" });
|
||||
return queue;
|
||||
}
|
||||
|
||||
@@ -113,6 +115,9 @@ export async function setLimit(limit: number) {
|
||||
export async function wipe() {
|
||||
if (!(await apiAuthCheck())) throw "Unauthorized";
|
||||
await db.delete(submissions);
|
||||
await db.execute(
|
||||
sql`ALTER SEQUENCE artifact.submissions_queue_seq RESTART WITH 1`,
|
||||
);
|
||||
revalidatePath("/artifact/admin");
|
||||
redirect("/artifact/admin");
|
||||
}
|
||||
|
||||
+92
-3
@@ -1,6 +1,95 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { type ClassValue, clsx } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
type ServerEventSource = {
|
||||
send: (data: object, event?: string) => void;
|
||||
write: (msg: string) => void;
|
||||
close: () => void;
|
||||
topic: string;
|
||||
id: number;
|
||||
};
|
||||
|
||||
declare global {
|
||||
var sseList: ServerEventSource[];
|
||||
}
|
||||
|
||||
if (!globalThis.sseList) {
|
||||
globalThis.sseList = [];
|
||||
}
|
||||
|
||||
export class EventSourceManager {
|
||||
private id = 0;
|
||||
new(topic = "_global"): Response {
|
||||
let timeout: NodeJS.Timeout | undefined;
|
||||
this.id++;
|
||||
if (this.id > 100000) this.id = 0;
|
||||
const id = this.id;
|
||||
|
||||
const push = (s: ServerEventSource) => {
|
||||
globalThis.sseList.push(s);
|
||||
};
|
||||
const remove = (id: number) => {
|
||||
globalThis.sseList = globalThis.sseList.toSpliced(
|
||||
globalThis.sseList.findIndex((e) => e.id === id),
|
||||
1,
|
||||
);
|
||||
};
|
||||
return new Response(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
const encoder = new TextEncoder();
|
||||
// Register this client
|
||||
const res = {
|
||||
send: (data: object, event?: string) =>
|
||||
res.write(
|
||||
`${event ? `event: ${event}\n` : ""}data: ${JSON.stringify(data)}\n\n`,
|
||||
),
|
||||
write: (msg: string) => controller.enqueue(encoder.encode(msg)),
|
||||
close: () => {
|
||||
try {
|
||||
controller.close();
|
||||
} catch {}
|
||||
clearTimeout(timeout);
|
||||
remove(res.id);
|
||||
},
|
||||
topic,
|
||||
id,
|
||||
};
|
||||
timeout = setInterval(() => {
|
||||
try {
|
||||
res.write(`:ping\n\n`);
|
||||
} catch {
|
||||
res.close();
|
||||
}
|
||||
}, 5000);
|
||||
push(res);
|
||||
},
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "text/event-stream",
|
||||
"Cache-Control": "no-cache",
|
||||
Connection: "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
publish(
|
||||
data: object,
|
||||
{ event, topic = "_global" }: { event?: string; topic?: string },
|
||||
) {
|
||||
for (const s of globalThis.sseList) {
|
||||
if (s.topic !== topic) continue;
|
||||
try {
|
||||
s.send(data, event);
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const sse = new EventSourceManager();
|
||||
Reference in New Issue
Block a user