Created
April 16, 2020 03:21
-
-
Save sukima/d18796ec265313b41e87b5f6cfe362d5 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
function isKey(key) { | |
return function(_, evt) { | |
return evt.which === key; | |
}; | |
} | |
function *buildStatesSequences([initial, ...sequences]) { | |
let [, firstState] = initial; | |
let current = firstState; | |
function keyState(key, target) { | |
return { | |
[current]: { | |
on: { | |
KEYUP: [ | |
{ target, cond: isKey(key) }, | |
{ target: firstState } | |
] | |
} | |
} | |
}; | |
} | |
for (let [key, target] of sequences) { | |
yield keyState(key, target); | |
current = target; | |
} | |
yield { [current]: { type: 'final' } }; | |
} | |
const fetchMachine = Machine({ | |
id: 'konami', | |
initial: 'idle', | |
states: Object.assign( | |
...buildStatesSequences([ | |
[null, 'idle'], | |
['keyUP', 'up1'], | |
['keyUP', 'up2'], | |
['keyDOWN', 'down1'], | |
['keyDOWN', 'down2'], | |
['keyLEFT', 'left1'], | |
['keyRIGHT', 'right1'], | |
['keyLEFT', 'left2'], | |
['keyRIGHT', 'right2'], | |
['keyB', 'b'], | |
['keyA', 'a'] | |
]) | |
) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment