Last active
October 18, 2020 12:15
-
-
Save cmd-save/9ac78c0f3923932cf08a89dbcfdedcd2 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) | |
| const searchStateChart = { | |
| id: 'search', | |
| initial: 'idle', | |
| context: { | |
| result: {} | |
| }, | |
| on: { | |
| SEARCH: [ | |
| { | |
| target: 'searching', | |
| cond: { | |
| type: 'search query has more than one character' | |
| } | |
| }, | |
| { | |
| target: 'idle', | |
| actions: ['resetSearchResult'] | |
| } | |
| ] | |
| }, | |
| states: { | |
| idle: {}, | |
| searching: { | |
| invoke: { | |
| src: 'searchService', | |
| onDone: { | |
| target: 'loaded', | |
| actions: ['storeResult'] | |
| }, | |
| onError: { | |
| target: 'failure' | |
| } | |
| } | |
| }, | |
| loaded: {}, | |
| failure: {} | |
| } | |
| }; | |
| const machineConfig = { | |
| guards: { | |
| 'search query has more than one character' : (context, event) => { | |
| return event.q.length >=2 | |
| } | |
| }, | |
| services: { | |
| searchService: (_, event) => { | |
| return search(event.entity, { | |
| q: event.q | |
| }) | |
| } | |
| }, | |
| actions: { | |
| storeResult: assign({ | |
| result: (_, event) => { | |
| return event.data.items | |
| } | |
| }), | |
| resetSearchResult: assign({ | |
| result: () => [] | |
| }) | |
| } | |
| }; | |
| const searchStateMachine = Machine(searchStateChart, machineConfig); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment