genery
ES6 Generator flow engine, goodies, express and mocha generator wrapper
Description
Genery rely on Generator and Promise introduced by ES6 and available in nodeJs from version 0.12
Genery provides a generator flow engine that aims to execute a generator function, where yielded functions are Promised
var { ; ; // etc... } ;
Genery return a Promise from the generator (that can be yielded again,...):
var promiseOfMyGenerator = ;promiseOfMyGenerator ;
In addition, genery provides some goodies to run your generator:
- Support of Continuation Local Storage
- Generator based all, map, reduce and filter
- Express generator wrapper, allow usage of generator instead of express callback
- Mocha generator wrapper, allow usage of generator and yield in unit testing
Installation
npm install genery
Before Starting
All the example bellow are available in the folder test/doc.js in Git
Getting Started
The gerery API must be used as follow to execute a generator that yield promise:
var g = // A dummy promise to executevar { return { ; };}; // now execute a generator that use the Promise;
the 'g' function take a generator function in input and will 'orchestrate' the execution of the generator function for you and return back a promise.
How to pass arguments to the generator
It is usefull to pass some arguments to the generator function to executem to do such genery allow to pass arguments that will be used to execute the generator function.
var { var result1 = ; var result2 = ; return result1+' '+result2;}; ;
it is equivalent to:
var { var result1 = ; var result2 = ; return result1+' '+result2;};
API
var promise = g(function *, [arg1, ...])
how to execute generator function in parallel
Genery provides a function 'all' that takes in parameter a list of generator function
Genery will run them in parralel, and return a promise
Once this promise executed, it return an array containing the execution result of each generator function
var { var result = ; return result;}; var { var result = ; return result;}; // orgallgeneratorFct1 generatorFct2;
API
var promise = g.all(Array of function *)
how to use map/reduce
var { var result = ; return result;}; var { var result = ;}
API
var promise = g.map(Array to map, mapper function *)
var promise = g.reduce(Array of result to reduce, reducer function *)
how to use filter
The filter() method filter array value using a generator function
var { return { if param > 4 ; else ; };}; var { var result = ; return result;}; g// output [10, 13, 14, 16, 7, 9]
var promise = g.map(Array to filter, filter function *)
how to use call
The call() method calls a generator function with a given this value and arguments provided individually.
var context = param1: 'hello' param2: ' genery' g
API
var promise = g.call(context, function *, [arg1, ...])
context management / Continuation Local Storage
The drawback with yield * is the 'this' of the caller is lost.
To overcome this limitation, genery manage a global context.
Once the g() or g.call() is invoke, genery 'save' the this and assign it to a global variable before each call to the next() function of the generator.
The global context can be access using g.currentContext
To explain it simply, it provides a 'Continuation Local Storage' (also known as Thread Local in Java: https://en.wikipedia.org/wiki/Thread-local_storage)
Main usage of this functionality is to handle context information for logging accross inner call
var logger = { console; }; // a dumy promise that log using the contextvar { logger; return { ; };}; var { logger; ; process; logger; ;}; var { logger; ;}; var { logger; process;}; let context = logInfo: sessionId: 2354 event: 'PROCESS' g; will output:INFO "sessionId":2354"event":"PROCESS" call sub generator 1INFO "sessionId":2354"event":"PROCESS" call sub-sub generator INFO "sessionId":2354"event":"PROCESS" call processINFO "sessionId":2354"event":"PROCESS" processINFO "sessionId":2354"event":"PROCESS" processINFO "sessionId":2354"event":"PROCESS" call sub generator 2INFO "sessionId":2354"event":"PROCESS" call sub-sub generator INFO "sessionId":2354"event":"PROCESS" call processINFO "sessionId":2354"event":"PROCESS" process
API
var context = g.currentContext
Express wrapper
You can ask genery to wrapp express to allow the use of generator as follow:
var app = ;var g_app = g
now we can use the wrapper as follow
server = g_app; g_app; // etc...
here is the list of express API that support generator:
API
g_app.METHOD(path, function * [, function * ...])
Method supported:
- checkout
- connect
- copy
- delete
- get
- head
- lock
- merge
- mkactivity
- mkcol
- move
- m-search
- notify
- options
- patch
- post
- propfind
- proppatch
- purge
- put
- report
- search
- subscribe
- trace
- unlock
- unsubscribe
in addition:
app.use([path,] function * [, function *...])
app.engine(ext, callback)
mocha
Genery provides a wrapping of mocha to use generator in unit test.
var g = ;var mocha = gmocha;;