• 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


  • 相关阅读:
    如何让Surface RT支持网站的flash
    C# 线程同步
    WPF中Visible设为Collapse时,VisualTreeHelper.GetChildrenCount为0
    Set connectionId threw an exception.
    C# 深化基本概念
    wpf如何获取control template里的元素
    DataGrid当列宽超出当前宽度时,没有数据也恒有滚动条
    利用MEF实现插件机制(可根据输入类型来加载特定dll)
    利用正则表达式类解析SQL语句,达到Worklist兼容各个RIS数据库的目的
    Mysql 创建存储过程 更新表
  • 原文地址:https://www.cnblogs.com/godghdai/p/6917130.html
Copyright © 2020-2023  润新知