@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.
@@ -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":"AA0BA,KAAK,kBAAkB,GAAG;IACxB,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAiDF,eAAO,MAAM,aAAa;yCAtCgB,kBAAkB;sBA6BnC,MAAM;CAY9B,CAAC"}
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 = new Date();
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.getTime();
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,7 @@
1
+ export declare const TaskSchedulerStore: {
2
+ get: (key: string) => number | undefined;
3
+ add: (key: string, firingDate: number) => void;
4
+ remove: (key: string) => void;
5
+ has: (key: string) => boolean;
6
+ };
7
+ //# sourceMappingURL=TaskScheduler.store.d.ts.map
@@ -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
@@ -60,6 +60,6 @@
60
60
  "test:coverage": "jest --coverage",
61
61
  "watch": "tsc --watch"
62
62
  },
63
- "version": "40.3.1",
64
- "gitHead": "836a3d8c59158817afaae9b3672f33ee35dd0217"
63
+ "version": "40.4.0",
64
+ "gitHead": "a12b89048ce0df0671320be7dccd0db011722fb8"
65
65
  }