Skip to content

Commit

Permalink
fix: manual emit wallet data
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed May 3, 2024
1 parent e7e9b5f commit b1f1b7f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
39 changes: 21 additions & 18 deletions packages/dapp-toolkit/src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,36 @@ export const StateClient = (input: {
sharedData: {},
} satisfies RdtState

const resetState = () => storageClient.setState(defaultState)
const resetState = () =>
storageClient.setState(defaultState).map(() => {
emitWalletData()
})

const initializeState = () =>
getState().mapErr(() => {
logger?.debug({
module: 'StateClient',
method: `initializeState.loadedCorruptedStateFromStorage`,
getState()
.mapErr(() => {
logger?.debug({
module: 'StateClient',
method: `initializeState.loadedCorruptedStateFromStorage`,
})
resetState()
emitWalletData()
})
.map(() => {
emitWalletData()
})
resetState()
})

initializeState()

const walletDataSubject = new BehaviorSubject<WalletData | undefined>(
undefined,
)

subscriptions.add(
merge(storageClient.storage$, of(null))
.pipe(
switchMap(() =>
getState().map((state) => {
walletDataSubject.next(state.walletData)
}),
),
)
.subscribe(),
)
const emitWalletData = () => {
storageClient.getState().map((state) => {
walletDataSubject.next(state?.walletData)
})
}

const walletData$ = walletDataSubject
.asObservable()
Expand All @@ -81,6 +83,7 @@ export const StateClient = (input: {
patchState,
getState,
walletData$,
emitWalletData,
getWalletData: () => walletDataSubject.value,
reset: resetState,
destroy: () => {
Expand Down
23 changes: 14 additions & 9 deletions packages/dapp-toolkit/src/wallet-request/wallet-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,20 @@ export const WalletRequestClient = (input: {
.map((transformedWalletResponse) => {
interactionStatusChangeSubject.next('success')

if (!oneTime)
stateClient.setState({
loggedInTimestamp: Date.now().toString(),
walletData: transformedWalletResponse,
sharedData: transformWalletRequestToSharedData(
walletInteraction,
state.sharedData,
),
})
if (!oneTime) {
stateClient
.setState({
loggedInTimestamp: Date.now().toString(),
walletData: transformedWalletResponse,
sharedData: transformWalletRequestToSharedData(
walletInteraction,
state.sharedData,
),
})
.map(() => {
stateClient.emitWalletData()
})
}

return transformedWalletResponse
})
Expand Down

0 comments on commit b1f1b7f

Please sign in to comment.