Last active
January 15, 2019 16:32
-
-
Save xseignard/cb9fc259de578c982d404820f39afba3 to your computer and use it in GitHub Desktop.
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 { Machine } = require('xstate'); | |
const { interpret } = require('xstate/lib/interpreter'); | |
const mechanisms = { | |
initial: 'empty', | |
states: { | |
empty: { | |
on: { NEXT: 'e1' }, | |
}, | |
e1: { | |
on: { NEXT: 'e2' }, | |
}, | |
e2: { | |
on: { NEXT: 'e3' }, | |
}, | |
e3: { | |
on: { NEXT: 'solved' }, | |
}, | |
solved: { | |
type: 'final', | |
on: { '': 'empty' } | |
} | |
}, | |
}; | |
const game = Machine({ | |
key: 'haunted', | |
initial: 'wait', | |
states: { | |
wait: { | |
on: { ENTER: 'r1' }, | |
}, | |
r1: { | |
onDone: 'r2', | |
...mechanisms, | |
}, | |
r2: { | |
onDone: 'r3', | |
...mechanisms, | |
}, | |
r3: { | |
onDone: 'r4', | |
...mechanisms, | |
}, | |
r4: { | |
onDone: 'r5', | |
...mechanisms, | |
}, | |
r5: { | |
onDone: 'r6', | |
...mechanisms, | |
}, | |
r6: { | |
onDone: 'end', | |
...mechanisms, | |
}, | |
end: { type: 'final' }, | |
}, | |
on: { LEAVE: 'end' }, | |
}); | |
const service = interpret(game) | |
.onTransition(state => console.log(state.value)) | |
.start(); | |
service.send('ENTER'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment