Last active
February 11, 2020 21:50
-
-
Save MilanLempera/b1f189fe7128e2c041122329dd618273 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 START_CHAR = '-'; | |
| const END_CHAR = '-'; | |
| const CONTROL_SEQUENCE_COUNT = 3; | |
| const CONTROL_SEQUENCE_TIMEOUT = 500; | |
| const DATA_TIMEOUT = 2000; | |
| const scannerMachine = Machine( | |
| { | |
| id: 'scannerMachine', | |
| initial: 'waitingForChars', | |
| context: { | |
| code: '', | |
| startCharCount: CONTROL_SEQUENCE_COUNT, | |
| endCharCount: CONTROL_SEQUENCE_COUNT, | |
| }, | |
| states: { | |
| waitingForChars: { | |
| entry: assign({ | |
| code: '', | |
| startCharCount: CONTROL_SEQUENCE_COUNT, | |
| endCharCount: CONTROL_SEQUENCE_COUNT, | |
| }), | |
| on: { | |
| KEYPRESS: { target: 'startSeq', cond: 'isStartChar' }, | |
| }, | |
| }, | |
| startSeq: { | |
| entry: assign({ | |
| startCharCount: ({ startCharCount }) => startCharCount - 1, | |
| }), | |
| on: { | |
| KEYPRESS: [ | |
| { | |
| target: 'startSeq', | |
| cond: 'isStartChar', | |
| }, | |
| { target: 'waitingForChars' }, | |
| ], | |
| '': { target: 'receiving', cond: 'isStarted' }, | |
| }, | |
| after: { | |
| [CONTROL_SEQUENCE_TIMEOUT]: 'waitingForChars', | |
| }, | |
| }, | |
| receiving: { | |
| entry: assign({ | |
| startCharCount: 3, | |
| endCharCount: 3, | |
| }), | |
| on: { | |
| KEYPRESS: [ | |
| { | |
| target: 'endSeq', | |
| cond: 'isEndChar', | |
| }, | |
| { | |
| target: 'receiving', | |
| actions: assign({ | |
| code: ({ code }, { key }) => code + key, | |
| }), | |
| }, | |
| ], | |
| }, | |
| after: { | |
| [DATA_TIMEOUT]: 'waitingForChars', | |
| }, | |
| }, | |
| endSeq: { | |
| entry: assign({ | |
| endCharCount: ({ endCharCount }) => endCharCount - 1, | |
| }), | |
| on: { | |
| KEYPRESS: [ | |
| { | |
| target: 'endSeq', | |
| cond: 'isEndChar', | |
| }, | |
| { | |
| target: 'receiving', | |
| actions: assign({ | |
| code: ({ code, endCharCount }, { key }) => code + '\\'.repeat(CONTROL_SEQUENCE_COUNT - endCharCount) + key, | |
| }), | |
| }, | |
| ], | |
| '': { | |
| target: 'received', | |
| cond: 'isEnded', | |
| }, | |
| }, | |
| after: { | |
| [CONTROL_SEQUENCE_TIMEOUT]: 'waitingForChars', | |
| }, | |
| }, | |
| received: { | |
| entry: 'codeReceived', | |
| on: { | |
| '': 'waitingForChars', | |
| }, | |
| }, | |
| }, | |
| }, | |
| { | |
| guards: { | |
| isStartChar: (_, { key }) => key === START_CHAR, | |
| isEndChar: (_, { key }) => key === END_CHAR, | |
| isStarted: ({ startCharCount }) => startCharCount === 0, | |
| isEnded: ({ endCharCount }) => endCharCount === 0, | |
| }, | |
| actions: { | |
| codeReceived: console.log.bind(console, 'codeReceived'), | |
| }, | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment