From 1e6097ce2da416d9f4a11a4d3551978cea528f18 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 6 Nov 2019 10:17:00 +1100 Subject: [PATCH] Set initial state to null and don't call subscription callback unless not null --- src/@types/libraries.d.ts | 2 +- src/onboard.ts | 8 ++++++-- src/stores.ts | 14 +++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/@types/libraries.d.ts b/src/@types/libraries.d.ts index 6e1ee8d71..3ce5f1d6c 100644 --- a/src/@types/libraries.d.ts +++ b/src/@types/libraries.d.ts @@ -1,7 +1,7 @@ declare module "promise-cancelable" { export default class Cancelable extends Promise { constructor(executor: any) - cancel(): any + cancel(func: () => void): any isFulfilled(): any isResolved(): any isRejected(): any diff --git a/src/onboard.ts b/src/onboard.ts index 214f3cec1..5c9053993 100644 --- a/src/onboard.ts +++ b/src/onboard.ts @@ -56,7 +56,9 @@ function init(initialization: Initialization): API { if (subscriptions) { if (subscriptions.address) { address.subscribe((address: string | null) => { - address && subscriptions.address(address) + if (address !== null) { + subscriptions.address(address) + } }) } @@ -68,7 +70,9 @@ function init(initialization: Initialization): API { if (subscriptions.balance) { balance.subscribe((balance: string) => { - balance && subscriptions.balance(balance) + if (balance !== null) { + subscriptions.balance(balance) + } }) } diff --git a/src/stores.ts b/src/stores.ts index cfa787879..2e3ac6f73 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -41,7 +41,7 @@ export const network: WalletStateSliceStore = createWalletStateSliceStore({ parameter: "network", initialState: null }) -export const balance: BalanceStore = createBalanceStore("") +export const balance: BalanceStore = createBalanceStore(null) export const wallet: WritableStore = writable({ name: null, provider: null, @@ -106,14 +106,14 @@ function createWalletInterfaceStore( function createWalletStateSliceStore(options: { parameter: string - initialState: string | number | null + initialState: string | number | null | undefined }): WalletStateSliceStore { const { parameter, initialState } = options const { subscribe, set } = writable(initialState) return { subscribe, - reset: () => set(initialState), + reset: () => set(undefined), setStateSyncer: (stateSyncer: StateSyncer) => { validateType({ name: "stateSyncer", value: stateSyncer, type: "object" }) @@ -155,7 +155,7 @@ function createWalletStateSliceStore(options: { } } -function createBalanceStore(initialState: string): BalanceStore { +function createBalanceStore(initialState: string | null): BalanceStore { let stateSyncer: StateSyncer let emitter @@ -181,7 +181,7 @@ function createBalanceStore(initialState: string): BalanceStore { emitter.on("all", () => false) } else { // no address, so set balance back to null - set && set(initialState) + set && set(undefined) } } }) @@ -234,7 +234,7 @@ function syncStateWithTimeout(options: { "There was a problem getting the balance of this wallet" }) } - ) + ).catch(() => {}) ) balanceSyncStatus.syncing = prom @@ -251,7 +251,7 @@ function syncStateWithTimeout(options: { timedOut.then(() => { if (!prom.isFulfilled()) { - prom.cancel() + prom.cancel(() => {}) } }) }