Skip to content

Instantly share code, notes, and snippets.

@BrianCortes
Last active July 8, 2021 16:51
Show Gist options
  • Save BrianCortes/f40701d5487f4788b0c14fa41deefd2e to your computer and use it in GitHub Desktop.
Save BrianCortes/f40701d5487f4788b0c14fa41deefd2e 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 fetchMachine = Machine(
{
id: 'balanceDataActor',
type: 'parallel',
context: {
balance: 0,
dining: 0
},
states: {
gettingData: {
initial: 'fetching',
states: {
fetching: {
invoke: {
id: 'getBalance',
src: (context, event) =>
new Promise((resolve, reject) => {
setTimeout(function () {
resolve({ balance: 10, dining: 0 });
}, 2000);
}),
onDone: {
target: 'waiting',
actions: ['updateBalance']
},
onError: {
target: 'failure',
actions: assign({ error: (context, event) => event.data })
}
}
},
waiting: {
on: {
UPDATE_BALANCE: {
target: 'fetching',
actions: ['updateBalance']
}
}
},
failure: {
TRY_AGAIN: 'fetching'
}
}
},
gettingStorageData: {
initial: 'getStorageBalance',
states: {
getStorageBalance: {
invoke: {
id: 'getStorageBalance',
src: (context, event) =>
new Promise((resolve, reject) => {
setTimeout(function () {
resolve({ balance: 10, dining: 0 });
}, 100);
}),
onDone: {
target: '',
actions: ['updateBalance']
}
}
}
}
}
}
},
{
actions: {
updateBalance: assign((_, event) => {
return event.data;
})
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment