Last active
December 15, 2020 17:10
-
-
Save pke/9ce4bb4840c35b4c3567adf98dacf8fb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
const guards = { | |
canCountDown: context => !guards.countedDown(context), | |
countedDown: context => context.count === 0 | |
} | |
const countdownMachine = Machine({ | |
id: "countdown", | |
initial: "idle", | |
context: { | |
count: 10 | |
}, | |
states: { | |
idle: { | |
on: { | |
START: { | |
actions: "startCounting", | |
target: "counting" | |
} | |
} | |
}, | |
counting: { | |
after: { | |
1000: { | |
target: "counting", | |
actions: "countdown", | |
cond: "canCountDown" | |
} | |
}, | |
on: { | |
"": { | |
target: "done", | |
cond: "countedDown" | |
}, | |
STOP: { | |
target: "idle", | |
actions: "reset" | |
} | |
} | |
}, | |
done: { | |
type: "final", | |
on: { | |
RESTART: { | |
target: "counting", | |
actions: "reset" | |
} | |
} | |
} | |
} | |
}, { | |
guards, | |
actions: { | |
startCounting: assign({ startCount: context => context.count }), | |
reset: assign({ count: context => context.startCount }), | |
countdown: assign({ count: context => context.count - 1 }) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment