Functional programming for generators
Higher order flow control functions for creating generators
Compose coroutines and koa middleware using higher order functions!
npm install shen
There's still alot of work to be done on this repo. I'm putting it out there for people to try out early and let you all get a taste of what's to come.
If you'd like to contribute, I'd love ideas for things to impliment. Its a brave new world so lets find out what makes sense! Tests are also very much appreciated.
you can run the tests supplied with
npm installmocha --harmony test
Make sure to have node 0.11 installed or you'll be a sad panda!
Generators are the new concurrency primative slated for ecmascript 6. There is scant information on how generators work. The one really useful library I've seen out there is TJHollowaychuck's co module.
var co = require'co';covar data = yield someAsyncTask;doStuffWithDatadata;return data;;
Shen is a set of tools for constructing generators from smaller generators. Through composition, Shen allows you to assemble coroutines from smaller coroutines. The asychronous code even looks synchronous and the shnozberries taste like shnozberries!
They are all nestable so you can build arbitrarily large generators from smaller ones like lego pieces.
Takes a generator and binds it to the context, optional args are partially applied to the returned generator.
var shen = require'shen';var boundGenerator = shenbindcontext//etc;
Takes one or more generators and returns a new generator that can be passed into a coroutine function. each generator is passed a next which allows you to yield downstream to the next generator on the list.
it'should pass returned values to the next function'var list = ;var genFunc = shencascadelistpush1;var b = yield next;listpush3;return b;shencascadelistpush2;return yield next;listpush7;return 'ret';;cogenFunclistshouldhaveproperty'0' 1;listshouldhaveproperty'1' 2;listshouldhaveproperty'2' 7;listshouldhaveproperty'3' 3;valshouldequal'ret';done;;;
Takes a three generators. The first one recieves links to the other two. With this you can set up conditional branches to either path1 or path2.
it'should yield to the second argument'var stub;var genFunc = shenbranchyield path1;stubshouldequal5;stub = 5;stub = 6;;cogenFuncdone;;it'should yield to the third argument'var stub;var genFunc = shenbranchyield path2;stubshouldequal6;stub = 5;stub = 6;;cogenFuncdone;;
Takes a generator and an object of genertors. Inside gen, you can yield control to one of the generators inside the map.
it'should dispatch to a branch'var genFunc = shendispatchvar foo = yield paths'fuu';return foo;return 'yeaaaa';;cogenFuncvalueshouldequal'yeaaaa';done;;;
Takes one or more generators. All the generators are run asychronosly. The returned values can be returned upstream into an array yelded from the generators yielding to the parallel generator.
var genFunc = shencascadevar vals = yield next;return vals;shenparallelreturn yield request''then//console.log('wierd', arguments);return arguments01;;return 'two';;cogenFuncvar countOne = !!val0match'google';countOneshouldbetrue;var countTwo = val1 === 'two';countTwoshouldbetrue;done;;
Takes a generator and timeout. it returns a generator that runs the passed in generator after the timeout.
it'should delay timeout'thistimeout10000;var timeStamp = Datenow;//defer = Promise.defer();var start = Datenow;//defer.resolve('resolved');varvar delay = Datenow - timeStamp;return delay;;coshendelaygenFunc 3000delayshouldbegreaterThan2950;done;;;
Takes a generator and returns a generator which returns an eventEmitter that calls a function with the return values from calling gen at specified intervals.
it'should run run the function over and over'thistimeout10000;var i = 0;var i++; ;var genFunc = shenoscillatortest;500;cogenFunc;setTimeoutishouldbegreaterThan5;done;6000;;xit'should let me stop the oscillation with stopTick()'thistimeout10000;var i = 0;var i++; ;var genFunc = shenoscillatortest;500;cogenFunc;setTimeoutishouldbegreaterThan5;done;6000;;
You can compose together koa middleware using shen functions. The follow example shows how returns up the middleware chain are gracefully passed up.
var koa = require'koa';var app = koa;var shen = require'shen';appusethisstatus = 200;thisbody = yield next;;var foo = shencascadereturn yield next + 6;shenbranchreturn yield path2+ 5;//this never runsreturn yield next;shencascadereturn yield next + 4;return yield next + 3;return yield next + 2;return yield next + 1;;appusefoo;appusereturn "hello world ";;console.log'now listening on port 3000';applisten3000;
You should see 'hello world 123456' in your browser!