taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 7e5827eb4fbde75e109d68393145151901c7a8ea
parent f78d7dc613946b0316a559aa0161dd02b056b05d
Author: Florian Dold <florian@dold.me>
Date:   Tue, 27 Feb 2024 15:36:33 +0100

-move state into shepherd

Diffstat:
Mpackages/taler-wallet-core/src/exchanges.ts | 2+-
Mpackages/taler-wallet-core/src/pay-merchant.ts | 3+--
Mpackages/taler-wallet-core/src/shepherd.ts | 23+++++++++++++++++++++--
Mpackages/taler-wallet-core/src/testing.ts | 12++++++------
Mpackages/taler-wallet-core/src/wallet.ts | 17-----------------
5 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/packages/taler-wallet-core/src/exchanges.ts b/packages/taler-wallet-core/src/exchanges.ts @@ -1082,7 +1082,7 @@ export async function fetchFreshExchange( ): Promise<ReadyExchangeSummary> { const canonUrl = canonicalizeBaseUrl(baseUrl); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); await startUpdateExchangeEntry(ws, canonUrl, { forceUpdate: options.forceUpdate, diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts @@ -1732,8 +1732,7 @@ async function waitPaymentResult( ): Promise<ConfirmPayResult> { const ctx = new PayMerchantTransactionContext(ws, proposalId); - ws.ensureTaskLoopRunning(); - + ws.taskScheduler.ensureRunning(); ws.taskScheduler.startShepherdTask(ctx.taskId); // FIXME: Clean up using the new JS "using" / Symbol.dispose syntax. diff --git a/packages/taler-wallet-core/src/shepherd.ts b/packages/taler-wallet-core/src/shepherd.ts @@ -141,6 +141,8 @@ export class TaskScheduler { private throttler = new TaskThrottler(); + isRunning: boolean = false; + constructor(private ws: InternalWalletState) {} async loadTasksFromDb(): Promise<void> { @@ -153,9 +155,26 @@ export class TaskScheduler { } } + ensureRunning(): void { + if (this.isRunning) { + return; + } + this.run() + .catch((e) => { + logger.error("error running task loop"); + logger.error(`err: ${e}`); + }) + .then(() => { + logger.info("done running task loop"); + }); + } + async run(opts: RetryLoopOpts = {}): Promise<void> { + if (this.isRunning) { + throw Error("task loop already running"); + } logger.info("Running task loop."); - this.ws.isTaskLoopRunning = true; + this.isRunning = true; await this.loadTasksFromDb(); logger.info("loaded!"); logger.info(`sheps: ${this.sheps.size}`); @@ -182,7 +201,7 @@ export class TaskScheduler { } await this.iterCond.wait(); } - this.ws.isTaskLoopRunning = false; + this.isRunning = false; logger.info("Done with task loop."); } diff --git a/packages/taler-wallet-core/src/testing.ts b/packages/taler-wallet-core/src/testing.ts @@ -402,7 +402,7 @@ export async function waitUntilAllTransactionsFinal( ws: InternalWalletState, ): Promise<void> { logger.info("waiting until all transactions are in a final state"); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise<void> | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -462,7 +462,7 @@ export async function waitUntilGivenTransactionsFinal( if (transactionIds.length === 0) { return; } - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); const txIdSet = new Set(transactionIds); let p: OpenedPromise<void> | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { @@ -522,7 +522,7 @@ export async function waitUntilRefreshesDone( ws: InternalWalletState, ): Promise<void> { logger.info("waiting until all refresh transactions are in a final state"); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise<void> | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -576,7 +576,7 @@ async function waitUntilTransactionPendingReady( transactionId: string, ): Promise<void> { logger.info(`starting waiting for ${transactionId} to be in pending(ready)`); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise<void> | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -617,7 +617,7 @@ export async function waitTransactionState( txState, )})`, ); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise<void> | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -669,7 +669,7 @@ export async function runIntegrationTest2( ws: InternalWalletState, args: IntegrationTestV2Args, ): Promise<void> { - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); logger.info("running test with arguments", args); const exchangeInfo = await fetchFreshExchange(ws, args.exchangeBaseUrl); diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts @@ -1486,8 +1486,6 @@ export class InternalWalletState { */ private resourceLocks: Set<string> = new Set(); - isTaskLoopRunning: boolean = false; - taskScheduler: TaskScheduler = new TaskScheduler(this); config: Readonly<WalletConfig>; @@ -1604,21 +1602,6 @@ export class InternalWalletState { } } } - - ensureTaskLoopRunning(): void { - if (this.isTaskLoopRunning) { - return; - } - this.taskScheduler - .run() - .catch((e) => { - logger.error("error running task loop"); - logger.error(`err: ${e}`); - }) - .then(() => { - logger.info("done running task loop"); - }); - } } /**