Skip to content

Commit

Permalink
Merge pull request #50 from blocknative/enhancement/subscription-init…
Browse files Browse the repository at this point in the history
…ial-state

Subscription Initial State
  • Loading branch information
lnbc1QWFyb24 authored Nov 5, 2019
2 parents 0e92b8c + 1e6097c commit 8ec8417
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/@types/libraries.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module "promise-cancelable" {
export default class Cancelable extends Promise<any> {
constructor(executor: any)
cancel(): any
cancel(func: () => void): any
isFulfilled(): any
isResolved(): any
isRejected(): any
Expand Down
8 changes: 6 additions & 2 deletions src/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}

Expand All @@ -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)
}
})
}

Expand Down
14 changes: 7 additions & 7 deletions src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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" })

Expand Down Expand Up @@ -155,7 +155,7 @@ function createWalletStateSliceStore(options: {
}
}

function createBalanceStore(initialState: string): BalanceStore {
function createBalanceStore(initialState: string | null): BalanceStore {
let stateSyncer: StateSyncer
let emitter

Expand All @@ -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)
}
}
})
Expand Down Expand Up @@ -234,7 +234,7 @@ function syncStateWithTimeout(options: {
"There was a problem getting the balance of this wallet"
})
}
)
).catch(() => {})
)

balanceSyncStatus.syncing = prom
Expand All @@ -251,7 +251,7 @@ function syncStateWithTimeout(options: {

timedOut.then(() => {
if (!prom.isFulfilled()) {
prom.cancel()
prom.cancel(() => {})
}
})
}

0 comments on commit 8ec8417

Please sign in to comment.