Commit fa862176 authored by Romain Courteaud's avatar Romain Courteaud

cancel: propagate cancel message to thenable

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