Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created June 22, 2017 00:49
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 Gozala/5ec6ef89ee3ab7deecdf453f6e8c70dc to your computer and use it in GitHub Desktop.
Save Gozala/5ec6ef89ee3ab7deecdf453f6e8c70dc to your computer and use it in GitHub Desktop.
var {Task} = require('./Task')
var interval = (frame, mailbox) => {
let next
let id
const handler = (time=Date.now()) => {
if (log) {
console.log('> handler', fork && fork.abortHandle)
}
next({ time })
}
const init = () => new Task((succeed, fail) => {
id = setInterval(handler, frame)
succeed()
})
const wait = () => new Task(succeed => {
if (log) {
console.log('> wait', id == null, fork && fork.abortHandle)
}
next = succeed
return id
}, clearInterval)
const loop = (message) => {
if (log) {
console.log('> loop', fork && fork.abortHandle)
}
return Task.send(mailbox, message).chain(wait).chain(loop)
}
return init().chain(wait).chain(loop)
}
var fork = Task.fork(interval(3000, msg => console.log(`>>>`, msg)), a => console.log('!!!', a), e => console.error(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment