Skip to content

Instantly share code, notes, and snippets.

@chenxsan
Last active November 28, 2019 14:05
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 chenxsan/fd9f5c0e148aff2520f43b7f6101b69a to your computer and use it in GitHub Desktop.
Save chenxsan/fd9f5c0e148aff2520f43b7f6101b69a 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 historyMachine = Machine({
id: 'history',
initial: 'idle',
context: {
history: ['https://blog.zfanw.com'],
index: 0,
},
states: {
idle: {
on: {
PUSH: {
target: 'idle',
actions: assign({
history: ({ history, index }, { url }) => {
if (history.length - 1 === index) {
return [...history, url]
} else {
return [...history.slice(0, index + 1), url]
}
},
index: ({ index }, event) => index + 1,
}),
},
FORWARD: {
target: 'idle',
actions: assign({
index: (context, event) => context.index + 1,
}),
cond: (context, event) => context.index < context.history.length - 1,
},
BACK: {
target: 'idle',
actions: assign({
index: (context, _event) => context.index - 1,
}),
cond: (context, event) => context.index > 0,
},
},
},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment