Skip to content

Instantly share code, notes, and snippets.

@jonathanj
Last active November 3, 2019 17:55
Show Gist options
  • Save jonathanj/333a34b1accc36b1c8feee12566ed7d0 to your computer and use it in GitHub Desktop.
Save jonathanj/333a34b1accc36b1c8feee12566ed7d0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const guidedEvent = target => ({
target,
actions: assign({
waitingForGuidedEvent: (context, event) => false
})
})
const updateStates = (isGuided) => {
const guidedWrapper = isGuided ? guidedEvent : x => x;
return {
initial: 'updating',
states: {
updating: {
on: {
PROGRESS: guidedWrapper('updating'),
DONE: guidedWrapper('#update.success'),
FAILURE: guidedWrapper('#update.failure'),
SUPPORT_CHAT: '#update.supportChat',
}
},
}
};
}
const canPerformGuided = (context) => {
return context.canPerformGuided;
}
const waitingForGuidedEvent = (context) => {
return context.waitingForGuidedEvent;
}
const updateMachine = Machine({
id: 'update',
initial: 'idle',
context: {
retries: 0,
canPerformGuided: true,
waitingForGuidedEvent: true,
},
states: {
idle: {
on: {
UPDATE: 'updating',
SNOOZE: 'snooze',
}
},
updating: {
on: {
'': [
{
target: 'updatingGuided',
cond: 'canPerformGuided'
},
{
target: 'updatingUnguided',
}
],
UNGUIDED: 'updatingUnguided',
GUIDED: 'updatingGuided'
},
},
updatingUnguided: {
...updateStates(false),
},
updatingGuided: {
...updateStates(true),
after: {
6000: [
{
target: 'updatingUnguided',
cond: 'waitingForGuidedEvent'
},
]
},
},
success: {
type: 'final'
},
failure: {
on: {
RETRY: {
target: 'updating',
actions: assign({
retries: (context, event) => context.retries + 1,
waitingForGuidedEvent: (context, event) => true,
})
},
SUPPORT_CALL: 'supportCall',
}
},
supportCall: {
type: 'final',
},
supportChat: {
type: 'final',
},
snooze: {
type: 'final',
},
},
},
{
guards: {
waitingForGuidedEvent,
canPerformGuided,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment