Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
 - fix jsdoc for getCurrentContext
 - clean up getCurrentContext code
 - clean up the new test
  • Loading branch information
bajtos committed Feb 24, 2017
1 parent 244627a commit 442d77f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
12 changes: 9 additions & 3 deletions server/current-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ var LoopBackContext = module.exports;
* Get the current context object. The context is preserved
* across async calls, it behaves like a thread-local storage.
*
* @options {Object} [options]
* @property {Boolean} bind Bind get/set/bind methods of the context to the
* context that's current at the time getCurrentContext() is invoked. This
* can be used to work around 3rd party code breaking CLS context propagation.
* @returns {Namespace} The context object or null.
*/
LoopBackContext.getCurrentContext = function() {
LoopBackContext.getCurrentContext = function(options) {
// A placeholder method, see LoopBackContext.createContext() for the real version
return null;
};
Expand Down Expand Up @@ -91,10 +95,12 @@ LoopBackContext.createContext = function(scopeName) {
* you may run into unexpected issues that are fixed only for get & set.
*/
var boundContext = Object.create(ns);
// Call to Function.prototype.bind(), not ns.bind()
boundContext.bind = ns.bind.bind(ns);
boundContext.get = boundContext.bind(ns.get);
boundContext.set = boundContext.bind(ns.set);

// Call to Function.prototype.bind(), not ns.bind()
boundContext.bind = ns.bind.bind(ns);

return boundContext;
};
}
Expand Down
30 changes: 18 additions & 12 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,31 +212,37 @@ describe('LoopBack Context', function() {
function runWithRequestId(pushedValue, bindNextCb) {
return new Promise(function chainExecutor(outerResolve, reject) {
LoopBackContext.runInContext(function concurrentChain() {
var middleware1 = function middleware1(req, res, next) {
function middlewareBreakingCls(req, res, next) {
var ctx = LoopBackContext.getCurrentContext({bind: true});
if (bindNextCb) {
next = ctx.bind(next);
}
ctx.set('test-key', req.pushedValue);
ctx.set('test-key', pushedValue);
var whenPromise = whenV377().delay(timeout);
whenPromise.then(next).catch(reject);
};
var middleware2 = function middleware2(req, res, next) {

function middlewareReadingContext(req, res, next) {
var ctx = LoopBackContext.getCurrentContext({bind: true});
var pulledValue = ctx && ctx.get('test-key');
next(null, pulledValue);
};
// Run chain
var req = {pushedValue: pushedValue};

// Run the chain
var req = null;
var res = null;
var next2 = function resolveWithResult(error, result) {
outerResolve({
pulledValue: result,
pushedValue: pushedValue,
middlewareBreakingCls(req, res, function(err) {
if (err) return reject(err);

middlewareReadingContext(req, res, function(err, result) {
if (err) return reject(err);

outerResolve({
pulledValue: result,
pushedValue: pushedValue,
});
});
};
var next1 = middleware2.bind(null, req, res, next2);
middleware1(req, res, next1);
});
});
});
}
Expand Down

0 comments on commit 442d77f

Please sign in to comment.