Skip to content

Instantly share code, notes, and snippets.

@schpet
Created July 27, 2021 22:21
Show Gist options
  • Save schpet/2e1c9028cf122b12b76859063deeb3f6 to your computer and use it in GitHub Desktop.
Save schpet/2e1c9028cf122b12b76859063deeb3f6 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: "animation",
initial: "pending",
context: {
staleUsers: null,
currentUsers: null,
},
states: {
// loading the stale list is an async operation
pending: {
invoke: {
src: "readStaleList",
onDone: {
target: "staleListLoaded",
actions: assign({
staleUsers: (_context, event) => event.data,
}),
},
},
},
// while we wait for the new list of users to come in,
// we show the stale list
staleListLoaded: {
on: {
UPDATE_CURRENT_USERS: {
target: "animatingStaleListOut",
actions: assign({
currentUsers: (_ctx, event) => event.users,
}),
},
},
},
// fade out people not in new list, move the people to their new positions
animatingStaleListOut: {
invoke: {
src: "animateStaleOut",
onDone: {
target: "animatingCurrentListIn",
},
},
},
animatingCurrentListIn: {
invoke: [
{ src: "animateCurrentIn", onDone: "static" },
{ src: "persistCurrentListAsStale" },
],
},
// change state so that stale and current are equal, this should
// not trigger an animation
static: {
on: {
UPDATE_CURRENT_USERS: {
target: "animatingStaleListOut",
actions: assign({
staleUsers: (ctx) => ctx.currentUsers,
currentUsers: (_ctx, event) => event.users,
}),
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment