Skip to content

Commit 5cab12b

Browse files
committed
Implement PR#42
meteorhacks#42
1 parent 6ea0742 commit 5cab12b

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

lib/implementation.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export const PickerImp = function(filterFunction) {
2222

2323
PickerImp.prototype.middleware = function(callback) {
2424
this.middlewares.push(callback);
25+
for(const subRouter of this.subRouters) {
26+
subRouter.middleware(callback);
27+
}
2528
};
2629

2730
PickerImp.prototype.route = function(path, callback) {
@@ -36,6 +39,9 @@ PickerImp.prototype.route = function(path, callback) {
3639
PickerImp.prototype.filter = function(callback) {
3740
const subRouter = new PickerImp(callback);
3841
this.subRouters.push(subRouter);
42+
for(const middleware of this.middlewares) {
43+
subRouter.middleware(middleware);
44+
}
3945
return subRouter;
4046
};
4147

@@ -51,12 +57,12 @@ PickerImp.prototype._dispatch = function(req, res, bypass) {
5157
}
5258
}
5359

54-
const processNextMiddleware = () => {
60+
const processNextMiddleware = (onDone) => {
5561
const middleware = this.middlewares[currentMiddleware++];
5662
if(middleware) {
57-
this._processMiddleware(middleware, req, res, processNextMiddleware);
63+
this._processMiddleware(middleware, req, res, () => processNextMiddleware(onDone));
5864
} else {
59-
processNextRoute();
65+
onDone();
6066
}
6167
}
6268

@@ -66,10 +72,12 @@ PickerImp.prototype._dispatch = function(req, res, bypass) {
6672
const uri = req.url.replace(/\?.*/, '');
6773
const m = uri.match(route);
6874
if(m) {
69-
const params = this._buildParams(route.keys, m);
70-
params.query = parseQuery(req._parsedUrl?.query);
71-
// See https://github.com/meteorhacks/picker/pull/39 for processNextRoute reason in the following method.
72-
this._processRoute(route.callback, params, req, res, processNextRoute);
75+
processNextMiddleware(() => {
76+
const params = this._buildParams(route.keys, m);
77+
params.query = parseQuery(req._parsedUrl?.query);
78+
// See https://github.com/meteorhacks/picker/pull/39 for processNextRoute reason in the following method.
79+
this._processRoute(route.callback, params, req, res, processNextRoute);
80+
});
7381
} else {
7482
processNextRoute();
7583
}
@@ -86,7 +94,7 @@ PickerImp.prototype._dispatch = function(req, res, bypass) {
8694
bypass();
8795
}
8896
}
89-
processNextMiddleware();
97+
processNextRoute();
9098
};
9199

92100
PickerImp.prototype._buildParams = function(keys, m) {

test/instance.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Tinytest.add('middlewares - with filtered routes', function(test) {
9797
test.equal(res.content, "ok");
9898
});
9999

100-
/*
100+
101101
Tinytest.add('middlewares - with several filtered routes', function(test) {
102102
const path1 = `${Random.id()}/coola`;
103103
const path2 = `${Random.id()}/coola`;
@@ -130,4 +130,4 @@ Tinytest.add('middlewares - with several filtered routes', function(test) {
130130

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

0 commit comments

Comments
 (0)