Skip to content

Commit

Permalink
Merge pull request #256 from radixdlt/RDT-222
Browse files Browse the repository at this point in the history
fix: wait for extension status before sending interaction
  • Loading branch information
dawidsowardx authored Sep 20, 2024
2 parents aa56ef6 + 7f098e5 commit 3ee6308
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
25 changes: 7 additions & 18 deletions packages/dapp-toolkit/src/modules/state/state.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { BehaviorSubject, Subscription, filter } from 'rxjs'
import { RdtState, WalletData, walletDataDefault } from './types'
import { Logger } from '../../helpers'
import { StorageModule } from '../storage'
import { SdkError } from '../../error'
import { err, ok } from 'neverthrow'
import { ok, okAsync } from 'neverthrow'

export type StateModule = ReturnType<typeof StateModule>

Expand All @@ -23,12 +22,11 @@ export const StateModule = (input: {
const getState = () =>
storageModule
.getState()
.andThen((state) =>
state ? ok(state) : err(SdkError('StateNotFound', '')),
)

.orElse(() => okAsync(defaultState))
.andThen((state) => (state ? ok(state) : ok(defaultState)))

const patchState = (state: Partial<RdtState>) =>
getState().andThen((oldState) => setState({ ...oldState, ...state }))
getState().andThen((oldState) => setState({ ...oldState, ...state } as RdtState))

const defaultState = {
walletData: walletDataDefault,
Expand All @@ -43,17 +41,8 @@ export const StateModule = (input: {

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

initializeState()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const ConnectorExtensionModule = (input: {

const subjects = input?.subjects ?? ConnectorExtensionSubjects()
const subscription = new Subscription()
const extensionDetectionTime = input?.extensionDetectionTime ?? 100
const extensionDetectionTime = input?.extensionDetectionTime ?? 200
const requestItemModule = input.providers.requestItemModule
const storage =
input.providers.storageModule.getPartition('connectorExtension')
Expand Down Expand Up @@ -239,9 +239,9 @@ export const ConnectorExtensionModule = (input: {
filter((value): value is Err<never, SdkError> => !('eventType' in value)),
)

const sendWalletRequest$ = of(
wrapOutgoingInteraction(walletInteraction),
).pipe(
const sendWalletRequest$ = extensionStatus$.pipe(
filter((status) => status.isExtensionAvailable),
switchMap(() => of(wrapOutgoingInteraction(walletInteraction))),
tap((result) => {
result.map((message) => {
subjects.outgoingMessageSubject.next(message)
Expand Down

0 comments on commit 3ee6308

Please sign in to comment.