Created
June 22, 2017 00:49
-
-
Save Gozala/5ec6ef89ee3ab7deecdf453f6e8c70dc to your computer and use it in GitHub Desktop.
This file contains 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
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