summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/@modules/task/index.ts
blob: 02483e990849b8d5cdbb2181a458001de62759cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { keys } from "lodash";
import {
  siteAddProgress,
  siteRemoveProgress,
  siteUpdateNotifier,
  siteUpdateProgressCount,
} from "../../@redux/actions";
import store from "../../@redux/store";

// A background task manager, use for dispatching task one by one
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<T extends Task.Callable>(groupName: string, tasks: Task.Task<T>[]) {
    if (groupName in this.groups) {
      this.groups[groupName].push(...tasks);
      store.dispatch(
        siteUpdateProgressCount({
          id: groupName,
          count: this.groups[groupName].length,
        })
      );
      return;
    }

    this.groups[groupName] = tasks;
    setTimeout(async () => {
      for (let index = 0; index < tasks.length; index++) {
        const task = tasks[index];

        store.dispatch(
          siteAddProgress([
            {
              id: groupName,
              header: groupName,
              name: task.name,
              value: index,
              count: tasks.length,
            },
          ])
        );
        try {
          await task.callable(...task.parameters);
        } catch (error) {
          // TODO
        }
      }
      delete this.groups[groupName];
      store.dispatch(siteRemoveProgress([groupName]));
    });
  }

  find(groupName: string, id: number) {
    if (groupName in this.groups) {
      return this.groups[groupName].find((v) => v.id === id) !== undefined;
    }
    return false;
  }

  has(groupName: string) {
    return groupName in this.groups;
  }

  hasId(ids: number[]) {
    for (const id of ids) {
      for (const key in this.groups) {
        const tasks = this.groups[key];
        if (tasks.find((v) => v.id === id) !== undefined) {
          return true;
        }
      }
    }
    return false;
  }

  isRunning() {
    return keys(this.groups).length > 0;
  }
}

const BGT = new BackgroundTask();

export default BGT;

export function dispatchTask<T extends Task.Callable>(
  groupName: string,
  tasks: Task.Task<T>[],
  comment?: string
) {
  BGT.dispatch(groupName, tasks);

  if (comment) {
    store.dispatch(siteUpdateNotifier(comment));
  }
}