using alarm service intead of timeout api when the wallet is running in a service worker environment
This commit is contained in:
parent
a644efe1ab
commit
c3c0f3bfbb
@ -99,6 +99,9 @@ export function useComponentState(
|
|||||||
undefined,
|
undefined,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ask the wallet about the withdraw URI
|
||||||
|
*/
|
||||||
const uriInfoHook = useAsyncAsHook(async () => {
|
const uriInfoHook = useAsyncAsHook(async () => {
|
||||||
if (!talerWithdrawUri) throw Error("ERROR_NO-URI-FOR-WITHDRAWAL");
|
if (!talerWithdrawUri) throw Error("ERROR_NO-URI-FOR-WITHDRAWAL");
|
||||||
|
|
||||||
@ -110,6 +113,9 @@ export function useComponentState(
|
|||||||
return { uriInfo, knownExchanges };
|
return { uriInfo, knownExchanges };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the amount and select one exchange
|
||||||
|
*/
|
||||||
const exchangeAndAmount = useAsyncAsHook(
|
const exchangeAndAmount = useAsyncAsHook(
|
||||||
async () => {
|
async () => {
|
||||||
if (!uriInfoHook || uriInfoHook.hasError || !uriInfoHook.response) return;
|
if (!uriInfoHook || uriInfoHook.hasError || !uriInfoHook.response) return;
|
||||||
@ -136,6 +142,9 @@ export function useComponentState(
|
|||||||
[!uriInfoHook || uriInfoHook.hasError ? undefined : uriInfoHook],
|
[!uriInfoHook || uriInfoHook.hasError ? undefined : uriInfoHook],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For the exchange selected, bring the status of the terms of service
|
||||||
|
*/
|
||||||
const terms = useAsyncAsHook(
|
const terms = useAsyncAsHook(
|
||||||
async () => {
|
async () => {
|
||||||
if (
|
if (
|
||||||
@ -159,6 +168,10 @@ export function useComponentState(
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* With the exchange and amount, ask the wallet the information
|
||||||
|
* about the withdrawal
|
||||||
|
*/
|
||||||
const info = useAsyncAsHook(
|
const info = useAsyncAsHook(
|
||||||
async () => {
|
async () => {
|
||||||
if (
|
if (
|
||||||
@ -466,7 +479,6 @@ export function WithdrawPage({ talerWithdrawUri }: Props): VNode {
|
|||||||
return <Loading />;
|
return <Loading />;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(state);
|
|
||||||
if (state.status === "loading-uri") {
|
if (state.status === "loading-uri") {
|
||||||
if (!state.hook) return <Loading />;
|
if (!state.hook) return <Loading />;
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -40,12 +40,14 @@ import {
|
|||||||
Wallet,
|
Wallet,
|
||||||
WalletStoresV1
|
WalletStoresV1
|
||||||
} from "@gnu-taler/taler-wallet-core";
|
} from "@gnu-taler/taler-wallet-core";
|
||||||
|
import { SetTimeoutTimerAPI, TimerGroup } from "@gnu-taler/taler-wallet-core/src/util/timer";
|
||||||
import { BrowserCryptoWorkerFactory } from "./browserCryptoWorkerFactory.js";
|
import { BrowserCryptoWorkerFactory } from "./browserCryptoWorkerFactory.js";
|
||||||
import { BrowserHttpLib } from "./browserHttpLib.js";
|
import { BrowserHttpLib } from "./browserHttpLib.js";
|
||||||
import { getReadRequestPermissions } from "./permissions.js";
|
import { getReadRequestPermissions } from "./permissions.js";
|
||||||
import { MessageFromBackend, platform } from "./platform/api.js";
|
import { MessageFromBackend, platform } from "./platform/api.js";
|
||||||
import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js";
|
import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js";
|
||||||
import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js";
|
import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js";
|
||||||
|
import { ServiceWorkerTimerAPI } from "./serviceWorkerTimerAPI.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Currently active wallet instance. Might be unloaded and
|
* Currently active wallet instance. Might be unloaded and
|
||||||
@ -188,17 +190,20 @@ async function reinitWallet(): Promise<void> {
|
|||||||
}
|
}
|
||||||
let httpLib;
|
let httpLib;
|
||||||
let cryptoWorker;
|
let cryptoWorker;
|
||||||
|
let timer;
|
||||||
|
|
||||||
if (platform.useServiceWorkerAsBackgroundProcess()) {
|
if (platform.useServiceWorkerAsBackgroundProcess()) {
|
||||||
httpLib = new ServiceWorkerHttpLib();
|
httpLib = new ServiceWorkerHttpLib();
|
||||||
cryptoWorker = new SynchronousCryptoWorkerFactory();
|
cryptoWorker = new SynchronousCryptoWorkerFactory();
|
||||||
|
timer = new ServiceWorkerTimerAPI();
|
||||||
} else {
|
} else {
|
||||||
httpLib = new BrowserHttpLib();
|
httpLib = new BrowserHttpLib();
|
||||||
cryptoWorker = new BrowserCryptoWorkerFactory();
|
cryptoWorker = new BrowserCryptoWorkerFactory();
|
||||||
|
timer = new SetTimeoutTimerAPI();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("setting wallet");
|
console.log("setting wallet");
|
||||||
const wallet = await Wallet.create(currentDatabase, httpLib, cryptoWorker);
|
const wallet = await Wallet.create(currentDatabase, httpLib, timer, cryptoWorker);
|
||||||
try {
|
try {
|
||||||
await wallet.handleCoreApiRequest("initWallet", "native-init", {});
|
await wallet.handleCoreApiRequest("initWallet", "native-init", {});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -218,7 +223,7 @@ async function reinitWallet(): Promise<void> {
|
|||||||
(window as any).talerWallet = wallet;
|
(window as any).talerWallet = wallet;
|
||||||
}
|
}
|
||||||
currentWallet = wallet;
|
currentWallet = wallet;
|
||||||
walletInit.resolve();
|
return walletInit.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseTalerUriAndRedirect(tabId: number, talerUri: string): void {
|
function parseTalerUriAndRedirect(tabId: number, talerUri: string): void {
|
||||||
|
Loading…
Reference in New Issue
Block a user