Skip to content

Commit

Permalink
add Context reference to returned Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Apr 20, 2017
1 parent 7aec6ae commit 6527ed9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"type": "node",
"request": "launch",
"name": "Launch sandbox",
"program": "${workspaceRoot}\\examples\\sandbox\\transit.js",
"program": "${workspaceRoot}\\examples\\sandbox\\call.js",
"cwd": "${workspaceRoot}\\examples\\sandbox"
},
{
Expand Down
2 changes: 1 addition & 1 deletion benchmark/perf-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let [b1, b2] = createBrokers(Transporters.Fake);
let count = 0;
function doRequest() {
count++;
return b1.call("echo.reply", { a: count }).then(res => {
return b2.call("echo.reply", { a: count }).then(res => {
if (count % 10000) {
// Fast cycle
doRequest();
Expand Down
9 changes: 7 additions & 2 deletions examples/sandbox/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
let Promise = require("bluebird");
let ServiceBroker = require("../../src/service-broker");

let broker = new ServiceBroker({ logger: console, validation: true, metrics: true });
let broker = new ServiceBroker({ logger: console, validation: false, metrics: true });
broker.loadService(__dirname + "/../../benchmark/user.service");
broker.loadService(__dirname + "/../metrics.service");

broker.start();

console.log(" --- CALL ---");
//broker.call("users.empty").then(res => console.log(res));
broker.call("users.validate", { id: "5" }).then(res => console.log(res));
let p = broker.call("users.validate", { id: 5 });
p.then(res => {
const ctx = p.ctx;
console.log(`Success! Action: ${ctx.action.name}, Duration: ${ctx.duration}`);
console.log("Result:", res);
});
7 changes: 6 additions & 1 deletion src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,12 @@ class ServiceBroker {
p = p.timeout(ctx.timeout);

// Error handler
return p.catch(err => this._callErrorHandler(err, ctx, opts));
p = p.catch(err => this._callErrorHandler(err, ctx, opts));

// Pointer to Context
p.ctx = ctx;

return p;
}

_callErrorHandler(err, ctx, opts) {
Expand Down
5 changes: 4 additions & 1 deletion test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,8 @@ describe("Test broker.call method", () => {
});

it("should call handler with new Context without params", () => {
return broker.call("posts.find").then(ctx => {
let p = broker.call("posts.find");
return p.then(ctx => {
expect(ctx).toBeDefined();
expect(ctx.broker).toBe(broker);
expect(ctx.nodeID).toBeUndefined();
Expand All @@ -785,6 +786,8 @@ describe("Test broker.call method", () => {
expect(ctx.params).toEqual({});
expect(ctx.metrics).toBe(true);

expect(p.ctx).toBe(ctx);

expect(actionHandler).toHaveBeenCalledTimes(1);
expect(actionHandler).toHaveBeenCalledWith(ctx);
});
Expand Down

0 comments on commit 6527ed9

Please sign in to comment.