toffeescript-coroutines
v0.1.0
Published
Coroutines in ToffeeScript
Downloads
37
Maintainers
Readme
Coroutines in ToffeeScript
A simple coroutine construct over ToffeeScript's Continuation Passing Style Transform
{pause, wait, update} = require 'coroutines.coffee'
setInterval update, 1000 / 60 # run the scheduler
#requestAnimationFrame update
it_pauses = ->
loop
do somethingEveryFrame
pause! # continue in next frame
it_waits = ->
loop
for thing in things
do thing.action
wait! 100 # continue after 100 milliseconds
it_flows = ->
# a coro that can optionally be waited on by another
one = (nxt) ->
wait! 1000
console.log 'one'
nxt?()
two = ->
one! # wait for one to finish
console.log 'two'
do two # after a second, prints "one", then "two"