Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: two state dispatches into one to prevent possible race #1378

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/legacy/core/App/contexts/reducers/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { generateRandomWalletName } from '../../utils/helpers'
import { PersistentStorage } from '../../services/storage'

enum StateDispatchAction {
STATE_DISPATCH = 'state/stateDispatch',
STATE_LOADED = 'state/stateLoaded',
STATE_DISPATCH = 'state/stateDispatch'
}

enum OnboardingDispatchAction {
Expand Down Expand Up @@ -110,12 +109,9 @@ export interface ReducerAction<R> {

export const reducer = <S extends State>(state: S, action: ReducerAction<DispatchAction>): S => {
switch (action.type) {
case StateDispatchAction.STATE_LOADED: {
return { ...state, stateLoaded: true }
}
case StateDispatchAction.STATE_DISPATCH: {
const newState: State = (action?.payload || []).pop()
return { ...state, ...newState }
return { ...state, ...newState, stateLoaded: true }
}
case PreferencesDispatchAction.ENABLE_DEVELOPER_MODE: {
const choice = (action?.payload ?? []).pop() ?? false
Expand Down
4 changes: 0 additions & 4 deletions packages/legacy/core/App/navigators/RootStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { DeviceEventEmitter } from 'react-native'
import IconButton, { ButtonLocation } from '../components/buttons/IconButton'
import { EventTypes } from '../constants'
import { TOKENS, useServices } from '../container-api'
import { DispatchAction } from '../contexts/reducers/store'
import { useStore } from '../contexts/store'
import { useTheme } from '../contexts/theme'
import { useDeepLinks } from '../hooks/deep-links'
Expand Down Expand Up @@ -67,9 +66,6 @@ const RootStack: React.FC = () => {

useEffect(() => {
loadState(dispatch)
.then(() => {
dispatch({ type: DispatchAction.STATE_LOADED })
})
.catch((err: unknown) => {
const error = new BifoldError(t('Error.Title1044'), t('Error.Message1044'), (err as Error).message, 1001)
DeviceEventEmitter.emit(EventTypes.ERROR_ADDED, error)
Expand Down