Skip to content

Instantly share code, notes, and snippets.

@valscion
Last active September 24, 2019 11:52
Show Gist options
  • Save valscion/ad21cf41123bd4e6fba4264e23c0b013 to your computer and use it in GitHub Desktop.
Save valscion/ad21cf41123bd4e6fba4264e23c0b013 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const handleRequestSuccess = assign({
results: (_ctx, event) => event.results
});
const handleHistoryPopState = assign({
results: (_ctx, event) => event.results
});
const searchStateMachine = Machine({
id: 'search',
initial: 'boot',
context: {
results: []
},
states: {
boot: {
on: {
'': {
target: 'idle',
actions: ['replaceHistoryState', 'hookToHistory']
}
}
},
idle: {
on: {
HISTORY_POPSTATE: {
actions: [handleHistoryPopState]
},
CHANGE_TEXT_INPUT: 'debouncing',
CHANGE_OPTION: 'fetching'
}
},
debouncing: {
entry: ['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