Skip to content

Instantly share code, notes, and snippets.

@MaelFr
Last active March 26, 2021 17:12
Show Gist options
  • Save MaelFr/c650c9dd130201e3e82d63239d1eb85f to your computer and use it in GitHub Desktop.
Save MaelFr/c650c9dd130201e3e82d63239d1eb85f 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: 'fetch',
initial: 'idle',
context: {
elapsed: 0,
duration: 2,
interval: 0.1
},
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
invoke: {
src: context => cb => {
const interval = setInterval(() => {
cb('TICK');
}, 1000 * context.interval);
return () => {
clearInterval(interval);
};
}
},
on: {
'': {
target: 'longLoading',
cond: context => {
return context.elapsed >= context.duration;
}
},
RESOLVE: 'success',
REJECT: 'failure',
TICK: {
actions: assign({
elapsed: context => +(context.elapsed + context.interval).toFixed(2)
})
}
}
},
longLoading: {
on: {
RESOLVE: 'success',
REJECT: 'failure',
}
},
success: {
type: 'final'
},
failure: {
on: {
RETRY: 'loading'
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment