Created
May 21, 2019 17:32
-
-
Save alizbazar/3fc0e76ab7060f30f35bfef225e719ed to your computer and use it in GitHub Desktop.
Using global events for local transitions causes services be re-invoked
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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