diff options
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); + } +} |