Commit 64cddeee authored by Stefan Penner's avatar Stefan Penner

[fixes #66]

- Node >= 0.10.0 should used setImmediate over nextTick
- prefer argument passing then callback rebinding
parent f2de93a4
var browserGlobal = (typeof window !== 'undefined') ? window : {};
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var async;
if (typeof process !== 'undefined' &&
{}.toString.call(process) === '[object process]') {
async = function(callback, binding) {
// old node
function useNextTick() {
return function(callback, arg) {
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 observer = new BrowserMutationObserver(function() {
......@@ -18,8 +29,8 @@ if (typeof process !== 'undefined' &&
queue = [];
toProcess.forEach(function(tuple) {
var callback = tuple[0], binding = tuple[1];
callback.call(binding);
var callback = tuple[0], arg= tuple[1];
callback(arg);
});
});
......@@ -32,16 +43,28 @@ if (typeof process !== 'undefined' &&
observer = null;
});
async = function(callback, binding) {
queue.push([callback, binding]);
return function(callback, arg) {
queue.push([callback, arg]);
element.setAttribute('drainQueue', 'drainQueue');
};
} else {
async = function(callback, binding) {
}
function useSetTimeout() {
return function(callback, arg) {
setTimeout(function() {
callback.call(binding);
callback(arg);
}, 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 };
......@@ -95,14 +95,14 @@ Promise.prototype = {
var thenPromise = new this.constructor(function() {});
if (this.isFulfilled) {
config.async(function() {
invokeCallback('resolve', thenPromise, done, { detail: this.fulfillmentValue });
config.async(function(promise) {
invokeCallback('resolve', thenPromise, done, { detail: promise.fulfillmentValue });
}, this);
}
if (this.isRejected) {
config.async(function() {
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedReason });
config.async(function(promise) {
invokeCallback('reject', thenPromise, fail, { detail: promise.rejectedReason });
}, 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