diff options
author | Sebastian <sebasjm@gmail.com> | 2022-04-27 19:05:42 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-04-27 19:05:42 -0300 |
commit | 451c9884dcd20e7e886ed32e5ef93913d29e36ff (patch) | |
tree | 5a0955dea1b32263d7545805a48c69bc30136178 | |
parent | 451dd746daeb57cfe0a601d1bf1f2b5506a5fc3c (diff) |
use settimeout and setinterval in serviceWorker
-rw-r--r-- | packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts | 58 |
1 files changed, 20 insertions, 38 deletions
diff --git a/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts b/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts index e88da680c..15614ed18 100644 --- a/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts +++ b/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts @@ -18,71 +18,53 @@ * Imports. */ import { Logger } from "@gnu-taler/taler-util"; -import { TimerAPI, TimerHandle } from "@gnu-taler/taler-wallet-core"; +import { timer, TimerAPI, TimerHandle } from "@gnu-taler/taler-wallet-core"; -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 { + every(delayMs: number, callback: VoidFunction): TimerHandle { const seconds = delayMs / 1000; const periodInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60); - logger.trace(`creating a alarm every ${periodInMinutes} ${delayMs}`) - chrome.alarms.create("wallet-worker", { periodInMinutes }) - chrome.alarms.onAlarm.addListener((a) => { - logger.trace(`alarm called, every: ${a.name}`) - callback() - }) + logger.info(`creating a alarm every ${periodInMinutes} min, intended delay was ${delayMs}`) + // chrome.alarms.create("wallet-worker", { periodInMinutes }) + // chrome.alarms.onAlarm.addListener((a) => { + // logger.info(`alarm called, every: ${a.name}`) + // callback() + // }) - return new AlarmHandle(); + const p = timer.every(delayMs, callback) + return p; } /** * Call a function after the delay given in milliseconds passes. */ - after(delayMs: number, callback: () => void): TimerHandle { + after(delayMs: number, callback: VoidFunction): TimerHandle { const seconds = delayMs / 1000; const delayInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60); - logger.trace(`creating a alarm after ${delayInMinutes} ${delayMs}`) + logger.info(`creating a alarm after ${delayInMinutes} min, intended delay was ${delayMs}`) chrome.alarms.create("wallet-worker", { delayInMinutes }) - chrome.alarms.onAlarm.addListener((a) => { - logger.trace(`alarm called, after: ${a.name}`) - callback(); - }) - return new AlarmHandle(); - } + // chrome.alarms.onAlarm.addListener((a) => { + // logger.info(`alarm called, after: ${a.name}`) + // callback(); + // }) -} -class AlarmHandle implements TimerHandle { + const p = timer.after(delayMs, callback); - clear(): void { - chrome.alarms.clear("wallet-worker", (result) => { - logger.info(`Alarm 'wallet-worker' was cleared: ${result}`) - }) - return; - } - unref(): void { - return; + return p } } |