Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alizbazar/3fc0e76ab7060f30f35bfef225e719ed to your computer and use it in GitHub Desktop.
Save alizbazar/3fc0e76ab7060f30f35bfef225e719ed to your computer and use it in GitHub Desktop.
Using global events for local transitions causes services be re-invoked
let i = 0
Machine({
on: {
ERROR: 'restarting',
},
invoke: {
src: 'subscribeToStatus',
},
initial: 'running',
states: {
running: {},
restarting: {
after: {
1000: 'running',
},
},
},
}, {
services: {
subscribeToStatus: () => sendParent => {
const id = i
i++
console.log('Attached service', id)
const timer = setInterval(() => {
sendParent({ type: 'PING' })
}, 10000)
return () => {
clearInterval(timer)
console.log('Detached service', id)
}
},
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment