Created
December 6, 2009 01:39
-
-
Save fearphage/249972 to your computer and use it in GitHub Desktop.
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
/* | |
This is a simplified sample of an idea I had today inspired by the fab | |
project. Event loops currently suck... so I've built this wonky DSL for you. | |
I'll release a real library later that actually implements some not so easy | |
things. | |
*/ | |
(function() { | |
var loops; | |
loops = {}; | |
window.loop = loop; | |
function loop(name) { | |
var L, table, queue; | |
if (loops[name]) return loops[name]; | |
table = {}; | |
queue = []; | |
L = function() { | |
while (queue.length > 0) { | |
L.call.apply(this,queue.shift()); | |
} | |
return L; | |
}; | |
L.def = function(name, fn) { | |
table[name] = fn; | |
return L.def; | |
}; | |
L.def.then = L; | |
L.call = function(name) { | |
table[arguments[0]].apply(this, Array.prototype.slice.call(arguments, 1)); | |
return L.call; | |
}; | |
L.call.then = L; | |
L.cast = function() { | |
queue.push(arguments); | |
return L.cast; | |
}; | |
L.cast.then = L; | |
L.run = L; | |
loops[name] = L; | |
return L; | |
} | |
})(); | |
var demo = loop("demo"); | |
demo.def | |
("show", print) // replace this with whatever you use to show stuff | |
("count_down", function(y) { | |
if (y < 0) { | |
demo.call("finish"); | |
} else { | |
demo.cast | |
("show", y) | |
("count_down", y - 1); | |
} | |
}) | |
("finish", function() { | |
demo.call("show", "Done"); | |
}) | |
.then.cast | |
("count_down", 42) | |
.then.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment