@wireapp/core 40.3.1 → 40.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/util/TaskScheduler/TaskScheduler.d.ts +4 -1
- package/lib/util/TaskScheduler/TaskScheduler.d.ts.map +1 -1
- package/lib/util/TaskScheduler/TaskScheduler.js +24 -3
- package/lib/util/TaskScheduler/TaskScheduler.store.d.ts +7 -0
- package/lib/util/TaskScheduler/TaskScheduler.store.d.ts.map +1 -0
- package/lib/util/TaskScheduler/TaskScheduler.store.js +34 -0
- package/package.json +2 -2
|
@@ -2,10 +2,13 @@ type ScheduleTaskParams = {
|
|
|
2
2
|
task: () => void;
|
|
3
3
|
firingDate: number;
|
|
4
4
|
key: string;
|
|
5
|
+
persist?: boolean;
|
|
5
6
|
};
|
|
6
7
|
export declare const TaskScheduler: {
|
|
7
|
-
addTask: ({ task, firingDate, key }: ScheduleTaskParams) => void;
|
|
8
|
+
addTask: ({ task, firingDate, key, persist }: ScheduleTaskParams) => void;
|
|
8
9
|
cancelTask: (key: string) => void;
|
|
10
|
+
continueTask: ({ key, task }: Omit<ScheduleTaskParams, 'firingDate' | 'persist'>) => void;
|
|
11
|
+
hasActiveTask: (key: string) => boolean;
|
|
9
12
|
};
|
|
10
13
|
export {};
|
|
11
14
|
//# sourceMappingURL=TaskScheduler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskScheduler.d.ts","sourceRoot":"","sources":["../../../src/util/TaskScheduler/TaskScheduler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"TaskScheduler.d.ts","sourceRoot":"","sources":["../../../src/util/TaskScheduler/TaskScheduler.ts"],"names":[],"mappings":"AA4BA,KAAK,kBAAkB,GAAG;IACxB,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAoEF,eAAO,MAAM,aAAa;kDAzDiC,kBAAkB;sBAoCpD,MAAM;kCAcI,KAAK,kBAAkB,EAAE,YAAY,GAAG,SAAS,CAAC;;CAYpF,CAAC"}
|
|
@@ -23,6 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.TaskScheduler = void 0;
|
|
25
25
|
const logdown_1 = __importDefault(require("logdown"));
|
|
26
|
+
const TaskScheduler_store_1 = require("./TaskScheduler.store");
|
|
26
27
|
const logger = (0, logdown_1.default)('@wireapp/core/TaskScheduler', {
|
|
27
28
|
logger: console,
|
|
28
29
|
markdown: false,
|
|
@@ -35,20 +36,27 @@ const activeTimeouts = {};
|
|
|
35
36
|
* @param firingDate execution date
|
|
36
37
|
* @param key unique key for the task
|
|
37
38
|
*/
|
|
38
|
-
const addTask = ({ task, firingDate, key }) => {
|
|
39
|
-
const now =
|
|
39
|
+
const addTask = ({ task, firingDate, key, persist = false }) => {
|
|
40
|
+
const now = Date.now();
|
|
40
41
|
const execute = new Date(firingDate);
|
|
41
|
-
const delay = execute.getTime() - now
|
|
42
|
+
const delay = execute.getTime() - now;
|
|
43
|
+
if (TaskScheduler_store_1.TaskSchedulerStore.has(key)) {
|
|
44
|
+
TaskScheduler_store_1.TaskSchedulerStore.remove(key);
|
|
45
|
+
}
|
|
42
46
|
if (activeTimeouts[key]) {
|
|
43
47
|
cancelTask(key);
|
|
44
48
|
}
|
|
45
49
|
const timeout = setTimeout(async () => {
|
|
46
50
|
logger.info(`Executing task with key "${key}"`);
|
|
47
51
|
delete activeTimeouts[key];
|
|
52
|
+
TaskScheduler_store_1.TaskSchedulerStore.remove(key);
|
|
48
53
|
await task();
|
|
49
54
|
}, delay > 0 ? delay : 0);
|
|
50
55
|
// add the task to the list of active tasks
|
|
51
56
|
activeTimeouts[key] = timeout;
|
|
57
|
+
if (persist) {
|
|
58
|
+
TaskScheduler_store_1.TaskSchedulerStore.add(key, firingDate);
|
|
59
|
+
}
|
|
52
60
|
logger.info(`New scheduled task to be executed at "${execute}" with key "${key}"`);
|
|
53
61
|
};
|
|
54
62
|
/**
|
|
@@ -64,7 +72,20 @@ const cancelTask = (key) => {
|
|
|
64
72
|
logger.info(`Scheduled task with key "${key}" prematurely cleared`);
|
|
65
73
|
}
|
|
66
74
|
};
|
|
75
|
+
/**
|
|
76
|
+
* Checks if a task has been scheduled in the past and reschedules it
|
|
77
|
+
* @param task function to be executed
|
|
78
|
+
* @param key unique key for the task
|
|
79
|
+
*/
|
|
80
|
+
const continueTask = ({ key, task }) => {
|
|
81
|
+
const activeTaskEndTime = TaskScheduler_store_1.TaskSchedulerStore.get(key);
|
|
82
|
+
if (activeTaskEndTime) {
|
|
83
|
+
addTask({ task, firingDate: activeTaskEndTime, key, persist: true });
|
|
84
|
+
}
|
|
85
|
+
};
|
|
67
86
|
exports.TaskScheduler = {
|
|
68
87
|
addTask,
|
|
69
88
|
cancelTask,
|
|
89
|
+
continueTask,
|
|
90
|
+
hasActiveTask: TaskScheduler_store_1.TaskSchedulerStore.has,
|
|
70
91
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TaskScheduler.store.d.ts","sourceRoot":"","sources":["../../../src/util/TaskScheduler/TaskScheduler.store.ts"],"names":[],"mappings":"AAqBA,eAAO,MAAM,kBAAkB;eAClB,MAAM;eAON,MAAM,cAAc,MAAM;kBACvB,MAAM;eACT,MAAM;CAClB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Wire
|
|
4
|
+
* Copyright (C) 2023 Wire Swiss GmbH
|
|
5
|
+
*
|
|
6
|
+
* This program is free software: you can redistribute it and/or modify
|
|
7
|
+
* it under the terms of the GNU General Public License as published by
|
|
8
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
* (at your option) any later version.
|
|
10
|
+
*
|
|
11
|
+
* This program is distributed in the hope that it will be useful,
|
|
12
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
* GNU General Public License for more details.
|
|
15
|
+
*
|
|
16
|
+
* You should have received a copy of the GNU General Public License
|
|
17
|
+
* along with this program. If not, see http://www.gnu.org/licenses/.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.TaskSchedulerStore = void 0;
|
|
22
|
+
const prependKey = (key) => `TaskScheduler_${key}`;
|
|
23
|
+
exports.TaskSchedulerStore = {
|
|
24
|
+
get: (key) => {
|
|
25
|
+
const value = localStorage.getItem(prependKey(key));
|
|
26
|
+
if (value) {
|
|
27
|
+
return Number(value);
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
},
|
|
31
|
+
add: (key, firingDate) => localStorage.setItem(prependKey(key), String(firingDate)),
|
|
32
|
+
remove: (key) => localStorage.removeItem(prependKey(key)),
|
|
33
|
+
has: (key) => !!localStorage.getItem(prependKey(key)),
|
|
34
|
+
};
|
package/package.json
CHANGED