Skip to content

Instantly share code, notes, and snippets.

@xseignard
Last active January 15, 2019 16:32
Show Gist options
  • Save xseignard/cb9fc259de578c982d404820f39afba3 to your computer and use it in GitHub Desktop.
Save xseignard/cb9fc259de578c982d404820f39afba3 to your computer and use it in GitHub Desktop.
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