toffeescript-coroutines

0.1.0 • Public • Published

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"

Package Sidebar

Install

npm i toffeescript-coroutines

Weekly Downloads

0

Version

0.1.0

License

ISC

Last publish

Collaborators

  • coreytrampe