aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-04-11 15:11:44 -0300
committerSebastian <sebasjm@gmail.com>2022-04-11 15:11:44 -0300
commitc3c0f3bfbb700f617c4fdfa0926c4ce5289c4449 (patch)
tree3bad9f708a466116e89a194ed99bfd8fb3b2c5df /packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts
parenta644efe1abe8072bc92efa6d0fefde7504563086 (diff)
using alarm service intead of timeout api when the wallet is running in a service worker environment
Diffstat (limited to 'packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts')
-rw-r--r--packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts b/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts
new file mode 100644
index 000000000..f2b6ee7a2
--- /dev/null
+++ b/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts
@@ -0,0 +1,80 @@
+/*
+ This file is part of GNU Taler
+ (C) 2020 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+/**
+ * Imports.
+ */
+import { Duration, Logger } from "@gnu-taler/taler-util";
+import { TimerAPI, TimerGroup, TimerHandle } from "@gnu-taler/taler-wallet-core/src/util/timer";
+
+
+const nullTimerHandle = {
+ clear() {
+ // do nothing
+ return;
+ },
+ unref() {
+ // do nothing
+ return;
+ },
+};
+
+const logger = new Logger("ServiceWorkerTimerGroup.ts");
+/**
+ * Implementation of [[TimerAPI]] using alarm API
+ */
+export class ServiceWorkerTimerAPI implements TimerAPI {
+
+ /**
+ * Call a function every time the delay given in milliseconds passes.
+ */
+ every(delayMs: number, callback: () => void): TimerHandle {
+ const seconds = delayMs / 1000;
+ const periodInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60);
+
+ chrome.alarms.create("wallet-worker", { periodInMinutes })
+ chrome.alarms.onAlarm.addListener(callback)
+
+ return new AlarmHandle();
+ }
+
+ /**
+ * Call a function after the delay given in milliseconds passes.
+ */
+ after(delayMs: number, callback: () => void): TimerHandle {
+ const seconds = delayMs / 1000;
+ const delayInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60);
+
+ chrome.alarms.create("wallet-worker", { delayInMinutes })
+ chrome.alarms.onAlarm.addListener(callback)
+ return new AlarmHandle();
+ }
+
+}
+
+class AlarmHandle implements TimerHandle {
+
+ clear(): void {
+ chrome.alarms.clear("wallet-worker", (result) => {
+ logger.info(`Alarm 'wallet-worker' was cleared: ${result}`)
+ })
+ return;
+ }
+ unref(): void {
+ return;
+ }
+
+}