Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Last active September 18, 2020 19:28
Show Gist options
  • Save austinsamsel/5d7f69421a4f52e98d2b5be693bcfa44 to your computer and use it in GitHub Desktop.
Save austinsamsel/5d7f69421a4f52e98d2b5be693bcfa44 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const IGNORE_ERRORS_DURATION = 3000;
const FS = {
IDLE: 'idle',
UNSEALED_RESET: 'unsealed_reset',
OPENING: 'opening',
UNSEALED_RETRYING: 'unsealed_retrying',
POLLING_PACK: 'polling_pack',
GETTING_MOMENTS: 'getting_moments',
DONE: 'done',
}
const PACK_STATUSES = {
SEALED: 'SEALED',
FULFILLED: 'FULFILLED',
OPENED: 'OPENED'
}
Machine(
{
id: 'openPack',
initial: 'idle',
states: {
[FS.IDLE]: {
on: {
OPEN_PACK: [
{
target: '#openPack.polling_pack',
cond: 'isPackUnsealed',
},
{ target: '#openPack.opening' },
],
},
},
[FS.UNSEALED_RESET]: {
on: { RETRY: '#openPack.polling_pack' },
},
[FS.OPENING]: {
invoke: {
id: 'opening',
src: 'openPack',
onDone: 'polling_pack',
onError: {
target: '#openPack.idle',
actions: 'setErrorMessage',
},
},
},
[FS.POLLING_PACK]: {
after: {
[IGNORE_ERRORS_DURATION]: {
actions: send('STOP', { to: 'pollingPack' }),
},
},
invoke: {
id: 'pollingPack',
src: 'pollPack',
onError: {
target: '#openPack.unsealed_reset',
actions: 'setErrorMessage',
},
},
on: {
PACK_POLL_ERROR: {
target: '#openPack.unsealed_reset',
actions: 'setErrorMessage',
},
PACK_POLL_SUCCESS: '#openPack.getting_moments',
},
},
[FS.GETTING_MOMENTS]: {
invoke: {
id: 'getting_moments',
src: 'getMoments',
onDone: {
target: '#openPack.idle',
actions: 'opened',
},
onError: {
target: '#openPack.unsealed_reset',
actions: 'setErrorMessage',
},
},
},
},
},
{
guards: {
isPackUnsealed: (context, event) => {
const status = event?.pack?.status;
return (
status === PACK_STATUSES.OPENED || status === PACK_STATUSES.FULFILLED
);
// TO TEST:
// return true;
// return false;
},
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment