Last active
May 6, 2020 04:07
-
-
Save jasir/2c4d22cd6ce45017508aef25f9be9bf5 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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) | |
const fetchMachine = Machine( | |
// ***** | |
{ | |
// | |
id: 'zolik', | |
initial: 'startup', | |
context: { | |
lcdHelpLine: '', | |
lcdInputLine: '', | |
editedNum: '', | |
editedPin: '', | |
pinsForThisUserEntered: 0, | |
lastActivity: new Date(), | |
}, | |
states: { | |
startup: { | |
entry: [ | |
assign({ lcdInputLine: ctx => 'Žolík v1.0' }), | |
assign({ lcdHelpLine: ctx => '(c) Altisima software' }), | |
], | |
on: { | |
STARTUP_FINISHED: { | |
target: 'idle', | |
}, | |
}, | |
}, | |
// normální flow | |
idle: { | |
entry: [ | |
assign({ lcdHelpLine: ctx => 'UID:' }), | |
assign({ lcdInputLine: ctx => 'Žolík 1.0' }), | |
assign({ editedNum: ctx => '' }), | |
assign({ editedPin: ctx => '' }), | |
], | |
on: { | |
KEY_DIGIT: { | |
target: 'idEntering', | |
actions: [ | |
assign({ editedNum: (ctx, event) => event.digit?.toString() || '1' }), | |
assign({ lcdInputLine: ctx => ctx.editedNum + '_' }), | |
], | |
}, | |
}, | |
}, | |
logged: { | |
entry: [ | |
'setLastActivity', | |
assign({ lcdInputLine: ctx => '1)ŽOLÍK 2)KONTA' }), | |
assign({ lcdHelpLine: ctx => 'Aleš Brumlík' }), | |
], | |
on: { | |
KEY_1: 'printZolik', | |
KEY_2: 'printStavKonta', | |
TIMEOUT_LOGOUT: 'onTimerOut', | |
TIMEOUT: { | |
target: 'onTimerOut', | |
}, | |
PRINT_ZOLIK: 'printZolik', | |
PRINT_STAV_KONTA: 'printStavKonta', | |
PRINT_FINISHED: 'logged', | |
}, | |
}, | |
printZolik: { | |
invoke: [{ | |
id: 'printZolik', | |
src: (context, event) => sleep(2000), | |
onDone: [{ | |
target: 'logged', | |
}], | |
}], | |
entry: [ | |
'setLastActivity', | |
assign({ lcdInputLine: ctx => 'ČEKEJTE, PROSÍM' }), | |
assign({ lcdHelpLine: ctx => 'Tisknu žolíka' }), | |
], | |
on: { | |
PRINT_FINISHED: 'logged', | |
TIMEOUT: 'logged', | |
KEY_1: { target: 'logged' }, | |
}, | |
}, | |
printStavKonta: { | |
on: { | |
PRINT_FINISHED: 'logged', | |
}, | |
}, | |
// idEntering | |
idEntering: { | |
entry: [ | |
'setLastActivity', | |
assign({ lcdInputLine: ctx => ctx.editedNum + '_' }), | |
assign({ lcdHelpLine: ctx => 'UID:' }), | |
], | |
on: { | |
USERID_CORRECT: { | |
target: 'pinEntering', | |
}, | |
USERID_UNKNOWN: { | |
target: 'userNotFound', | |
}, | |
TIMEOUT: { | |
target: 'idle', | |
}, | |
KEY_DIGIT: { | |
target: 'idEntering', | |
actions: assign({ editedNum: (ctx, event) => ctx.editedNum + event.digit }), | |
}, | |
KEY_DELETE: { | |
target: 'idEntering', | |
actions: assign({ editedNum: ctx => '' }), | |
}, | |
KEY_BACKSPACE: { | |
target: 'idEntering', | |
actions: assign({ editedNum: ctx => ctx.editedNum.substr(0, ctx.editedNum.length - 1) }), | |
}, | |
KEY_ENTER: { | |
target: 'pinEntering', | |
}, | |
KEY_RESET: { | |
target: 'idEntering', | |
actions: assign({ editedNum: ctx => '' }), | |
}, | |
KEY_HELP: {}, | |
}, | |
}, | |
pinEntering: { | |
entry: [ | |
'setLastActivity', | |
assign({ lcdInputLine: ctx => '*'.repeat(ctx.editedPin.length) + '_' }), | |
assign({ lcdHelpLine: ctx => 'PIN:' }), | |
], | |
on: { | |
PIN_CORRECT: { | |
target: 'logged', | |
}, | |
PIN_MAX_TRIES: 'pinMaxTriesOut', | |
TIMEOUT: { | |
target: 'idle', | |
}, | |
KEY_DIGIT: { | |
target: 'pinEntering', | |
actions: assign({ editedPin: (ctx, event) => ctx.editedPin + event.digit }), | |
}, | |
KEY_DELETE: { | |
target: 'pinEntering', | |
actions: assign({ editedPin: ctx => '' }), | |
}, | |
KEY_BACKSPACE: { | |
target: 'pinEntering', | |
actions: assign({ editedPin: ctx => ctx.editedPin.substr(0, ctx.editedPin.length - 1) }), | |
}, | |
KEY_ENTER: { | |
target: 'logged', | |
}, | |
KEY_RESET: { | |
target: 'pinEntering', | |
actions: assign({ editedNum: ctx => '' }), | |
}, | |
}, | |
}, | |
userNotFound: { | |
on: { | |
TIMEOUT: { | |
target: 'idle', | |
}, | |
}, | |
}, | |
// noPaper | |
noPaper: {}, | |
onTimerOut: { | |
entry: [ | |
'setLastActivity', | |
assign({ lcdInputLine: ctx => 'Automatické...' }), | |
assign({ lcdHelpLine: ctx => 'odhlášení' }), | |
], | |
on: { | |
TIMEOUT: 'idle', | |
KEY_DIGIT: 'idle', | |
}, | |
}, | |
pinMaxTriesOut: { | |
on: { | |
TIMEOUT: 'idle', | |
KEY_DIGIT: 'idEntering', | |
}, | |
}, | |
}, | |
// | |
}, | |
{ | |
actions: { | |
setLastActivity: assign({ lastActivity: ctx => new Date() }), | |
}, | |
} | |
// ***** | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment