9eb40b6fb14f33713217a0d803a20af08e73aff5
[aai/esr-gui.git] /
1 "use strict";
2 module.exports = function(Promise) {
3 var errors = require("./errors.js");
4 var async = require("./async.js");
5 var CancellationError = errors.CancellationError;
6
7 Promise.prototype._cancel = function (reason) {
8     if (!this.isCancellable()) return this;
9     var parent;
10     var promiseToReject = this;
11     while ((parent = promiseToReject._cancellationParent) !== undefined &&
12         parent.isCancellable()) {
13         promiseToReject = parent;
14     }
15     this._unsetCancellable();
16     promiseToReject._target()._rejectCallback(reason, false, true);
17 };
18
19 Promise.prototype.cancel = function (reason) {
20     if (!this.isCancellable()) return this;
21     if (reason === undefined) reason = new CancellationError();
22     async.invokeLater(this._cancel, this, reason);
23     return this;
24 };
25
26 Promise.prototype.cancellable = function () {
27     if (this._cancellable()) return this;
28     async.enableTrampoline();
29     this._setCancellable();
30     this._cancellationParent = undefined;
31     return this;
32 };
33
34 Promise.prototype.uncancellable = function () {
35     var ret = this.then();
36     ret._unsetCancellable();
37     return ret;
38 };
39
40 Promise.prototype.fork = function (didFulfill, didReject, didProgress) {
41     var ret = this._then(didFulfill, didReject, didProgress,
42                          undefined, undefined);
43
44     ret._setCancellable();
45     ret._cancellationParent = undefined;
46     return ret;
47 };
48 };