Commit a39f0734 authored by Stefan Penner's avatar Stefan Penner

Merge pull request #91 from stefanpenner/master

[fixes #66] Use setImmediate when available, which fixes newer node's
parents 6b05a90d 64cddeee
var browserGlobal = (typeof window !== 'undefined') ? window : {}; var browserGlobal = (typeof window !== 'undefined') ? window : {};
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var async; var async;
if (typeof process !== 'undefined' && // old node
{}.toString.call(process) === '[object process]') { function useNextTick() {
async = function(callback, binding) { return function(callback, arg) {
process.nextTick(function() { process.nextTick(function() {
callback.call(binding); callback(arg);
}); });
}; };
} else if (BrowserMutationObserver) { }
// node >= 0.10.x
function useSetImmediate() {
return function(callback, arg) {
/* global setImmediate */
setImmediate(function(){
callback(arg);
});
};
}
function useMutationObserver() {
var queue = []; var queue = [];
var observer = new BrowserMutationObserver(function() { var observer = new BrowserMutationObserver(function() {
...@@ -18,8 +29,8 @@ if (typeof process !== 'undefined' && ...@@ -18,8 +29,8 @@ if (typeof process !== 'undefined' &&
queue = []; queue = [];
toProcess.forEach(function(tuple) { toProcess.forEach(function(tuple) {
var callback = tuple[0], binding = tuple[1]; var callback = tuple[0], arg= tuple[1];
callback.call(binding); callback(arg);
}); });
}); });
...@@ -32,16 +43,28 @@ if (typeof process !== 'undefined' && ...@@ -32,16 +43,28 @@ if (typeof process !== 'undefined' &&
observer = null; observer = null;
}); });
async = function(callback, binding) { return function(callback, arg) {
queue.push([callback, binding]); queue.push([callback, arg]);
element.setAttribute('drainQueue', 'drainQueue'); element.setAttribute('drainQueue', 'drainQueue');
}; };
} else { }
async = function(callback, binding) {
function useSetTimeout() {
return function(callback, arg) {
setTimeout(function() { setTimeout(function() {
callback.call(binding); callback(arg);
}, 1); }, 1);
}; };
} }
if (typeof setImmediate === 'function') {
async = useSetImmediate();
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
async = useNextTick();
} else if (BrowserMutationObserver) {
async = useMutationObserver();
} else {
async = useSetTimeout();
}
export { async }; export { async };
...@@ -95,14 +95,14 @@ Promise.prototype = { ...@@ -95,14 +95,14 @@ Promise.prototype = {
var thenPromise = new this.constructor(function() {}); var thenPromise = new this.constructor(function() {});
if (this.isFulfilled) { if (this.isFulfilled) {
config.async(function() { config.async(function(promise) {
invokeCallback('resolve', thenPromise, done, { detail: this.fulfillmentValue }); invokeCallback('resolve', thenPromise, done, { detail: promise.fulfillmentValue });
}, this); }, this);
} }
if (this.isRejected) { if (this.isRejected) {
config.async(function() { config.async(function(promise) {
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedReason }); invokeCallback('reject', thenPromise, fail, { detail: promise.rejectedReason });
}, this); }, this);
} }
......
// thanks to @wizardwerdna for the test case -> https://github.com/tildeio/rsvp.js/issues/66
describe("using reduce to sum integers using promises", function(){
var resolve = RSVP.resolve;
it("should build the promise pipeline without error", function(){
var array, iters, pZero, i;
array = [];
iters = 1000;
for (i=1; i<=iters; i++) {
array.push(i);
}
pZero = resolve(0);
array.reduce(function(promise, nextVal) {
return promise.then(function(currentVal) {
return resolve(currentVal + nextVal);
});
}, pZero);
});
it("should get correct answer without blowing the nextTick stack", function(done){
var pZero, array, iters, result, i;
pZero = resolve(0);
array = [];
iters = 1000;
for (i=1; i<=iters; i++) {
array.push(i);
}
result = array.reduce(function(promise, nextVal) {
return promise.then(function(currentVal) {
return resolve(currentVal + nextVal);
});
}, pZero);
result.then(function(value){
assert.equal(value, (iters*(iters+1)/2));
done();
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment