Commit dd3c1b1f authored by Romain Courteaud's avatar Romain Courteaud

Queue should respect promise A+.

parent 5ccaf0c5
...@@ -13,10 +13,12 @@ ResolvedQueueError.prototype = new Error(); ...@@ -13,10 +13,12 @@ ResolvedQueueError.prototype = new Error();
ResolvedQueueError.prototype.constructor = ResolvedQueueError; ResolvedQueueError.prototype.constructor = ResolvedQueueError;
var Queue = function() { var Queue = function() {
var promise = this, var queue = this,
promise_list = [], promise_list = [],
promise,
fulfill, fulfill,
reject; reject,
resolved;
if (!(this instanceof Queue)) { if (!(this instanceof Queue)) {
return new Queue(); return new Queue();
...@@ -28,10 +30,22 @@ var Queue = function() { ...@@ -28,10 +30,22 @@ var Queue = function() {
} }
} }
Promise.apply(promise, [function (done, fail) { promise = new Promise(function(done, fail) {
fulfill = done; fulfill = function (fulfillmentValue) {
reject = fail; if (resolved) {return;}
}, canceller]); queue.isFulfilled = true;
queue.fulfillmentValue = fulfillmentValue;
resolved = true;
return done(fulfillmentValue);
};
reject = function (rejectedReason) {
if (resolved) {return;}
queue.isRejected = true;
queue.rejectedReason = rejectedReason ;
resolved = true;
return fail(rejectedReason);
};
}, canceller);
promise_list.push(delay()); promise_list.push(delay());
promise_list.push(promise_list[0].then(function () { promise_list.push(promise_list[0].then(function () {
...@@ -41,11 +55,24 @@ var Queue = function() { ...@@ -41,11 +55,24 @@ var Queue = function() {
} }
})); }));
promise.push = function(done, fail) { queue.cancel = function () {
if (resolved) {return;}
resolved = true;
promise.cancel();
promise.fail(function (rejectedReason) {
queue.isRejected = true;
queue.rejectedReason = rejectedReason;
});
};
queue.then = function () {
return promise.then.apply(promise, arguments);
};
queue.push = function(done, fail) {
var last_promise = promise_list[promise_list.length - 1], var last_promise = promise_list[promise_list.length - 1],
next_promise; next_promise;
if (this.isRejected || this.isFulfilled) { if (resolved) {
throw new ResolvedQueueError(); throw new ResolvedQueueError();
} }
...@@ -61,10 +88,12 @@ var Queue = function() { ...@@ -61,10 +88,12 @@ var Queue = function() {
return fulfillmentValue; return fulfillmentValue;
} }
}, function (rejectedReason) { }, function (rejectedReason) {
if (!(promise.isRejected || promise.isFulfilled)) { promise_list.splice(0, 2);
if (promise_list.length === 0) {
reject(rejectedReason); reject(rejectedReason);
} } else {
throw rejectedReason; throw rejectedReason;
}
})); }));
return this; return this;
......
...@@ -9,5 +9,15 @@ module.exports = { ...@@ -9,5 +9,15 @@ module.exports = {
options: { options: {
reporter: 'spec' reporter: 'spec'
} }
// },
// test2: {
// src: [
// 'test/vendor/assert.js',
// 'test/test-queue-adapter.js',
// 'node_modules/promises-aplus-tests/lib/tests/**/*.js'
// ],
// options: {
// reporter: 'spec'
// }
} }
}; };
/*global RSVP*/
var Queue;
if (typeof RSVP !== 'undefined') {
// Test the browser build
Queue = RSVP.Queue;
} else {
// Test the Node build
RSVP = require('../dist/commonjs/main');
Queue = require('../dist/commonjs/main').Queue;
Promise = require('../dist/commonjs/main').Promise;
assert = require('./vendor/assert');
}
if (typeof window === 'undefined' && typeof global !== 'undefined') {
window = global;
}
var adapter = {};
adapter.fulfilled = function(value) {
return new Queue()
.push(function () {
return value;
});
};
adapter.rejected = function(error) {
return new Queue()
.push(function () {
return RSVP.reject(error);
});
};
adapter.pending = function () {
var pending = {},
promise = new Promise(function(resolve, reject) {
pending.fulfill = resolve;
pending.reject = reject;
});
pending.promise = new Queue()
.push(function () {
return promise;
});
return pending;
};
module.exports = global.adapter = adapter;
...@@ -2331,7 +2331,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2331,7 +2331,7 @@ describe("`RSVP.Queue`", function () {
}); });
queue.push(undefined, function (value) { queue.push(undefined, function (value) {
pushed_result = value; pushed_result = value;
return "bar"; throw value;
}); });
setTimeout(function() { setTimeout(function() {
...@@ -2350,7 +2350,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2350,7 +2350,7 @@ describe("`RSVP.Queue`", function () {
}); });
queue.push(undefined, function (value) { queue.push(undefined, function (value) {
pushed_result = value; pushed_result = value;
return "bar"; throw value;
}); });
queue.cancel(); queue.cancel();
......
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