Skip to content

Instantly share code, notes, and snippets.

@hosmelq
Last active June 29, 2020 16:14
Show Gist options
  • Save hosmelq/45cfe46233817cd254e1e2056f391f35 to your computer and use it in GitHub Desktop.
Save hosmelq/45cfe46233817cd254e1e2056f391f35 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const {cancel} = actions
Machine(
{
id: `searchBox`,
initial: `idle`,
context: {
query: ``,
results: [],
},
states: {
idle: {
on: {
TYPE: {
actions: [`assignQuery`, `cancelSearch`, `sendSearchEvent`],
},
SEARCH: {
cond: `searchValid`,
target: `searching`,
},
},
},
searching: {
invoke: {
id: `search`,
src: `search`,
onDone: {
actions: `setResults`,
target: `idle`,
},
},
},
},
},
{
actions: {
assignQuery: assign({
query: (ctx, e) => e.query || `test`
}),
cancelSearch: cancel(`debouncedSearch`),
sendSearchEvent: send(`SEARCH`, {
delay: 2500,
id: `debouncedSearch`,
}),
setResults: assign({
results: (ctx, e) => [{}, {}]
}),
},
guards: {
searchValid: (ctx) => ctx.query.trim().length > 0,
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment