Skip to content

Commit

Permalink
v0.0.5 - order of middleware changed
Browse files Browse the repository at this point in the history
middleware now runs like so:
first element in middleware[] gets run first, thus making it last on
flow back

last element in middleware[] therefore gets run last, thus making it
the first on flow back

rewrite compose to avoid having to .reverse() array
  • Loading branch information
hnry committed Jul 5, 2016
1 parent d69c8de commit b2f578b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "spirit",
"version": "0.0.4",
"version": "0.0.5",
"description": "extensible web library for building applications & frameworks",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions spec/core/core-spec.js
@@ -1,11 +1,11 @@
const core = require("../../lib/core/core")

describe("compose", () => {
describe("core - compose", () => {
const reduce = core.compose

it("order", (done) => {
const handler = (request) => {
expect(request.called).toBe(".cba")
expect(request.called).toBe(".abc")
return Promise.resolve({ resp: "." })
}

Expand Down Expand Up @@ -45,7 +45,7 @@ describe("compose", () => {
const resp = route(mock_req)
resp.then((response) => {
expect(response).toEqual({
resp: ".123"
resp: ".321"
})
done()
})
Expand Down
18 changes: 9 additions & 9 deletions spec/spirit-spec.js
Expand Up @@ -11,15 +11,15 @@ describe("spirit spec", () => {
const middlewares = [
(handler) => {
return (input) => {
input += "c"
input += "b"
return handler(input).then((result) => {
return result + "1"
})
}
},
(handler) => {
return (input) => {
input += "b"
input += "c"
return handler(input).then((result) => {
return result + "2"
})
Expand All @@ -29,7 +29,7 @@ describe("spirit spec", () => {

const reducer = spirit.compose(handler, middlewares)
reducer("a").then((result) => {
expect(result).toBe("out12")
expect(result).toBe("out21")
done()
})
})
Expand Down Expand Up @@ -63,19 +63,19 @@ describe("spirit spec", () => {
throw "never gets run"
}
const middlewares = [
(handler) => {
return (input) => {
expect(input).toBe("ab")
return Promise.resolve("stop")
}
},
(handler) => {
return (input) => {
input += "b"
return handler(input).then((result) => {
return result + "2"
})
}
},
(handler) => {
return (input) => {
expect(input).toBe("ab")
return Promise.resolve("stop")
}
}
]
const reducer = spirit.compose(handler, middlewares)
Expand Down
9 changes: 9 additions & 0 deletions src/core/core.js
Expand Up @@ -20,13 +20,22 @@ const compose = (handler, middleware) => {
}
}

let prev = wrap(handler)

for (let i = middleware.length - 1; i >= 0; i--) {
prev = wrap(middleware[i](prev))
}

return prev
/*
return middleware.reduce((prev, curr) => {
const r = curr(prev)
if (typeof r !== "function") {
throw new TypeError("Expected middleware to return a function that takes a request")
}
return wrap(r)
}, wrap(handler))
*/
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions src/core/promise_utils.js
Expand Up @@ -3,8 +3,8 @@
*/
const Promise = require("bluebird")
Promise.onPossiblyUnhandledRejection(function(e, promise) {
throw e;
});
throw e
})

/**
* checks if `p` is a promise, returning true or false
Expand Down

0 comments on commit b2f578b

Please sign in to comment.