From c1a26fd9ebd601417fbfe975bf148485384c6475 Mon Sep 17 00:00:00 2001 From: LASER-Yi Date: Sat, 19 Mar 2022 11:54:24 +0800 Subject: Fix issues when trying to create background tasks --- frontend/src/modules/task/index.ts | 22 +++++++++++++++------- frontend/src/modules/task/task.d.ts | 6 +++--- frontend/src/modules/task/utilities.ts | 14 +++++++++++--- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/frontend/src/modules/task/index.ts b/frontend/src/modules/task/index.ts index 6ca47341f..8580ca05c 100644 --- a/frontend/src/modules/task/index.ts +++ b/frontend/src/modules/task/index.ts @@ -9,7 +9,7 @@ class TaskManager { private onBeforeUnload(e: BeforeUnloadEvent) { const message = "Background tasks are still running"; - if (this.tasks.some((t) => t.status !== "success")) { + if (this.tasks.some((t) => t.status === "running")) { e.preventDefault(); e.returnValue = message; return; @@ -17,22 +17,29 @@ class TaskManager { delete e["returnValue"]; } - private generateUniqueId(): string { - return "TODO-TODO"; + private findTask(ref: Task.TaskRef): Task.Callable | undefined { + return this.tasks.find((t) => t.id === ref.callableId); } - create(fn: T, parameters: Parameters): string { - const newTask = fn as Task.Callable; + create(fn: T, parameters: Parameters): symbol { + // Clone this function + const newTask = fn.bind({}) as Task.Callable; newTask.status = "idle"; newTask.parameters = parameters; - newTask.id = this.generateUniqueId(); + newTask.id = Symbol(this.tasks.length); this.tasks.push(newTask); return newTask.id; } - async run(task: Task.Callable) { + async run(taskRef: Task.TaskRef) { + const task = this.findTask(taskRef); + + if (task === undefined) { + throw new Error(`Task ${taskRef.name} not found`); + } + if (task.status !== "idle") { return; } @@ -44,6 +51,7 @@ class TaskManager { task.status = "success"; } catch (err) { task.status = "failure"; + throw err; } } } diff --git a/frontend/src/modules/task/task.d.ts b/frontend/src/modules/task/task.d.ts index ae8f785e2..4c7e2add1 100644 --- a/frontend/src/modules/task/task.d.ts +++ b/frontend/src/modules/task/task.d.ts @@ -5,13 +5,13 @@ declare namespace Task { type AnyCallable = (...args: any[]) => Promise; export type Callable = T & { parameters: Parameters; - id: string; + id: symbol; status: Status; }; - export interface Task { + export interface TaskRef { name: string; - callableId: string; + callableId: symbol; description?: string; } diff --git a/frontend/src/modules/task/utilities.ts b/frontend/src/modules/task/utilities.ts index 9f8b8d1e5..8acb95a74 100644 --- a/frontend/src/modules/task/utilities.ts +++ b/frontend/src/modules/task/utilities.ts @@ -1,3 +1,4 @@ +import { LOG } from "@/utilities/console"; import taskManager from "."; import { GroupName } from "./group"; @@ -5,17 +6,24 @@ export function createTask( name: string, callable: T, ...parameters: Parameters -): Task.Task { +): Task.TaskRef { const callableId = taskManager.create(callable, parameters); + LOG("info", "task created", name); + return { name, callableId, }; } -export function dispatchTask(task: Task.Task[], group: GroupName) { - // TODO +export function dispatchTask(tasks: Task.TaskRef[], group: GroupName) { + setTimeout(async () => { + for (const ref of tasks) { + LOG("info", "dispatching task", ref.name); + await taskManager.run(ref); + } + }); } export function createAndDispatchTask( -- cgit v1.2.3