Last active
April 7, 2020 00:18
-
-
Save jkjustjoshing/5a37b8e7c39fe5267c51b79290c38a35 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 calculationMachine = Machine( | |
{ | |
id: 'calculator', | |
initial: 'init', | |
context: { | |
number1: '', | |
operand: null, | |
number2: '', | |
result: null, | |
}, | |
states: { | |
init: { | |
on: { | |
NUMBER: { | |
target: 'waitingForInput', | |
actions: 'inputNumber1', | |
}, | |
}, | |
}, | |
waitingForInput: { | |
on: { | |
NUMBER: { | |
target: 'waitingForInput', | |
actions: 'inputNumber1', | |
}, | |
OPERAND: { | |
target: 'waitingForSecondInput', | |
actions: 'inputOperand', | |
}, | |
}, | |
}, | |
waitingForSecondInput: { | |
on: { | |
NUMBER: { | |
target: 'waitingForSecondInput', | |
actions: 'inputNumber2', | |
}, | |
GET_RESULT: { | |
target: 'result', | |
actions: 'computeResult', | |
}, | |
}, | |
}, | |
result: { | |
type: 'final', | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
inputNumber1: assign({ | |
number1: (context, event) => context.number1 + event.value, | |
}), | |
inputNumber2: assign({ | |
number2: (context, event) => context.number2 + event.value, | |
}), | |
inputOperand: assign({ | |
operand: (context, event) => event.value, | |
}), | |
computeResult: assign({ | |
result: (context) => { | |
const a = parseInt(context.number1, 10) | |
const b = parseInt(context.number2, 10) | |
return context.operand.fn(a, b) | |
}, | |
}), | |
}, | |
} | |
) | |
const fetchMachine = Machine({ | |
id: 'root', | |
initial: 'init', | |
context: { | |
result: null, | |
}, | |
states: { | |
init: { | |
invoke: { | |
src: calculationMachine, | |
onDone: { | |
actions: (...args) => console.log('onDone', args), | |
}, | |
}, | |
}, | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment