Skip to content

Commit

Permalink
Clean up the context setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed Sep 19, 2014
1 parent 5401be5 commit 3d4db3b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion example/client-server/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CartItem.sum = function(cartId, callback) {
}, 0);

var ns = loopback.getCurrentContext();
console.log(ns.get('req').url);
console.log(ns.get('http').req.url);
callback(null, total);
});
}
Expand Down
40 changes: 18 additions & 22 deletions lib/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,18 @@ function context(options) {
return ns;
};

if (typeof juggler.getCurrentContext === 'function') {
var jugglerContext = new ChainedContext(juggler.getCurrentContext(), ns);
juggler.getCurrentContext = function () {
return jugglerContext;
};
} else {
juggler.getCurrentContext = loopback.getCurrentContext;
}

if (typeof remoting.getCurrentContext === 'function') {
var remotingContext = new ChainedContext(remoting.getCurrentContext(), ns);
remoting.getCurrentContext = function () {
return remotingContext;
};
} else {
remoting.getCurrentContext = loopback.getCurrentContext;
}
chain(juggler);
chain(remoting);

// Return the middleware
return function (req, res, next) {
return function(req, res, next) {
// Bind req/res event emitters to the given namespace
ns.bindEmitter(req);
ns.bindEmitter(res);
// Create namespace for the request context
ns.run(function (context) {
ns.run(function(context) {
// Run the code in the context of the namespace
ns.set('req', req);
ns.set('res', res);
ns.set('http', {req: req, res: res}); // Set up the transport context
next();
});
};
Expand Down Expand Up @@ -93,4 +77,16 @@ ChainedContext.prototype.reset = function (name, val) {
} else {
return this.parent && this.parent.reset(name, val);
}
};
};

function chain(child) {
if (typeof child.getCurrentContext === 'function') {
var childContext = new ChainedContext(child.getCurrentContext(),
loopback.getCurrentContext());
child.getCurrentContext = function() {
return childContext;
};
} else {
child.getCurrentContext = loopback.getCurrentContext;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"uid2": "0.0.3",
"underscore": "~1.7.0",
"underscore.string": "~2.3.3",
"continuation-local-storage": "~3.0.0"
"continuation-local-storage": "~3.1.1"
},
"peerDependencies": {
"loopback-datasource-juggler": "^2.8.0"
Expand Down
8 changes: 5 additions & 3 deletions test/rest.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@ describe('loopback.rest', function() {
var User = givenUserModelWithAuth();
User.getToken = function(cb) {
var context = loopback.getCurrentContext();
var req = context.get('req');
var req = context.get('http').req;
expect(req).to.have.property('accessToken');

var juggler = require('loopback-datasource-juggler');
expect(juggler.getCurrentContext().get('req')).to.have.property('accessToken');
expect(juggler.getCurrentContext().get('http').req)
.to.have.property('accessToken');

var remoting = require('strong-remoting');
expect(remoting.getCurrentContext().get('req')).to.have.property('accessToken');
expect(remoting.getCurrentContext().get('http').req)
.to.have.property('accessToken');

cb(null, req && req.accessToken ? req.accessToken.id : null);
};
Expand Down

0 comments on commit 3d4db3b

Please sign in to comment.