This commit is contained in:
2023-06-08 22:47:46 +08:00
parent 2002dd4749
commit 7b5ba18d89
11 changed files with 193 additions and 34 deletions

View File

@@ -1,15 +1,10 @@
import { Handlers } from "$fresh/server.ts";
import { get_task_manager } from "../../server.ts";
import { Task, TaskProgress } from "../../task.ts";
import { DiscriminatedUnion } from "../../utils.ts";
type EventMap = {
close: Record<PropertyKey, never>;
new_download_task: { gid: number; token: string };
new_export_zip_task: { gid: number; output?: string };
};
type EventData = DiscriminatedUnion<"type", EventMap>;
import {
TaskClientSocketData,
TaskServerSocketData,
} from "../../server/task.ts";
export const handler: Handlers<Task[]> = {
GET(req, _ctx) {
@@ -24,6 +19,9 @@ export const handler: Handlers<Task[]> = {
t.removeEventListener("task_finished", handle);
t.removeEventListener("task_progress", handle);
};
function sendMessage(mes: TaskServerSocketData) {
socket.send(JSON.stringify(mes));
}
socket.onclose = () => {
removeListener();
};
@@ -33,13 +31,22 @@ export const handler: Handlers<Task[]> = {
};
socket.onmessage = (e) => {
try {
const d: EventData = JSON.parse(e.data);
const d: TaskClientSocketData = JSON.parse(e.data);
if (d.type == "close") {
sendMessage({ type: "close" });
socket.close();
} else if (d.type == "new_download_task") {
t.add_download_task(d.gid, d.token);
} else if (d.type == "new_export_zip_task") {
t.add_export_zip_task(d.gid, d.output);
} else if (d.type == "task_list") {
t.get_task_list().then((tasks) => {
sendMessage({
type: "tasks",
tasks,
running: t.get_running_task(),
});
});
}
} catch (_) {
null;