Last active
October 8, 2019 11:35
-
-
Save valscion/a3c60b6fa5823d2049fd73e5b8be8537 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
const handleRequestSuccess = assign({ | |
results: (_ctx, event) => event.results | |
}); | |
const handleHistoryPopState = assign({ | |
results: (_ctx, event) => event.results | |
}); | |
const searchFSM = Machine({ | |
id: 'search', | |
initial: 'boot', | |
context: { | |
results: [] | |
}, | |
states: { | |
boot: { | |
on: { | |
'': { | |
target: 'idle', | |
actions: ['replaceHistoryState'] | |
} | |
} | |
}, | |
idle: { | |
on: { | |
HISTORY_POPSTATE: { | |
actions: [handleHistoryPopState] | |
}, | |
CHANGE_TEXT_INPUT: 'debouncing', | |
CHANGE_OPTION: 'fetching' | |
} | |
}, | |
debouncing: { | |
enter: ['startDebounceTimer'], | |
exit: ['clearDebounceTimer'], | |
on: { | |
HISTORY_POPSTATE: { | |
target: 'idle', | |
actions: [handleHistoryPopState] | |
}, | |
CHANGE_TEXT_INPUT: 'debouncing', | |
DEBOUNCE_TIMER: 'fetching', | |
CHANGE_OPTION: 'fetching' | |
} | |
}, | |
fetching: { | |
exit: ['clearSpinnerTimer', 'clearPendingRequest'], | |
entry: ['startRequest', 'startSpinnerTimer'], | |
on: { | |
CHANGE_TEXT_INPUT: 'debouncing', | |
CHANGE_OPTION: 'fetching', | |
HISTORY_POPSTATE: { | |
target: 'idle', | |
actions: [handleHistoryPopState] | |
}, | |
SEARCH_REQUEST_SUCCESS: { | |
target: 'idle', | |
actions: [handleRequestSuccess] | |
}, | |
SEARCH_REQUEST_FAILURE: { | |
target: 'error', | |
actions: ['handleRequestFailure'] | |
} | |
}, | |
initial: 'start', | |
states: { | |
start: { | |
on: { | |
TIMER: 'spinner' | |
} | |
}, | |
spinner: { | |
type: 'final' | |
} | |
} | |
}, | |
error: { | |
on: { | |
'': { | |
target: 'idle', | |
actions: ['logRequestError'] | |
} | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment