Skip to content

Instantly share code, notes, and snippets.

@andrewgordstewart
Created April 3, 2020 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgordstewart/22de47394d05a9e6204be288d43bd119 to your computer and use it in GitHub Desktop.
Save andrewgordstewart/22de47394d05a9e6204be288d43bd119 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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'},
isDirectFunding: {name: 'isDirectFunding', predicate: () => true, type: 'xstate.guard'},
isLedgerFunding: {name: 'isLedgerFunding', predicate: () => true, type: 'xstate.guard'},
isVirtualFunding: {name: 'isVirtualFunding', predicate: () => true, type: 'xstate.guard'},
amCreator: {name: 'amCreator', predicate: () => true, type: 'xstate.guard'},
amJoiner: {name: 'amJoiner', predicate: () => true, type: 'xstate.guard'}
};
const customActions = {};
const config = {
id: 'application-workflow',
initial: 'joiningChannel',
on: {CHANNEL_UPDATED: {actions: ['sendChannelUpdatedNotification']}},
states: {
joiningChannel: {
initial: 'joining',
states: {
failure: {},
joining: {
on: {
'': [
{
target: 'failure',
cond: {name: 'isLedgerFunding', predicate: () => true, type: 'xstate.guard'}
},
{
target: 'done',
cond: {name: 'amCreator', predicate: () => true, type: 'xstate.guard'}
}
],
JOIN_CHANNEL: {target: 'done', actions: 'sendJoinChannelResponse'}
}
},
done: {type: 'final'}
},
onDone: [
{
target: 'confirmingWithUser',
cond: {name: 'isDirectFunding', predicate: () => true, type: 'xstate.guard'}
},
{
target: 'creatingChannel',
cond: {name: 'isVirtualFunding', predicate: () => true, type: 'xstate.guard'}
}
]
},
confirmingWithUser: {
entry: ['displayUi', 'assignUIState'],
invoke: {src: 'invokeCreateChannelConfirmation', onDone: 'creatingChannel'}
},
creatingChannel: {
on: {
'': {
target: 'fundingChannel',
cond: {name: 'amJoiner', predicate: () => true, type: 'xstate.guard'}
}
},
invoke: {
data: (_, event) => event.data,
src: 'createChannel',
onDone: {
target: 'fundingChannel',
actions: ['assignChannelId', 'sendCreateChannelResponse']
}
}
},
fundingChannel: {
invoke: {
src: 'invokeCreateChannelAndFundProtocol',
data: ctx => ({
channelId: ctx.channelId,
funding: ctx.fundingStrategy
}),
onDone: 'running'
}
},
running: {
entry: ['hideUi', 'spawnObservers'],
on: {
SPAWN_OBSERVERS: {actions: 'spawnObservers'},
PLAYER_STATE_UPDATE: {
target: 'running',
actions: ['updateStoreWithPlayerState', 'sendUpdateChannelResponse']
},
CHANNEL_UPDATED: [
{
target: 'closing',
cond: {name: 'channelClosing', predicate: () => true, type: 'xstate.guard'}
}
],
PLAYER_REQUEST_CONCLUDE: {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 machine = Machine(config, {guards, actions: customActions});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment