From b239ae1029eef5a68bf98d21e81acd317abb0673 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 28 Apr 2022 13:26:29 -0300 Subject: keep alive manifest v3 --- packages/taler-wallet-core/src/wallet.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'packages/taler-wallet-core') 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 @@ -326,6 +326,27 @@ export interface RetryLoopOpts { stopWhenDone?: boolean; } +/** + * 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. * @@ -336,6 +357,10 @@ async function runTaskLoop( opts: RetryLoopOpts = {}, ): Promise { 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; -- cgit v1.2.3