Skip to content

Commit

Permalink
Merge pull request #337 from taras/master
Browse files Browse the repository at this point in the history
Replaced deprecated process.nextTick with setImmediate
  • Loading branch information
stefanpenner committed Dec 6, 2014
2 parents e4b2f24 + 3810382 commit 69714a9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,6 +1,7 @@
language: node_js
node_js:
- '0.10'
- '0.11'
after_success:
- "./bin/publish_to_s3.js"
env:
Expand Down
12 changes: 10 additions & 2 deletions lib/rsvp/asap.js
Expand Up @@ -15,6 +15,7 @@ export default function asap(callback, arg) {
var browserWindow = (typeof window !== 'undefined') ? window : undefined
var browserGlobal = browserWindow || {};
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';

// test for web worker but not in IE10
var isWorker = typeof Uint8ClampedArray !== 'undefined' &&
Expand All @@ -23,8 +24,15 @@ var isWorker = typeof Uint8ClampedArray !== 'undefined' &&

// node
function useNextTick() {
var nextTick = process.nextTick;
// node version 0.10.x displays a deprecation warning when nextTick is used recursively
// setImmediate should be used instead instead
var version = process.versions.node.match(/^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)$/);
if (Array.isArray(version) && version[1] === '0' && version[2] === '10') {
nextTick = setImmediate;
}
return function() {
process.nextTick(flush);
nextTick(flush);
};
}

Expand Down Expand Up @@ -88,7 +96,7 @@ function attemptVertex() {

var scheduleFlush;
// Decide what async method to use to triggering processing of queued callbacks:
if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
if (isNode) {
scheduleFlush = useNextTick();
} else if (BrowserMutationObserver) {
scheduleFlush = useMutationObserver();
Expand Down
34 changes: 34 additions & 0 deletions test/tests/extension-test.js
Expand Up @@ -2520,3 +2520,37 @@ describe("Thenables should not be able to run code during assimilation", functio
assert.strictEqual(thenCalled, false);
});
});

describe("on node 0.10.x, using process.nextTick recursively shows deprecation warning. setImmediate should be used instead.", function() {
// (node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

it("should not cause 'RangeError: Maximum call stack size exceeded'", function (done) {
var total = 1000;

var resolved = 0;
var nextTick = function(depth) {
if (depth >= total) {
return RSVP.resolve();
}
var d = depth + 1;
//console.log('entered:', d);
return new RSVP.Promise(function(resolve){
process.nextTick(function(){
nextTick(d).then(function(){
resolved++;
//console.log('resolving:', d);
resolve();
});
});
});
};

nextTick(0)
.then(function(){
//console.log('nextTick: final');
assert.strictEqual(resolved, total);
done();
});
});

});

0 comments on commit 69714a9

Please sign in to comment.