Skip to content

Instantly share code, notes, and snippets.

@send2moran
Created March 21, 2020 16:44
Show Gist options
  • Save send2moran/9b492fed1a17656343e7ea4578edfdaf to your computer and use it in GitHub Desktop.
Save send2moran/9b492fed1a17656343e7ea4578edfdaf to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const autocompleteMachine = Machine({
id: "autocomplete",
type: "parallel",
context: {
hits: [],
highlightedIndex: null
},
states: {
searchBox: {
initial: "idle",
states: {
idle: {
entry: ["reset", "hello"],
on: { INPUT: "debounce" }
},
debounce: {
states: {
debouncing: {
invoke: {
id: "debounce",
src: (_, { data: { query } }) =>
new Promise(resolve => {
setTimeout(() => resolve(query), 1500);
}),
onDone: {target: "#autocomplete.searchBox.searching", actions: ["search"]}
}
},
debouncerestart: {
on: {
"": "debouncing"
}
},
settled: {
on: {
// SETTLED: {target: "#autocomplete.searchBox.searching"}
}
}
},
initial: "debouncing",
on: {
INPUT: ".debouncerestart"
}
},
searching: {
// entry: ["search"],
on: {
FETCHED: "success",
RESET_SEARCH: "idle"
}
},
success: {
entry: ["setHits", "openOrCloseDropdown"],
on: { INPUT: "debounce", RESET_SEARCH: "idle" }
}
}
},
dropdown: {
initial: "closed",
states: {
closed: {
on: { OPEN: "opened" }
},
opened: {
on: { CLOSE: "closed" }
}
}
},
highlight: {
initial: "none",
states: {
none: {
entry: ["resetHighlightedIndex"],
on: {
HIGHLIGHT_NEXT: { target: "highlighted", cond: "hasHits" },
HIGHLIGHT_PREV: { target: "highlighted", cond: "hasHits" }
}
},
highlighted: {
entry: ["updateHighlightedIndex"],
on: {
HIGHLIGHT_NEXT: "highlighted",
HIGHLIGHT_PREV: "highlighted",
RESET_HIGHLIGHT: "none"
}
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment