Created
March 10, 2020 04:18
-
-
Save andrewgordstewart/2a1d8063dd5953e00f86c6d8d28dcbdf to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = { | |
id: 'application-workflow', | |
initial: 'initializing', | |
on: {CHANNEL_UPDATED: {actions: ['sendChannelUpdatedNotification']}}, | |
states: { | |
initializing: { | |
entry: 'displayUi', | |
on: { | |
CREATE_CHANNEL: {target: 'confirmCreateChannelWorkflow', actions: ['assignChannelParams']}, | |
JOIN_CHANNEL: {target: 'confirmJoinChannelWorkflow'} | |
} | |
}, | |
confirmCreateChannelWorkflow: { | |
initial: 'getDataForCreateChannelConfirmation', | |
states: { | |
getDataForCreateChannelConfirmation: { | |
invoke: { | |
src: 'getDataForCreateChannelConfirmation', | |
onDone: 'invokeCreateChannelConfirmation' | |
} | |
}, | |
invokeCreateChannelConfirmation: { | |
invoke: { | |
id: undefined, | |
src: 'invokeCreateChannelConfirmation', | |
data: (_, {data}) => data, | |
onDone: 'done', | |
autoForward: true | |
} | |
}, | |
done: {type: 'final'} | |
}, | |
onDone: 'createChannelInStore' | |
}, | |
confirmJoinChannelWorkflow: { | |
initial: 'signFirstState', | |
states: { | |
signFirstState: {invoke: {src: 'signFirstState', onDone: 'confirmChannelCreation'}}, | |
confirmChannelCreation: { | |
initial: 'getDataForCreateChannelConfirmation', | |
states: { | |
getDataForCreateChannelConfirmation: { | |
invoke: { | |
src: 'getDataForCreateChannelConfirmation', | |
onDone: 'invokeCreateChannelConfirmation' | |
} | |
}, | |
invokeCreateChannelConfirmation: { | |
invoke: { | |
id: undefined, | |
src: 'invokeCreateChannelConfirmation', | |
data: (_, {data}) => data, | |
onDone: 'done', | |
autoForward: true | |
} | |
}, | |
done: {type: 'final'} | |
}, | |
onDone: 'done' | |
}, | |
done: {type: 'final'} | |
}, | |
entry: ['assignChannelId', 'spawnObserver'], | |
exit: ['sendJoinChannelResponse'], | |
onDone: 'openChannelAndFundProtocol' | |
}, | |
createChannelInStore: { | |
invoke: { | |
data: (context, event) => event.data, | |
src: 'createChannel', | |
onDone: { | |
target: 'openChannelAndFundProtocol', | |
actions: ['assignChannelId', 'spawnObserver', 'sendCreateChannelResponse'] | |
} | |
} | |
}, | |
openChannelAndFundProtocol: { | |
initial: 'getDataForCreateChannelAndFund', | |
states: { | |
getDataForCreateChannelAndFund: { | |
invoke: { | |
src: 'getDataForCreateChannelAndFund', | |
onDone: 'invokeCreateChannelAndFundProtocol' | |
} | |
}, | |
invokeCreateChannelAndFundProtocol: { | |
invoke: { | |
id: undefined, | |
src: 'invokeCreateChannelAndFundProtocol', | |
data: (_, {data}) => data, | |
onDone: 'done', | |
autoForward: true | |
} | |
}, | |
done: {type: 'final'} | |
}, | |
onDone: 'running' | |
}, | |
running: { | |
entry: ['hideUi'], | |
on: { | |
SPAWN_OBSERVERS: {actions: ['spawnObserver']}, | |
PLAYER_STATE_UPDATE: { | |
target: 'running', | |
actions: ['updateStoreWithPlayerState', 'sendUpdateChannelResponse'] | |
}, | |
CHANNEL_UPDATED: [ | |
{ | |
cond: {name: 'channelClosing', predicate: () => true, type: 'xstate.guard'}, | |
target: 'concludeState' | |
} | |
], | |
PLAYER_REQUEST_CONCLUDE: {target: 'concludeState'} | |
} | |
}, | |
concludeState: {invoke: {src: 'signConcludeState', onDone: {target: 'closing'}}}, | |
closing: { | |
entry: 'displayUi', | |
exit: 'hideUi', | |
invoke: { | |
id: 'closing-protocol', | |
src: 'invokeClosingProtocol', | |
data: context => context, | |
autoForward: true, | |
onDone: {target: 'done', actions: ['sendCloseChannelResponse']} | |
} | |
}, | |
done: {type: 'final'} | |
} | |
}; | |
const guards = { | |
channelOpen: {name: 'channelOpen', predicate: () => true, type: 'xstate.guard'}, | |
channelClosing: {name: 'channelClosing', predicate: () => true, type: 'xstate.guard'}, | |
channelClosed: {name: 'channelClosed', predicate: () => true, type: 'xstate.guard'} | |
}; | |
const customActions = { | |
sendCloseChannelResponse: 'sendCloseChannelResponse', | |
sendUpdateChannelResponse: 'sendUpdateChannelResponse', | |
assignChannelParams: 'assignChannelParams', | |
sendCreateChannelResponse: 'sendCreateChannelResponse', | |
sendJoinChannelResponse: 'sendJoinChannelResponse', | |
sendChannelUpdatedNotification: 'sendChannelUpdatedNotification', | |
hideUi: 'hideUi', | |
displayUi: 'displayUi', | |
assignChannelId: 'assignChannelId', | |
spawnObservers: 'spawnObserver', | |
updateStoreWithPlayerState: 'updateStoreWithPlayerState' | |
}; | |
const machine = Machine(config, {guards, actions: customActions}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment