Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Last active May 5, 2021 19:02
Show Gist options
  • Save austinsamsel/2a000faf3af7257181c308f34efb677b to your computer and use it in GitHub Desktop.
Save austinsamsel/2a000faf3af7257181c308f34efb677b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fcl = {decode: () => {}}
const FS = {
IDLE: 'idle',
SENDING: 'sending',
POLLING: 'polling',
DONE: 'done',
ERROR: 'error',
};
const machineDef = {
id: 'script',
initial: FS.IDLE,
context: {
decoder: fcl.decode,
ix: undefined,
isPolling: false,
result: undefined,
error: undefined,
},
states: {
[FS.IDLE]: {
on: {
send: {
target: 'sending',
actions: 'assignSendData',
},
},
},
[FS.SENDING]: {
invoke: {
src: 'fclSend',
onDone: [
{
target: 'polling',
actions: 'assignScriptResult',
},
],
onError: {
target: '#error',
actions: 'assignError',
},
},
},
[FS.POLLING]: {
initial: 'waiting',
states: {
waiting: {
after: {
POLL_DELAY: 'sending',
},
},
sending: {
invoke: {
src: 'fclSend',
onDone: {
target: '#done',
actions: 'assignScriptResult',
},
onError: {
target: '#error',
actions: 'assignError',
},
},
},
},
on: {
stop: {
target: 'done',
actions: 'assignStopData',
},
},
},
[FS.DONE]: {
id: [FS.DONE],
always: {
target: 'polling',
cond: 'isPolling',
},
on: {
send: 'sending',
},
},
[FS.ERROR]: {
id: [FS.ERROR],
on: {
send: 'sending',
},
},
},
on: {
error: 'error',
},
};
const guards = {
isPolling: (ctx) => true, // ctx.poll,
};
const POLL_DELAY = 1000;
const machine = Machine(
machineDef,
{
delays: { POLL_DELAY },
guards,
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment