Commit fa862176 authored by Romain Courteaud's avatar Romain Courteaud

cancel: propagate cancel message to thenable

parent 69c6d7c9
...@@ -122,8 +122,8 @@ Promise.prototype = { ...@@ -122,8 +122,8 @@ Promise.prototype = {
this.off('error', onerror); this.off('error', onerror);
var thenPromise = new this.constructor(function() {}, var thenPromise = new this.constructor(function() {},
function () { function (msg) {
thenPromise.trigger('promise:cancelled', {}); thenPromise.trigger('promise:cancelled', { msg: msg});
}); });
if (this.isFulfilled) { if (this.isFulfilled) {
...@@ -183,7 +183,7 @@ function handleThenable(promise, value) { ...@@ -183,7 +183,7 @@ function handleThenable(promise, value) {
if (isFunction(then)) { if (isFunction(then)) {
promise.on('promise:cancelled', function(event) { promise.on('promise:cancelled', function(event) {
if (isFunction(value.cancel)) { if (isFunction(value.cancel)) {
value.cancel(); value.cancel(event.msg);
} }
}); });
then.call(value, function(val) { then.call(value, function(val) {
......
...@@ -593,8 +593,10 @@ describe("RSVP extensions", function() { ...@@ -593,8 +593,10 @@ describe("RSVP extensions", function() {
setTimeout(function() { setTimeout(function() {
assert(first.isRejected); assert(first.isRejected);
assert(first.rejectedReason instanceof RSVP.CancellationError); assert(first.rejectedReason instanceof RSVP.CancellationError);
assert.equal(first.rejectedReason.message, "Foo");
assert(second.isRejected); assert(second.isRejected);
assert(second.rejectedReason instanceof RSVP.CancellationError); assert(second.rejectedReason instanceof RSVP.CancellationError);
assert.equal(second.rejectedReason.message, "Foo");
assert.equal(error.message, "Foo"); assert.equal(error.message, "Foo");
done(); done();
}, 20); }, 20);
...@@ -823,8 +825,10 @@ describe("RSVP extensions", function() { ...@@ -823,8 +825,10 @@ describe("RSVP extensions", function() {
setTimeout(function() { setTimeout(function() {
assert(first.isRejected); assert(first.isRejected);
assert(first.rejectedReason instanceof RSVP.CancellationError); assert(first.rejectedReason instanceof RSVP.CancellationError);
assert.equal(first.rejectedReason.message, "Foo");
assert(second.isRejected); assert(second.isRejected);
assert(second.rejectedReason instanceof RSVP.CancellationError); assert(second.rejectedReason instanceof RSVP.CancellationError);
assert.equal(second.rejectedReason.message, "Foo");
assert.equal(error.message, "Foo"); assert.equal(error.message, "Foo");
done(); done();
}, 20); }, 20);
...@@ -1762,6 +1766,7 @@ describe("`cancel` on promise created by then", function () { ...@@ -1762,6 +1766,7 @@ describe("`cancel` on promise created by then", function () {
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
assert.equal(resolve_count, 0); assert.equal(resolve_count, 0);
done(); done();
}, 20); }, 20);
...@@ -1777,6 +1782,7 @@ describe("`cancel` on promise created by then", function () { ...@@ -1777,6 +1782,7 @@ describe("`cancel` on promise created by then", function () {
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
assert.equal(reject_count, 0); assert.equal(reject_count, 0);
done(); done();
}, 20); }, 20);
...@@ -1793,6 +1799,7 @@ describe("`cancel` on promise created by then", function () { ...@@ -1793,6 +1799,7 @@ describe("`cancel` on promise created by then", function () {
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
assert.equal(reject_count, 0); assert.equal(reject_count, 0);
done(); done();
}, 20); }, 20);
...@@ -1821,10 +1828,12 @@ describe("`cancel` on promise created by then", function () { ...@@ -1821,10 +1828,12 @@ describe("`cancel` on promise created by then", function () {
it('should cancel the pending fulfilled promise', function (done) { it('should cancel the pending fulfilled promise', function (done) {
var cancel_called = false, var cancel_called = false,
cancel_msg,
promise = new RSVP.Promise(function (resolve) {resolve();}), promise = new RSVP.Promise(function (resolve) {resolve();}),
promise2 = promise.then(function () { promise2 = promise.then(function () {
return RSVP.Promise(function() {}, function () { return RSVP.Promise(function() {}, function (msg) {
cancel_called = true; cancel_called = true;
cancel_msg = msg;
}); });
}); });
...@@ -1832,9 +1841,11 @@ describe("`cancel` on promise created by then", function () { ...@@ -1832,9 +1841,11 @@ describe("`cancel` on promise created by then", function () {
promise2.cancel("Foo"); promise2.cancel("Foo");
setTimeout(function() { setTimeout(function() {
assert.equal(cancel_called, true); assert.equal(cancel_called, true);
assert.equal(cancel_msg, "Foo");
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
done(); done();
}, 20); }, 20);
}, 20); }, 20);
...@@ -1856,6 +1867,7 @@ describe("`cancel` on promise created by then", function () { ...@@ -1856,6 +1867,7 @@ describe("`cancel` on promise created by then", function () {
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
done(); done();
}, 20); }, 20);
}, 20); }, 20);
...@@ -1866,10 +1878,12 @@ describe("`cancel` on promise created by then", function () { ...@@ -1866,10 +1878,12 @@ describe("`cancel` on promise created by then", function () {
describe("`cancel` on thenable created by then", function () { describe("`cancel` on thenable created by then", function () {
it('should cancel the pending fulfilled promise', function (done) { it('should cancel the pending fulfilled promise', function (done) {
var cancel_called = false, var cancel_called = false,
cancel_msg,
promise = new RSVP.Promise(function (resolve) {resolve();}), promise = new RSVP.Promise(function (resolve) {resolve();}),
promise2 = promise.then(function () { promise2 = promise.then(function () {
return {"then": function() {}, "cancel": function () { return {"then": function() {}, "cancel": function (msg) {
cancel_called = true; cancel_called = true;
cancel_msg = msg;
}}; }};
}); });
...@@ -1877,9 +1891,11 @@ describe("`cancel` on thenable created by then", function () { ...@@ -1877,9 +1891,11 @@ describe("`cancel` on thenable created by then", function () {
promise2.cancel("Foo"); promise2.cancel("Foo");
setTimeout(function() { setTimeout(function() {
assert.equal(cancel_called, true); assert.equal(cancel_called, true);
assert.equal(cancel_msg, "Foo");
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
done(); done();
}, 20); }, 20);
}, 20); }, 20);
...@@ -1887,10 +1903,12 @@ describe("`cancel` on thenable created by then", function () { ...@@ -1887,10 +1903,12 @@ describe("`cancel` on thenable created by then", function () {
it('should cancel the pending rejected promise', function (done) { it('should cancel the pending rejected promise', function (done) {
var cancel_called = false, var cancel_called = false,
cancel_msg,
promise = new RSVP.Promise(function (resolve, reject) {reject();}), promise = new RSVP.Promise(function (resolve, reject) {reject();}),
promise2 = promise.then(undefined, function () { promise2 = promise.then(undefined, function () {
return {"then": function() {}, "cancel": function () { return {"then": function() {}, "cancel": function (msg) {
cancel_called = true; cancel_called = true;
cancel_msg = msg;
}}; }};
}); });
...@@ -1898,6 +1916,7 @@ describe("`cancel` on thenable created by then", function () { ...@@ -1898,6 +1916,7 @@ describe("`cancel` on thenable created by then", function () {
promise2.cancel("Foo"); promise2.cancel("Foo");
setTimeout(function() { setTimeout(function() {
assert.equal(cancel_called, true); assert.equal(cancel_called, true);
assert.equal(cancel_msg, "Foo");
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
...@@ -1921,6 +1940,7 @@ describe("`cancel` on non cancellable thenable created by then", function () { ...@@ -1921,6 +1940,7 @@ describe("`cancel` on non cancellable thenable created by then", function () {
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
done(); done();
}, 20); }, 20);
}, 20); }, 20);
...@@ -1939,6 +1959,7 @@ describe("`cancel` on non cancellable thenable created by then", function () { ...@@ -1939,6 +1959,7 @@ describe("`cancel` on non cancellable thenable created by then", function () {
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(promise2.rejectedReason.message, "Foo");
done(); done();
}, 20); }, 20);
}, 20); }, 20);
...@@ -2578,6 +2599,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2578,6 +2599,7 @@ describe("`RSVP.Queue`", function () {
it('should `cancel` pending success `thenable`', function (done) { it('should `cancel` pending success `thenable`', function (done) {
var thenable_cancel_called = false, var thenable_cancel_called = false,
thenable_cancel_msg,
thenable_ongoing = false, thenable_ongoing = false,
later_success_thenable_called = false, later_success_thenable_called = false,
later_error_thenable_called = false, later_error_thenable_called = false,
...@@ -2587,8 +2609,9 @@ describe("`RSVP.Queue`", function () { ...@@ -2587,8 +2609,9 @@ describe("`RSVP.Queue`", function () {
function () { function () {
thenable_ongoing = true; thenable_ongoing = true;
}, },
function () { function (msg) {
thenable_cancel_called = true; thenable_cancel_called = true;
thenable_cancel_msg = msg;
}); });
}) })
.push( .push(
...@@ -2610,6 +2633,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2610,6 +2633,7 @@ describe("`RSVP.Queue`", function () {
assert.equal(thenable_ongoing, true); assert.equal(thenable_ongoing, true);
queue.cancel("Foo"); queue.cancel("Foo");
assert.equal(thenable_cancel_called, true); assert.equal(thenable_cancel_called, true);
assert.equal(thenable_cancel_msg, "Foo");
setTimeout(function() { setTimeout(function() {
assert.equal(queue.isRejected, true); assert.equal(queue.isRejected, true);
...@@ -2628,6 +2652,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2628,6 +2652,7 @@ describe("`RSVP.Queue`", function () {
it('should `cancel` pending error `thenable`', function (done) { it('should `cancel` pending error `thenable`', function (done) {
var thenable_cancel_called = false, var thenable_cancel_called = false,
thenable_cancel_msg,
thenable_ongoing = false, thenable_ongoing = false,
later_success_thenable_called = false, later_success_thenable_called = false,
later_error_thenable_called = false, later_error_thenable_called = false,
...@@ -2640,8 +2665,9 @@ describe("`RSVP.Queue`", function () { ...@@ -2640,8 +2665,9 @@ describe("`RSVP.Queue`", function () {
function () { function () {
thenable_ongoing = true; thenable_ongoing = true;
}, },
function () { function (msg) {
thenable_cancel_called = true; thenable_cancel_called = true;
thenable_cancel_msg = msg;
}); });
}) })
.push( .push(
...@@ -2663,6 +2689,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2663,6 +2689,7 @@ describe("`RSVP.Queue`", function () {
assert.equal(thenable_ongoing, true); assert.equal(thenable_ongoing, true);
queue.cancel("Foo"); queue.cancel("Foo");
assert.equal(thenable_cancel_called, true); assert.equal(thenable_cancel_called, true);
assert.equal(thenable_cancel_msg, "Foo");
setTimeout(function() { setTimeout(function() {
assert.equal(queue.isRejected, true); assert.equal(queue.isRejected, true);
...@@ -2682,6 +2709,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2682,6 +2709,7 @@ describe("`RSVP.Queue`", function () {
it('should `cancel` propagate message to CancellationError', function (done) { it('should `cancel` propagate message to CancellationError', function (done) {
var error, var error,
thenable_cancel_called = false, thenable_cancel_called = false,
thenable_cancel_msg,
thenable_ongoing = false, thenable_ongoing = false,
queue = new RSVP.Queue() queue = new RSVP.Queue()
.push(function () { .push(function () {
...@@ -2692,8 +2720,9 @@ describe("`RSVP.Queue`", function () { ...@@ -2692,8 +2720,9 @@ describe("`RSVP.Queue`", function () {
function () { function () {
thenable_ongoing = true; thenable_ongoing = true;
}, },
function () { function (msg) {
thenable_cancel_called = true; thenable_cancel_called = true;
thenable_cancel_msg = msg;
}); });
}); });
queue.fail(function(e) { queue.fail(function(e) {
...@@ -2703,6 +2732,7 @@ describe("`RSVP.Queue`", function () { ...@@ -2703,6 +2732,7 @@ describe("`RSVP.Queue`", function () {
assert.equal(thenable_ongoing, true); assert.equal(thenable_ongoing, true);
queue.cancel("Propagate this message"); queue.cancel("Propagate this message");
assert.equal(thenable_cancel_called, true); assert.equal(thenable_cancel_called, true);
assert.equal(thenable_cancel_msg, "Propagate this message");
setTimeout(function() { setTimeout(function() {
assert(queue.rejectedReason instanceof RSVP.CancellationError, queue.rejectedReason); assert(queue.rejectedReason instanceof RSVP.CancellationError, queue.rejectedReason);
assert(error instanceof RSVP.CancellationError, error); assert(error instanceof RSVP.CancellationError, error);
......
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