diff options
author | Florian Dold <florian@dold.me> | 2023-02-20 21:26:08 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-02-20 21:26:08 +0100 |
commit | a49959d2c8bf82575c5d232217a33d91e7b008e8 (patch) | |
tree | 03dbbfd397a83dffaaa53580102f3d7108a7b70d /packages/taler-wallet-core/src/operations/common.ts | |
parent | e8b5f26ab6407fbcecfe464f5aba8d1db9e487cd (diff) | |
download | wallet-core-a49959d2c8bf82575c5d232217a33d91e7b008e8.tar.xz |
wallet-core: support long-polling for peer push credit
Diffstat (limited to 'packages/taler-wallet-core/src/operations/common.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/common.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/packages/taler-wallet-core/src/operations/common.ts b/packages/taler-wallet-core/src/operations/common.ts index e5eda074c..3905eaf3e 100644 --- a/packages/taler-wallet-core/src/operations/common.ts +++ b/packages/taler-wallet-core/src/operations/common.ts @@ -21,11 +21,13 @@ import { AgeRestriction, AmountJson, Amounts, + CancellationToken, CoinRefreshRequest, CoinStatus, ExchangeEntryStatus, ExchangeListItem, ExchangeTosStatus, + getErrorDetailFromException, j2s, Logger, OperationErrorInfo, @@ -453,3 +455,42 @@ export function makeExchangeListItem( lastUpdateErrorInfo, }; } + +export interface LongpollResult { + ready: boolean; +} + +export function runLongpollAsync( + ws: InternalWalletState, + retryTag: string, + reqFn: (ct: CancellationToken) => Promise<LongpollResult>, +): void { + const asyncFn = async () => { + if (ws.stopped) { + logger.trace("not long-polling reserve, wallet already stopped"); + await storeOperationPending(ws, retryTag); + return; + } + const cts = CancellationToken.create(); + let res: { ready: boolean } | undefined = undefined; + try { + ws.activeLongpoll[retryTag] = { + cancel: () => { + logger.trace("cancel of reserve longpoll requested"); + cts.cancel(); + }, + }; + res = await reqFn(cts.token); + } catch (e) { + await storeOperationError(ws, retryTag, getErrorDetailFromException(e)); + return; + } finally { + delete ws.activeLongpoll[retryTag]; + } + if (!res.ready) { + await storeOperationPending(ws, retryTag); + } + ws.latch.trigger(); + }; + asyncFn(); +} |