Skip to content

Commit

Permalink
Implement PR#42
Browse files Browse the repository at this point in the history
  • Loading branch information
StorytellerCZ committed Jun 28, 2020
1 parent 6ea0742 commit 5cab12b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 16 additions & 8 deletions lib/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
};

Expand All @@ -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();
}
}

Expand All @@ -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();
}
Expand All @@ -86,7 +94,7 @@ PickerImp.prototype._dispatch = function(req, res, bypass) {
bypass();
}
}
processNextMiddleware();
processNextRoute();
};

PickerImp.prototype._buildParams = function(keys, m) {
Expand Down
4 changes: 2 additions & 2 deletions test/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down Expand Up @@ -130,4 +130,4 @@ Tinytest.add('middlewares - with several filtered routes', function(test) {

const res2 = HTTP.get(getPath(path2));
test.equal(res2.content, "12");
});*/
});

0 comments on commit 5cab12b

Please sign in to comment.