Skip to content

Commit

Permalink
fix: store state machine
Browse files Browse the repository at this point in the history
Signed-off-by: clegirar <clemntgirard@gmail.com>
  • Loading branch information
clegirar committed Nov 26, 2020
1 parent 4223f5a commit 1d25fab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
4 changes: 2 additions & 2 deletions js/packages/components/settings/DevTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const BodyDevTools: React.FC<{}> = () => {
/>
<ButtonSetting
name={t('settings.devtools.simulate-button')}
icon='info-outline'
icon='alert-triangle-outline'
iconSize={30}
iconColor={color.dark.grey}
onPress={() =>
Expand All @@ -330,7 +330,7 @@ const BodyDevTools: React.FC<{}> = () => {
/>
<ButtonSetting
name={t('settings.devtools.debug-button')}
icon='info-outline'
icon='monitor-outline'
iconSize={30}
iconColor={color.dark.grey}
toggled
Expand Down
45 changes: 29 additions & 16 deletions js/packages/store/providerEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
defaultPersistentOptions,
MessengerActions,
MessengerAppState,
PersistentOptions,
PersistentOptionsKeys,
PersistentOptionsUpdate,
PersistentOptions,
} from '@berty-tech/store/context'
import beapi from '@berty-tech/api'
import { reducerAction } from '@berty-tech/store/providerReducer'
Expand Down Expand Up @@ -198,6 +198,7 @@ export const initialLaunch = async (dispatch: (arg0: reducerAction) => void, emb
.catch((err) => {
console.error('unable to init bridge ', Object.keys(err), err.domain)
})

const f = async () => {
const accounts = await refreshAccountList(embedded, dispatch)

Expand Down Expand Up @@ -272,18 +273,30 @@ export const openingDaemon = async (
bridgeOpts = cloneDeep(GoBridgeDefaultOpts)
}

try {
await accountService.openAccount({
args: bridgeOpts.cliArgs,
accountId: selectedAccount.toString(),
accountService
.getGRPCListenerAddrs({})
.then(() => {
// account already open
dispatch({ type: MessengerActions.SetStateOpeningClients })
})
dispatch({ type: MessengerActions.SetStateOpeningClients })
} catch (err) {
dispatch({
type: MessengerActions.SetStreamError,
payload: { error: new Error(`Failed to start node: ${err}`) },
.catch(() => {
// account not open
accountService
.openAccount({
args: bridgeOpts.cliArgs,
accountId: selectedAccount.toString(),
})
.then(() => {
console.log('account service is opened')
dispatch({ type: MessengerActions.SetStateOpeningClients })
})
.catch((err) => {
dispatch({
type: MessengerActions.SetStreamError,
payload: { error: new Error(`Failed to start node: ${err}`) },
})
})
})
}
}

// handle state OpeningWaitingForClients
Expand Down Expand Up @@ -399,7 +412,7 @@ export const openingClients = (

dispatch({
type: MessengerActions.SetStateOpeningListingEvents,
payload: { messengerClient, protocolClient, clearClients: cancel },
payload: { messengerClient, protocolClient, clearClients: () => cancel() },
})
}

Expand Down Expand Up @@ -433,17 +446,17 @@ export const closingDaemon = (
clearClients: (() => Promise<void>) | null,
dispatch: (arg0: reducerAction) => void,
) => {
if (!clearClients) {
return
}
return () => {
const f = async () => {
try {
if (clearClients) {
await clearClients()
}
await clearClients()
await accountService.closeAccount({})
} catch (e) {
console.warn('unable to stop protocol', e)
}

dispatch({ type: MessengerActions.BridgeClosed })
}

Expand Down
16 changes: 9 additions & 7 deletions js/packages/store/providerReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ export const reducerActions: {
}
},

[MessengerActions.SetStateOpeningListingEvents]: (oldState, action) => ({
...oldState,
client: action.payload.messengerClient || oldState.client,
protocolClient: action.payload.protocolClient || oldState.protocolClient,
clearClients: action.payload.unmountClients || oldState.clearClients,
appState: MessengerAppState.OpeningListingEvents,
}),
[MessengerActions.SetStateOpeningListingEvents]: (oldState, action) => {
return {
...oldState,
client: action.payload.messengerClient || oldState.client,
protocolClient: action.payload.protocolClient || oldState.protocolClient,
clearClients: action.payload.clearClients || oldState.clearClients,
appState: MessengerAppState.OpeningListingEvents,
}
},

[MessengerActions.SetStateClosed]: (oldState, _) => {
const ret = {
Expand Down

0 comments on commit 1d25fab

Please sign in to comment.