KOA中间件实现原理

Posted yzd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KOA中间件实现原理相关的知识,希望对你有一定的参考价值。

 1 //基本原理
 2 var empty=(function *(){})();
 3     //中间件3
 4     var mid2=function *(){
 5     console.log("2:before yield");
 6     yield empty;
 7     console.log("2:after yield");
 8 }
 9 //中间件2
10 var mid1=function *(){
11     console.log("1:before yield");
12     yield *(mid2());
13     console.log("1:after yield");
14 }
15 //中间件1
16 var start=(function *(){
17     console.log("0:before yield");
18     yield *(mid1());
19     console.log("0:after yield");
20 })();
21 while(!(start.next().done)){}

结果
0:before yield
1:before yield
2:before yield
2:after yield
1:after yield
0:after yield


//将数组里面的generator函数compose在一起
var mids=[
function *(next){
    console.log("0:before yield");
    yield 0;
    yield *next;
    yield 1;
    console.log("0:after yield");
 
},
function *(next){
    console.log("1:before yield");
    yield 2;
    yield *next;
    console.log("1:after yield");
    yield 3;
},
function *(next){
    console.log("2:before yield");
    yield 4;
    yield *next;
    yield 5;
    console.log("2:after yield");
}
];
 
var next = (function* (){})(),i=mids.length;
/* while (i--) {   next = mids[i].call(null, next);}}*/
for(;i>0;i--){  next=mids[i-1](next);}
var ret;
while(!(ret=next.next()).done){console.log(ret.value);} 


结果
0:before yield
0
1:before yield
2
2:before yield
4
5
2:after yield
1:after yield
3
1
0:after yield

 

//递归,使用next而非*next跳转到下一个中间件

var mids=[
function *(next){
    console.log("0:before yield");
    yield 0;
    yield next;
    yield 1;
    console.log("0:after yield");
 },
function *(next){
    console.log("1:before yield");
    yield 2;
    yield next;
    console.log("1:after yield");
    yield 3;
},
function *(next){
    console.log("2:before yield");
    yield 4;
    yield next;
    yield 5;
    console.log("2:after yield");
}
];
var next = (function* (){})(),i=mids.length;
for(;i>0;i--){  next=mids[i-1](next);}
 
function nextGenerator(generatorObj){
    var value,item;
    nextVal();
function nextVal(){
    item=generatorObj.next();
    if(item.done) {return;}
    value=item.value;
    if(isGenerator(value)){
    nextGenerator(value);
}else console.log(value);
    nextVal();
}
function isGenerator(obj) {
    return typeof obj.next==‘function‘ && typeof obj.throw==‘function‘;
}
}
nextGenerator(next);

结果

0:before yield
0
1:before yield
2
2:before yield
4
5
2:after yield
1:after yield
3
1
0:after yield

 1 //第二个中间件不跳转
 2 var mids=[
 3 
 4 function *(next){
 5     console.log("0:before yield");
 6     yield 0;
 7     yield next;
 8     yield 1;
 9     console.log("0:after yield");
10 },
11 function *(next){
12     console.log("1:before yield");
13     yield 2;
14     //yield next; 
15     console.log("1:after yield");
16     yield 3;
17 },
18 function *(next){
19     console.log("2:before yield");
20     yield 4;
21     yield next;
22     yield 5;
23     console.log("2:after yield");
24 }
25 ];

结果

0:before yield
0
1:before yield
2
1:after yield
3
1
0:after yield


 

以上是关于KOA中间件实现原理的主要内容,如果未能解决你的问题,请参考以下文章

Koa 系列 — Koa 中间件机制解析

Koa中间件(middleware)级联原理

koa2中间件koa和koa-compose源码分析原理

Koa源码解析,带你实现一个迷你版的Koa

有没有写过Koa中间件,说下中间件原理,介绍下自己写过的中间件

koa2 中间件里面的next到底是什么