diff options
8 files changed, 61 insertions, 78 deletions
diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index 96722aefb..579710d4e 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -327,6 +327,27 @@ export interface RetryLoopOpts { } /** + * This iteration hint will keep incrementing while the taskLoop iteration. + * If the hint does not match the current iteration it means that another + * promises is also working or has done some work before. + */ +let iterationHint = 0 +function thereIsAnotherPromiseWorking(iteration: number): boolean { + if (iterationHint > iteration) { + logger.trace(`some other promise is or has done some progress`); + iterationHint = iteration; + //we know that another promise did some work but don't know if still active + //so we take ownership and do work + } else if (iterationHint < iteration) { + //another promise take ownership that means that our time has come to an end + return true + } + // increment the hint to match the next loop + iterationHint++ + return false +} + +/** * Main retry loop of the wallet. * * Looks up pending operations from the wallet, runs them, repeat. @@ -336,6 +357,10 @@ async function runTaskLoop( opts: RetryLoopOpts = {}, ): Promise<void> { for (let iteration = 0; !ws.stopped; iteration++) { + if (thereIsAnotherPromiseWorking(iteration)) { + logger.trace(`another promise is working, we just need one`); + return; + } const pending = await getPendingOperations(ws); logger.trace(`pending operations: ${j2s(pending)}`); let numGivingLiveness = 0; diff --git a/packages/taler-wallet-webextension/manifest-v2.json b/packages/taler-wallet-webextension/manifest-v2.json index 9ea4ff031..596add176 100644 --- a/packages/taler-wallet-webextension/manifest-v2.json +++ b/packages/taler-wallet-webextension/manifest-v2.json @@ -26,7 +26,9 @@ }, "commands": { "_execute_browser_action": { - "suggested_key": "Alt+W" + "suggested_key": { + "default": "Alt+W" + } } }, "permissions": [ diff --git a/packages/taler-wallet-webextension/manifest-v3.json b/packages/taler-wallet-webextension/manifest-v3.json index 5e94466e0..abd07d30e 100644 --- a/packages/taler-wallet-webextension/manifest-v3.json +++ b/packages/taler-wallet-webextension/manifest-v3.json @@ -25,7 +25,9 @@ ], "commands": { "_execute_action": { - "suggested_key": "Alt+W" + "suggested_key": { + "default": "Alt+W" + } } }, "optional_permissions": [ diff --git a/packages/taler-wallet-webextension/src/platform/api.ts b/packages/taler-wallet-webextension/src/platform/api.ts index cee628a6f..4713d5d00 100644 --- a/packages/taler-wallet-webextension/src/platform/api.ts +++ b/packages/taler-wallet-webextension/src/platform/api.ts @@ -57,6 +57,10 @@ export interface WalletVersion { */ export interface PlatformAPI { /** + * Garantee that the + */ + keepAlive(cb: VoidFunction): void; + /** * FIXME: should not be needed * * check if the platform is firefox diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index d295168bf..900470dff 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -15,7 +15,7 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { classifyTalerUri, CoreApiResponse, TalerUriType } from "@gnu-taler/taler-util"; +import { classifyTalerUri, CoreApiResponse, Logger, TalerUriType } from "@gnu-taler/taler-util"; import { CrossBrowserPermissionsApi, MessageFromBackend, Permissions, PlatformAPI } from "./api.js"; const api: PlatformAPI = { @@ -38,10 +38,27 @@ const api: PlatformAPI = { sendMessageToWalletBackground, useServiceWorkerAsBackgroundProcess, containsTalerHeaderListener, + keepAlive, } export default api; +const logger = new Logger("chrome.ts"); + +function keepAlive(callback: any): void { + if (chrome.runtime && chrome.runtime.getManifest().manifest_version === 3) { + chrome.alarms.create("wallet-worker", { periodInMinutes: 1 }) + + chrome.alarms.onAlarm.addListener((a) => { + logger.trace(`kee p alive alarm: ${a.name}`) + callback() + }) + } else { + callback(); + } + +} + function isFirefox(): boolean { return false; } diff --git a/packages/taler-wallet-webextension/src/platform/dev.ts b/packages/taler-wallet-webextension/src/platform/dev.ts index e05ff29e9..59e6738c5 100644 --- a/packages/taler-wallet-webextension/src/platform/dev.ts +++ b/packages/taler-wallet-webextension/src/platform/dev.ts @@ -21,6 +21,7 @@ const frames = ["popup", "wallet"] const api: PlatformAPI = ({ isFirefox: () => false, + keepAlive: (cb: VoidFunction) => cb(), findTalerUriInActiveTab: async () => undefined, containsTalerHeaderListener: () => { return true }, getPermissionsApi: () => ({ diff --git a/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts b/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts deleted file mode 100644 index 15614ed18..000000000 --- a/packages/taler-wallet-webextension/src/serviceWorkerTimerAPI.ts +++ /dev/null @@ -1,70 +0,0 @@ -/* - 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 { Logger } from "@gnu-taler/taler-util"; -import { timer, TimerAPI, TimerHandle } from "@gnu-taler/taler-wallet-core"; - - -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: VoidFunction): TimerHandle { - const seconds = delayMs / 1000; - const periodInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60); - - 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() - // }) - - const p = timer.every(delayMs, callback) - return p; - } - - /** - * Call a function after the delay given in milliseconds passes. - */ - after(delayMs: number, callback: VoidFunction): TimerHandle { - const seconds = delayMs / 1000; - const delayInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60); - - logger.info(`creating a alarm after ${delayInMinutes} min, intended delay was ${delayMs}`) - chrome.alarms.create("wallet-worker", { delayInMinutes }) - // chrome.alarms.onAlarm.addListener((a) => { - // logger.info(`alarm called, after: ${a.name}`) - // callback(); - // }) - - - const p = timer.after(delayMs, callback); - - return p - } - -} diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index b0d690ef8..2ac8f6343 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -46,7 +46,6 @@ import { BrowserHttpLib } from "./browserHttpLib.js"; import { MessageFromBackend, platform } from "./platform/api.js"; import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js"; import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js"; -import { ServiceWorkerTimerAPI } from "./serviceWorkerTimerAPI.js"; /** * Currently active wallet instance. Might be unloaded and @@ -196,7 +195,7 @@ async function reinitWallet(): Promise<void> { if (platform.useServiceWorkerAsBackgroundProcess()) { httpLib = new ServiceWorkerHttpLib(); cryptoWorker = new SynchronousCryptoWorkerFactory(); - timer = new ServiceWorkerTimerAPI(); + timer = new SetTimeoutTimerAPI(); } else { httpLib = new BrowserHttpLib(); cryptoWorker = new BrowserCryptoWorkerFactory(); @@ -216,9 +215,12 @@ async function reinitWallet(): Promise<void> { const message: MessageFromBackend = { type: x.type }; platform.sendMessageToAllChannels(message) }); - wallet.runTaskLoop().catch((e) => { - logger.error("error during wallet task loop", e); - }); + + platform.keepAlive(() => { + wallet.runTaskLoop().catch((e) => { + logger.error("error during wallet task loop", e); + }); + }) // Useful for debugging in the background page. if (typeof window !== "undefined") { (window as any).talerWallet = wallet; |