Skip to content

Commit

Permalink
Workaround issue strongloop#17 with cujojs/when
Browse files Browse the repository at this point in the history
  • Loading branch information
josieusa committed Jan 10, 2017
1 parent 750d8bb commit cec955c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"eslint-config-loopback": "^4.0.0",
"loopback": "^3.0.0",
"mocha": "^2.5.3",
"supertest": "^1.2.0"
"supertest": "^1.2.0",
"when": "3.7.7"
}
}
14 changes: 13 additions & 1 deletion server/current-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ LoopBackContext.createContext = function(scopeName) {
process.context[scopeName] = ns;
// Set up LoopBackContext.getCurrentContext()
LoopBackContext.getCurrentContext = function() {
return ns && ns.active ? ns : null;
var boundMethods = {
get: ns.bind(ns.get).bind(ns),
set: ns.bind(ns.set).bind(ns),
};
var handler = {
get: function(target, name) {
return ['get', 'set'].includes(name) ?
boundMethods[name] :
target[name];
},
};
var proxy = new Proxy(ns, handler);
return ns && ns.active ? proxy : null;
};
}
return ns;
Expand Down
47 changes: 47 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict';

var async = require('async');
var when = require('when');
var LoopBackContext = require('..');
var Domain = require('domain');
var EventEmitter = require('events').EventEmitter;
Expand Down Expand Up @@ -129,4 +130,50 @@ describe('LoopBack Context', function() {
], done);
});
});
it('doesn\'t mix up contexts if using concurrently then() from when 3.7.7',
function() {
expect(require('when/package.json').version).to.equal('3.7.7');
var timeout = 50;
// Concurrent execution number 1 of 2
var execution1 = new Promise(function execution1(outerResolve, reject) {
LoopBackContext.runInContext(function pushToContext1() {
var ctx = LoopBackContext.getCurrentContext();
expect(ctx).is.an('object');
ctx.set('test-key', 'test-value-1');
var whenPromise = when.promise(function(resolve) {
setTimeout(resolve, timeout);
});
whenPromise.then(function pullFromContext1() {
var testValue = ctx && ctx.get('test-key', 'test-value-1');
return testValue;
}).then(function verify1(testValue) {
expect(testValue).to.equal('test-value-1');
outerResolve();
}).catch(function(error) {
reject(error);
});
});
});
// Concurrent execution number 2 of 2
var execution2 = new Promise(function execution1(outerResolve, reject) {
LoopBackContext.runInContext(function pushToContext2() {
var ctx = LoopBackContext.getCurrentContext();
expect(ctx).is.an('object');
ctx.set('test-key', 'test-value-2');
var whenPromise = when.promise(function(resolve) {
setTimeout(resolve, timeout);
});
whenPromise.then(function pullFromContext2() {
var testValue = ctx && ctx.get('test-key', 'test-value-2');
return testValue;
}).then(function verify2(testValue) {
expect(testValue).to.equal('test-value-2');
outerResolve();
}).catch(function(error) {
reject(error);
});
});
});
return Promise.all([execution1, execution2]);
});
});

0 comments on commit cec955c

Please sign in to comment.