From 5cab12b056b04db32916c873ad82aa17201cffed Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Sun, 28 Jun 2020 23:15:41 +0900 Subject: [PATCH] Implement PR#42 https://github.com/meteorhacks/picker/pull/42 --- lib/implementation.js | 24 ++++++++++++++++-------- test/instance.js | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/implementation.js b/lib/implementation.js index dec877c..bb334fd 100644 --- a/lib/implementation.js +++ b/lib/implementation.js @@ -22,6 +22,9 @@ export const PickerImp = function(filterFunction) { PickerImp.prototype.middleware = function(callback) { this.middlewares.push(callback); + for(const subRouter of this.subRouters) { + subRouter.middleware(callback); + } }; PickerImp.prototype.route = function(path, callback) { @@ -36,6 +39,9 @@ PickerImp.prototype.route = function(path, callback) { PickerImp.prototype.filter = function(callback) { const subRouter = new PickerImp(callback); this.subRouters.push(subRouter); + for(const middleware of this.middlewares) { + subRouter.middleware(middleware); + } return subRouter; }; @@ -51,12 +57,12 @@ PickerImp.prototype._dispatch = function(req, res, bypass) { } } - const processNextMiddleware = () => { + const processNextMiddleware = (onDone) => { const middleware = this.middlewares[currentMiddleware++]; if(middleware) { - this._processMiddleware(middleware, req, res, processNextMiddleware); + this._processMiddleware(middleware, req, res, () => processNextMiddleware(onDone)); } else { - processNextRoute(); + onDone(); } } @@ -66,10 +72,12 @@ PickerImp.prototype._dispatch = function(req, res, bypass) { const uri = req.url.replace(/\?.*/, ''); const m = uri.match(route); if(m) { - const params = this._buildParams(route.keys, m); - params.query = parseQuery(req._parsedUrl?.query); - // See https://github.com/meteorhacks/picker/pull/39 for processNextRoute reason in the following method. - this._processRoute(route.callback, params, req, res, processNextRoute); + processNextMiddleware(() => { + const params = this._buildParams(route.keys, m); + params.query = parseQuery(req._parsedUrl?.query); + // See https://github.com/meteorhacks/picker/pull/39 for processNextRoute reason in the following method. + this._processRoute(route.callback, params, req, res, processNextRoute); + }); } else { processNextRoute(); } @@ -86,7 +94,7 @@ PickerImp.prototype._dispatch = function(req, res, bypass) { bypass(); } } - processNextMiddleware(); + processNextRoute(); }; PickerImp.prototype._buildParams = function(keys, m) { diff --git a/test/instance.js b/test/instance.js index 520e800..b9e751d 100644 --- a/test/instance.js +++ b/test/instance.js @@ -97,7 +97,7 @@ Tinytest.add('middlewares - with filtered routes', function(test) { test.equal(res.content, "ok"); }); -/* + Tinytest.add('middlewares - with several filtered routes', function(test) { const path1 = `${Random.id()}/coola`; const path2 = `${Random.id()}/coola`; @@ -130,4 +130,4 @@ Tinytest.add('middlewares - with several filtered routes', function(test) { const res2 = HTTP.get(getPath(path2)); test.equal(res2.content, "12"); -});*/ +});