這篇文章主要為大家展示了“koa中間件處理模塊koa-compose怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“koa中間件處理模塊koa-compose怎么用”這篇文章吧。
成都創(chuàng)新互聯(lián)始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營理念,通過多達(dá)十多年累計(jì)超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)推廣解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶,其中包括:成都軟裝設(shè)計(jì)等企業(yè),備受客戶贊譽(yù)。
koa-compose看起來代碼少,但是確實(shí)繞。閉包,遞歸,Promise。。??戳艘槐槟X子里繞不清楚??戳司W(wǎng)上幾篇解讀文章,都是針對(duì)單行代碼做解釋,還是繞不清楚。最后只好采取一種傻瓜的方式:
koa-compose去掉一些注釋,類型校驗(yàn)后,源碼如下:
function compose (middleware) { return function (context, next) { // last called middleware # let index = -1 return dispatch(0) function dispatch (i) { if (i <= index) return Promise.reject(new Error('next() called multiple times')) index = i let fn = middleware[i] if (i === middleware.length) fn = next if (!fn) return Promise.resolve() try { return Promise.resolve(fn(context, dispatch.bind(null, i + 1))); } catch (err) { return Promise.reject(err) } } } }
寫出如下代碼:
var index = -1; function compose() { return dispatch(0) } function dispatch (i) { if (i <= index) return Promise.reject(new Error('next() called multiple times')) index = i var fn = middleware[i] if (i === middleware.length) fn = next if (!fn) return Promise.resolve('fn is undefined') try { return Promise.resolve(fn(context, dispatch.bind(null, i + 1))); } catch (err) { return Promise.reject(err) } } function f1(context,next){ console.log('middleware 1'); next().then(data=>console.log(data)); console.log('middleware 1'); return 'middleware 1 return'; } function f2(context,next){ console.log('middleware 2'); next().then(data=>console.log(data)); console.log('middleware 2'); return 'middleware 2 return'; } function f3(context,next){ console.log('middleware 3'); next().then(data=>console.log(data)); console.log('middleware 3'); return 'middleware 3 return'; } var middleware=[ f1,f2,f3 ] var context={}; var next=function(context,next){ console.log('middleware 4'); next().then(data=>console.log(data)); console.log('middleware 4'); return 'middleware 4 return'; }; compose().then(data=>console.log(data));
直接運(yùn)行結(jié)果如下:
"middleware 1"
"middleware 2"
"middleware 3"
"middleware 4"
"middleware 4"
"middleware 3"
"middleware 2"
"middleware 1"
"fn is undefined"
"middleware 4 return"
"middleware 3 return"
"middleware 2 return"
"middleware 1 return"
按著代碼運(yùn)行流程一步步分析:
dispatch(0)
i==0,index==-1 i>index 往下
index=0 fn=f1 Promise.resolve(f1(context, dispatch.bind(null, 0 + 1)))
這就會(huì)執(zhí)行
f1(context, dispatch.bind(null, 0 + 1))
進(jìn)入到f1執(zhí)行上下文
console.log('middleware 1');
輸出middleware 1
next()
其實(shí)就是調(diào)用dispatch(1) bind的功勞
遞歸開始
dispatch(1)
i==1,index==0 i>index 往下
index=1 fn=f2 Promise.resolve(f2(context, dispatch.bind(null, 1 + 1)))
這就會(huì)執(zhí)行
f2(context, dispatch.bind(null, 1 + 1))
進(jìn)入到f2執(zhí)行上下文
console.log('middleware 2');
輸出middleware 2
next()
其實(shí)就是調(diào)用dispatch(2)
接著遞歸
dispatch(2)
i==2,index==1 i>index 往下
index=2 fn=f3 Promise.resolve(f3(context, dispatch.bind(null, 2 + 1)))
這就會(huì)執(zhí)行
f3(context, dispatch.bind(null, 2 + 1))
進(jìn)入到f3執(zhí)行上下文
console.log('middleware 3');
輸出middleware 3
next()
其實(shí)就是調(diào)用dispatch(3)
接著遞歸
dispatch(3)
i==3,index==2 i>index 往下
index=3 i === middleware.length fn=next Promise.resolve(next(context, dispatch.bind(null, 3 + 1)))
這就會(huì)執(zhí)行
next(context, dispatch.bind(null, 3 + 1))
進(jìn)入到next執(zhí)行上下文
console.log('middleware 4');
輸出middleware 4
next()
其實(shí)就是調(diào)用dispatch(4)
接著遞歸
dispatch(4)
i==4,index==3 i>index 往下
index=4 fn=middleware[4] fn=undefined reuturn Promise.resolve('fn is undefined')
回到next執(zhí)行上下文
console.log('middleware 4');
輸出middleware 4
return 'middleware 4 return' Promise.resolve('middleware 4 return')
回到f3執(zhí)行上下文
console.log('middleware 3');
輸出middleware 3
return 'middleware 3 return' Promise.resolve('middleware 3 return')
回到f2執(zhí)行上下文
console.log('middleware 2');
輸出middleware 2
return 'middleware 2 return' Promise.resolve('middleware 2 return')
回到f1執(zhí)行上下文
console.log('middleware 1');
輸出middleware 1
return 'middleware 1 return' Promise.resolve('middleware 1 return')
回到全局上下文
至此已經(jīng)輸出
"middleware 1"
"middleware 2"
"middleware 3"
"middleware 4"
"middleware 4"
"middleware 3"
"middleware 2"
"middleware 1"
那么
"fn is undefined"
"middleware 4 return"
"middleware 3 return"
"middleware 2 return"
"middleware 1 return"
怎么來的呢
回頭看一下,每個(gè)中間件里都有
next().then(data=>console.log(data));
按照之前的分析,then里最先拿到結(jié)果的應(yīng)該是next中間件的,而且結(jié)果就是Promise.resolve('fn is undefined')的結(jié)果,然后分別是f4,f3,f2,f1。那么為什么都是最后才輸出呢?
Promise.resolve('fn is undefined').then(data=>console.log(data)); console.log('middleware 4');
運(yùn)行一下就清楚了
或者
setTimeout(()=>console.log('fn is undefined'),0); console.log('middleware 4');
整個(gè)調(diào)用過程還可以看成是這樣的:
function composeDetail(){ return Promise.resolve( f1(context,function(){ return Promise.resolve( f2(context,function(){ return Promise.resolve( f3(context,function(){ return Promise.resolve( next(context,function(){ return Promise.resolve('fn is undefined') }) ) }) ) }) ) }) ) } composeDetail().then(data=>console.log(data));
方法雖蠢,但是compose的作用不言而喻了
最后,if (i <= index) return Promise.reject(new Error('next() called multiple times'))這句代碼何時(shí)回其作用呢?
一個(gè)中間件里調(diào)用兩次next(),按照上面的套路走,相信很快就明白了。
以上是“koa中間件處理模塊koa-compose怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!