Skip to content

Instantly share code, notes, and snippets.

@drmikecrowe
Last active November 29, 2019 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmikecrowe/e84047089c26f3e71d8f851ecdbe236e to your computer and use it in GitHub Desktop.
Save drmikecrowe/e84047089c26f3e71d8f851ecdbe236e 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 fetchIt = (ctx, evt) => new Promise(resolve => {
setTimeout(() => {
resolve();
}, 1000);
});
const resetRetries = assign({ retries: () => -1 });
const monitorNetwork = () => {
const interval = setInterval(() => {
console.log(`Checking network connectivity`);
if (Math.random() > 0.55) {
console.log(`Simulating disconnected`);
send("NETWORK_DISCONNECTED");
}
}, 10000);
return () => {
console.log(`Leaving state, stopping network monitoring`);
clearInterval(interval);
};
}
const fetchMachine = Machine({
id: 'fetch',
initial: 'idle',
context: {
retries: 0
},
states: {
idle: {
invoke: {
id: 'fetching',
src: fetchIt,
onDone: {
target: 'connected',
},
}
},
connected: {
activities: ["monitorNetwork"],
entry: ["resetRetries", "sendConnected"],
on: {
NETWORK_DISCONNECTED: "success"
},
},
success: {
type: 'final'
},
}
}, {
activities: {
monitorNetwork,
},
actions: {
resetRetries,
sendConnected: sendParent("READY")
},
services: {
fetchIt
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment