Beads
Beads is a Koa like, general purpose middleware layer for nodejs.
Usage
app;
// will be fired as soon as possibleapp;
// add wrap for middleware (adding logger, and log BEGIN and END)var { return { thislogger = ; thislogger; var { _next; thislogger = ; } middleware; thislogger; };};app;
Features
-
Simple dependency resolution system
Use app.when. See the example below.
Dependency Test -> Functions whose dependency was satisfied -> RUN those functions one by one -> Dependency Test
-
Shared context among middlewares
-
Use
co
andgenerators
for middlewares
Example
var context = {};var app = new context; app; app; app; app; app; app;
The output:
set mark1!
timer start
mark1 set, set mark2!
mark1 set!
mark2 set!
Output Context: {"mark1":true,"mark2":true}
timer end
2ms
Installation
$ npm install beads
Compatibility
You can use the --harmony
flag when using node 0.11+.
Or, you can use gnode if you are using node 0.10 or below.
FAQ
Why app.wrap(wrap)?
Because sometimes you may want to add wrapper for some middleware, add logger or profile. So it's useful to have app.wrap.
SyntaxError: Unexpected identifier
You can't use yield inside a regular function!
http://stackoverflow.com/questions/20834113/syntaxerror-unexpected-identifier-generators-in-es6
这目标于一种情况
每次依赖条件已解决,执行一批插件。
插件 A, B 依赖于条件1,插件 C, D 依赖于条件2。
B中会解决条件2。
那么A, B的顺序不确定,但是A, B都先于C, D执行。
如果不是成批执行,
若定义顺序为 B C D A 则实际运行顺序为
B C D A,A的条件本早已满足,却在最后被执行了。
为此依赖必须单独与函数体存在。
否则在执行函数前无法知道依赖是否满足,
但是执行函数可能改变依赖条件,
这样就无法实现无损的依赖判断。