Skip to content

Instantly share code, notes, and snippets.

@NitsanBaleli
Created October 8, 2020 12:12
Show Gist options
  • Save NitsanBaleli/ee09ce5bba95a86a6480d4a196a48562 to your computer and use it in GitHub Desktop.
Save NitsanBaleli/ee09ce5bba95a86a6480d4a196a48562 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: {
networkQuality: '',
neverShowWarning: false,
warningTimstamp: undefined
},
states: {
idle: {
},
warning: {
},
idleNoWarning: {
on: {
REPORT_NETWORK_QUALITY: {
}
}
}
},
on: {
REPORT_NETWORK_QUALITY: [
{
target: "warning",
cond: (ctx, event) => { return event.payload === 'bad' || event.payload === 'worse'},
actions: assign({
networkQuality: (ctx, event) => event.payload,
warningTimstamp: () => {
const x = new Date();
return x.getTime();
}
})
},
{
target: "idle",
cond: function(ctx, event) {
const x = new Date();
const twoSecondsPassed = (x.getTime() - ctx.warningTimstamp) >= 2000;
return twoSecondsPassed && event.payload !== 'bad' && event.payload !== 'worse'
},
actions: assign({
networkQuality: (ctx, event) => event.payload
})
}
, {
actions: assign({
networkQuality: (ctx, event) => event.payload
})
}
],
DISMISS_NETWORK_QUALITY_WARNING: 'idleNoWarning'
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment