diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-05 19:38:19 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-05 19:38:19 +0100 |
commit | f67d7f54f9d0fed97446898942e3dfee67ee2985 (patch) | |
tree | 2b81738025e8f61250ede10908cbf81071e16975 /src/wallet-impl/state.ts | |
parent | 829acdd3d98f1014747f15ecb619b6fbaa06b640 (diff) | |
download | wallet-core-f67d7f54f9d0fed97446898942e3dfee67ee2985.tar.xz |
threads, retries and notifications WIP
Diffstat (limited to 'src/wallet-impl/state.ts')
-rw-r--r-- | src/wallet-impl/state.ts | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/src/wallet-impl/state.ts b/src/wallet-impl/state.ts index a04a7dd1c..18df861f1 100644 --- a/src/wallet-impl/state.ts +++ b/src/wallet-impl/state.ts @@ -15,19 +15,54 @@ */ import { HttpRequestLibrary } from "../util/http"; -import { Badge, Notifier, NextUrlResult } from "../walletTypes"; +import { + NextUrlResult, + WalletBalance, + PendingOperationsResponse, + WalletNotification, +} from "../walletTypes"; import { SpeculativePayData } from "./pay"; -import { CryptoApi } from "../crypto/cryptoApi"; -import { AsyncOpMemo } from "../util/asyncMemo"; - -export interface InternalWalletState { - db: IDBDatabase; - http: HttpRequestLibrary; - badge: Badge; - notifier: Notifier; +import { CryptoApi, CryptoWorkerFactory } from "../crypto/workers/cryptoApi"; +import { AsyncOpMemoMap, AsyncOpMemoSingle } from "../util/asyncMemo"; +import { Logger } from "../util/logging"; + +type NotificationListener = (n: WalletNotification) => void; + +const logger = new Logger("state.ts"); + +export class InternalWalletState { + speculativePayData: SpeculativePayData | undefined = undefined; + cachedNextUrl: { [fulfillmentUrl: string]: NextUrlResult } = {}; + memoProcessReserve: AsyncOpMemoMap<void> = new AsyncOpMemoMap(); + memoMakePlanchet: AsyncOpMemoMap<void> = new AsyncOpMemoMap(); + memoGetPending: AsyncOpMemoSingle< + PendingOperationsResponse + > = new AsyncOpMemoSingle(); + memoGetBalance: AsyncOpMemoSingle<WalletBalance> = new AsyncOpMemoSingle(); + memoProcessRefresh: AsyncOpMemoMap<void> = new AsyncOpMemoMap(); cryptoApi: CryptoApi; - speculativePayData: SpeculativePayData | undefined; - cachedNextUrl: { [fulfillmentUrl: string]: NextUrlResult }; - memoProcessReserve: AsyncOpMemo<void>; - memoMakePlanchet: AsyncOpMemo<void>; -}
\ No newline at end of file + + listeners: NotificationListener[] = []; + + constructor( + public db: IDBDatabase, + public http: HttpRequestLibrary, + cryptoWorkerFactory: CryptoWorkerFactory, + ) { + this.cryptoApi = new CryptoApi(cryptoWorkerFactory); + } + + public notify(n: WalletNotification) { + logger.trace("Notification", n); + for (const l of this.listeners) { + const nc = JSON.parse(JSON.stringify(n)); + setImmediate(() => { + l(nc); + }); + } + } + + addNotificationListener(f: (n: WalletNotification) => void): void { + this.listeners.push(f); + } +} |