Skip to content

Instantly share code, notes, and snippets.

@pangratz
Last active March 30, 2020 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pangratz/0abfea1330ba80a50c43f64b2c7557a7 to your computer and use it in GitHub Desktop.
Save pangratz/0abfea1330ba80a50c43f64b2c7557a7 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: 'fetch',
initial: 'active',
context: {
past: [],
current: null,
queue: [1, 2, 3]
},
on: {
CREATE: {
actions: [assign({
queue: context => [...context.queue, Math.random()]
}), send("CREATED")]
},
CREATE_NEXT: {
actions: [assign({
queue: context => [Math.random(), ...context.queue]
}), send("CREATED")]
},
MOVE: {
actions: send("MOVE")
}
},
states: {
active: {
type: "parallel",
states: {
past: {
initial: "empty",
states: {
empty: {
on: {
"": {
target: "filled",
cond: "hasPast"
}
}
},
filled: {}
}
},
current: {
initial: "empty",
states: {
empty: {
on: {
"": {
target: "filled",
cond: "hasCurrent"
}
}
},
filled: {
on: {
MOVE: {
target: "empty",
actions: assign({
current: context => null,
past: context => [...context.past, context.current]
}),
cond: "hasCurrent"
}
}
}
}
},
queued: {
initial: "empty",
states: {
empty: {
on: {
"": {
target: "filled",
cond: "hasNext"
}
}
},
filled: {
on: {
MOVE: {
target: "empty",
actions: assign({
current: context => context.queue[0],
queue: context => context.queue.slice(1)
}),
cond: "hasNext"
}
}
}
}
}
}
}
}
}, {
guards: {
hasCurrent: context => !!context.current,
hasNoCurrent: context => !context.current,
hasNext: context => context.queue.length > 0,
hasPast: context => context.past.length > 0
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment