咱們在不少地方都用到了中間件的概念,我理解的中間件就是代碼執行過程當中插入一些中間過程。git
就是普通函數的中間件,函數的嵌套執行github
/** 執行中間件,獲得中間件的返回函數數組chain,而後利用compose方法,生成嵌套的執行chain 方法的包裝dispatch函數, 中間件的形式是 (getState, dispatch)=> next => action => { next(action); } */ export default function applyMiddleware(...middlewares) { return (createStore) => (reducer, preloadedState, enhancer) => { var store = createStore(reducer, preloadedState, enhancer) var dispatch = store.dispatch var chain = [] var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch) /** store.dispatch 就是第一個next 是last ware的next (...args) => { return ware0(ware1(ware2(last(...args)))) } dispatch = ware0(ware1(ware2(last(...args)))) 因此中間件中next傳入後返回的函數就是咱們須要的函數形式, 例如dispatch 須要的函數形式是 傳一個action */ return { ...store, dispatch } } } /** reduceRight是數組的從右至左執行, 初始的參數是最後一個函數接受dispatch, 的到的一個action=>{ dispatch(action); } 形式的函數,做爲參數composed f的形式是 next=>action=>{ } 最終造成的就是 (...args) => { return funcs0(funcs1(funcs2(last(...args)))) } */ export default function compose(...funcs) { if (funcs.length === 0) { return arg => arg } if (funcs.length === 1) { return funcs[0] } const last = funcs[funcs.length - 1] const rest = funcs.slice(0, -1) return (...args) => rest.reduceRight((composed, f) => f(composed), last(...args)) }
模擬這個過程express
function func1 (next) { return function () { console.log("func1 start"); next(); console.log("func1 end"); } } function func2 (next) { return function () { console.log("func2 start"); next(); console.log("func2 end"); } } function func3 (next) { return function () { console.log("func3 start"); next(); console.log("func3 end"); } } function App () { this.middlewares = [] } App.prototype.use = function (middleware) { this.middlewares.push(middleware) } App.prototype.exec = function (data) { //funclist func1(func2(func3(dispatch))) let funclist = this.middlewares.reduceRight(function (generate, next) { return next(generate) }, dispatch) funclist() } function dispatch () { console.log('dispatch') } let app = new App(); app.use(func1) app.use(func2) app.use(func3) app.exec('exec');
模擬直接的中間件過程,express的中間件相似這個原理redux
function func4 (next) { console.log("func4 start"); next(); console.log("func4 end"); } function func5 (next) { console.log("func5 start"); next(); console.log("func5 end"); } function func6 (next) { console.log("func6 start"); next(); console.log("func6 end"); } let middlewareList = [func4, func5, func6]; function exec() { let result = middlewareList.reduceRight(function (first, second){ return function (name) { second(first) } },function(){console.log('last next')}) result() } exec();
koa2中的中間件是async函數數組
也是用use方法添加到數組中,使用的時候重點是compose,如何組裝中間件,而後返回Promise module.exports = class Application extends Emitter { constructor() { this.middleware = []; } listen() { debug('listen'); const server = http.createServer(this.callback()); return server.listen.apply(server, arguments); } use(fn) { if (typeof fn !== 'function') throw new TypeError('middleware must be a function!'); if (isGeneratorFunction(fn)) { deprecate('Support for generators will be removed in v3. ' + 'See the documentation for examples of how to convert old middleware ' + 'https://github.com/koajs/koa/blob/master/docs/migration.md'); fn = convert(fn); } debug('use %s', fn._name || fn.name || '-'); this.middleware.push(fn); return this; } callback() { const fn = compose(this.middleware); if (!this.listeners('error').length) this.on('error', this.onerror); const handleRequest = (req, res) => { res.statusCode = 404; const ctx = this.createContext(req, res); const onerror = err => ctx.onerror(err); const handleResponse = () => respond(ctx); onFinished(res, onerror); return fn(ctx).then(handleResponse).catch(onerror); }; return handleRequest; } }; //////koa-compose 這裏從第一個中間件開始執行,傳給中間件的next,就是執行下一個中間件的方法,而且還能夠返回值,這裏利用了async函數的返回值是promise,而且能夠await promise, 等待執行,達到了寫代碼和同步相似的效果。 return function (context, next) { // last called middleware # let index = -1 return dispatch(0) / //從第一個中間件開始執行,async函數的返回值也是promise。 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, function next () { return dispatch(i + 1) })) } catch (err) { return Promise.reject(err) } } }
koa1的中間件是generator函數,因此關鍵點是對中間件的compose方法不一樣,koa2中利用的async函數能夠直接執行返回promise,而koa1中generator函數不能直接執行,因此
koa1中的compose是把generator生成的迭代器進行傳遞,因此next是迭代器,koa2中next是方法,而後生成一個大的generator函數,利用co模塊進行執行。
co模塊的執行,是對next拿到的value先進行promise化,而後再執行promise,在promise的then和catch中繼續調用迭代器的next方法,執行迭代器。promise
app.callback = function(){ if (this.experimental) { console.error('Experimental ES7 Async Function support is deprecated. Please look into Koa v2 as the middleware signature has changed.') } //通過compose和co的wrap獲得一個promise var fn = this.experimental ? compose_es7(this.middleware) : co.wrap(compose(this.middleware)); var self = this; return function handleRequest(req, res){ res.statusCode = 404; var ctx = self.createContext(req, res); onFinished(res, ctx.onerror); fn.call(ctx).then(function handleResponse() { respond.call(ctx); }).catch(ctx.onerror); } }; /*****************koa-compose*****************/ function compose(middleware){ return function *(next){ if (!next) next = noop(); var i = middleware.length; while (i--) { next = middleware[i].call(this, next); } return yield *next; } } function *noop(){} /*****************co模塊*****************/ function co(gen) { var ctx = this; var args = slice.call(arguments, 1) // we wrap everything in a promise to avoid promise chaining, // which leads to memory leak errors. // see https://github.com/tj/co/issues/180 return new Promise(function(resolve, reject) { if (typeof gen === 'function') gen = gen.apply(ctx, args); if (!gen || typeof gen.next !== 'function') return resolve(gen); onFulfilled(); function onFulfilled(res) { var ret; try { //繼續執行迭代器 ret = gen.next(res); } catch (e) { return reject(e); } next(ret); } function onRejected(err) { var ret; try { ret = gen.throw(err); } catch (e) { return reject(e); } next(ret); } function next(ret) { //若是迭代器執行完畢 if (ret.done) return resolve(ret.value); //promise化value var value = toPromise.call(ctx, ret.value); //執行value if (value && isPromise(value)) return value.then(onFulfilled, onRejected); return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, ' + 'but the following object was passed: "' + String(ret.value) + '"')); } }); }