Last active
May 10, 2020 13:17
-
-
Save LostKobrakai/3b824a6fe12f6dc3adc22dfef1de3672 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetchMachine = Machine({ | |
id: 'timer', | |
initial: 'stopped', | |
context: { | |
duration: 0, | |
endDate: null | |
}, | |
states: { | |
stopped: { | |
on: { | |
START: { | |
cond: (_, event) => event.endDate, | |
target: 'running', | |
actions: assign({ | |
duration: (context, event) => | |
new Date(event.endDate) - (new Date()), | |
endDate: (context, event) => | |
new Date(event.endDate), | |
}) | |
} | |
} | |
}, | |
running: { | |
on: { | |
TICK: [ | |
{ | |
cond: (context) => | |
context.endDate <= (new Date()), | |
target: 'stopped' | |
}, | |
{ | |
actions: assign({ | |
duration: (context, event) => | |
context.endDate - (new Date()) | |
}) | |
} | |
], | |
PAUSE: 'paused' | |
} | |
}, | |
paused: { | |
on: { | |
START: 'running', | |
STOP: { | |
target: 'stopped', | |
actions: assign({ | |
duration: 0 | |
}) | |
} | |
} | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment