Skip to content

Instantly share code, notes, and snippets.

@gmaclennan
Last active February 21, 2020 12:09
Show Gist options
  • Save gmaclennan/d38ea59167a5ac15b3170a682247610a to your computer and use it in GitHub Desktop.
Save gmaclennan/d38ea59167a5ac15b3170a682247610a 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 indexMachine = Machine({
id: 'indexer',
initial: 'idle',
context: {
totalBlocks: 200,
indexedBlocks: 150,
prevIndexedBlocks: 120,
indexStartTime: Date.now(),
error: null
},
states: {
idle: {
on: {
START: {
target: 'indexing',
actions: assign({
indexStartTime: Date.now(),
prevIndexedBlocks: (context) => context.indexedBlocks,
totalBlocks: (context, event) => event.newTotalBlocks || context.totalBlocks
})
},
PAUSE: 'paused'
}
},
indexing: {
on: {
PROGRESS: {
target: 'indexing',
actions: assign({
indexedBlocks: (context, event) => context.indexedBlocks + (event.indexedBlocks || 1),
totalBlocks: (context, event) => event.newTotalBlocks || context.totalBlocks
})
},
COMPLETE: 'idle',
ERROR: {
target: 'error',
actions: assign({
error: (context, event) => event.error
})
},
PAUSE: 'paused'
}
},
paused: {
on: {
UNPAUSE: 'idle'
}
},
error: {
on: {
START: {
target: 'indexing',
actions: assign({
indexStartTime: Date.now()
})
},
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment