From 81507b9e750228de6d52cb3f7cbd631ecfac3713 Mon Sep 17 00:00:00 2001 From: LASER-Yi Date: Tue, 24 Aug 2021 09:31:47 +0800 Subject: [PATCH] Add a dialog before closing the page to inform user when there're still background tasks running --- frontend/src/@modules/task/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/frontend/src/@modules/task/index.ts b/frontend/src/@modules/task/index.ts index aee555b0d..02483e990 100644 --- a/frontend/src/@modules/task/index.ts +++ b/frontend/src/@modules/task/index.ts @@ -12,6 +12,17 @@ class BackgroundTask { private groups: Task.Group; constructor() { this.groups = {}; + window.addEventListener("beforeunload", this.onBeforeUnload.bind(this)); + } + + private onBeforeUnload(e: BeforeUnloadEvent) { + const message = "Background tasks are still running"; + if (Object.keys(this.groups).length !== 0) { + e.preventDefault(); + e.returnValue = message; + return; + } + delete e["returnValue"]; } dispatch(groupName: string, tasks: Task.Task[]) {