/usr/lib/thunderbird-addons/extensions/{a62ef8ec-5fdc-40c2-873c-223b8a6925cc}/modules/shim/Promise.jsm is in xul-ext-gdata-provider 1:38.6.0+build1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | /* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var EXPORTED_SYMBOLS = ["Promise"];
const STATUS_PENDING = 0;
const STATUS_RESOLVED = 1;
const STATUS_REJECTED = 2;
function log(msg) {
// Enable this to debug promises
//dump("PROMISES: " + msg + "\n");
}
var PromiseWalker = {
handlers: [],
completePromise: function(aPromise, aStatus, aValue) {
if (aPromise._status != STATUS_PENDING) {
return;
}
if (aStatus == STATUS_RESOLVED && aValue &&
typeof(aValue.then) == "function") {
aValue.then(this.completePromise.bind(this, aPromise, STATUS_RESOLVED),
this.completePromise.bind(this, aPromise, STATUS_REJECTED));
return;
}
aPromise._status = aStatus;
aPromise._value = aValue;
if (aPromise._handlers.length > 0) {
this.schedulePromise(aPromise);
} else if (aStatus == STATUS_REJECTED) {
log("Pending error: " + aValue);
}
},
scheduleWalkerLoop: function() {
this.walkerLoopScheduled = true;
let thread = Components.classes["@mozilla.org/thread-manager;1"]
.getService(Components.interfaces.nsIThreadManager)
.currentThread;
thread.dispatch({
run: function() {
PromiseWalker.walkerLoop();
}
}, Components.interfaces.nsIEventTarget.DISPATCH_NORMAL);
},
schedulePromise: function (aPromise) {
for each (let handler in aPromise._handlers) {
this.handlers.push(handler);
}
aPromise._handlers.length = 0;
if (!this.walkerLoopScheduled) {
this.scheduleWalkerLoop();
}
},
walkerLoopScheduled: false,
walkerLoop: function() {
if (this.handlers.length > 1) {
this.scheduleWalkerLoop();
} else {
this.walkerLoopScheduled = false;
}
while (this.handlers.length > 0) {
this.handlers.shift().process();
}
}
};
PromiseWalker.walkerLoop = PromiseWalker.walkerLoop.bind(PromiseWalker);
function Handler(aThisPromise, aOnResolve, aOnReject) {
this.thisPromise = aThisPromise;
this.onResolve = aOnResolve;
this.onReject = aOnReject;
this.nextPromise = new Promise(function() {});
}
Handler.prototype = {
process: function() {
let nextStatus = this.thisPromise._status;
let nextValue = this.thisPromise._value;
try {
if (nextStatus == STATUS_RESOLVED) {
if (typeof(this.onResolve) == "function") {
nextValue = this.onResolve.call(undefined, nextValue);
}
} else if (typeof(this.onReject) == "function") {
nextValue = this.onReject.call(undefined, nextValue);
nextStatus = STATUS_RESOLVED;
}
} catch (ex) {
// Enable this to get promise debugging
log("EXCEPTION: " + ex + "\n" + ex.stack + ex.printStackTrace);
nextStatus = STATUS_REJECTED;
nextValue = ex;
}
PromiseWalker.completePromise(this.nextPromise, nextStatus, nextValue);
}
};
function Deferred() {
this.promise = new Promise(function(aResolve, aReject) {
this.resolve = aResolve;
this.reject = aReject;
}.bind(this));
}
function Promise(executor) {
this._handlers = [];
let resolve = PromiseWalker.completePromise.bind(PromiseWalker, this, STATUS_RESOLVED);
let reject = PromiseWalker.completePromise.bind(PromiseWalker, this, STATUS_REJECTED);
try {
executor.call(undefined, resolve, reject);
} catch (ex) {
reject(ex);
}
}
Promise.prototype = {
_status: STATUS_PENDING,
_value: undefined,
then: function(aOnResolve, aOnReject) {
let handler = new Handler(this, aOnResolve, aOnReject);
this._handlers.push(handler);
if (this._status != STATUS_PENDING) {
PromiseWalker.schedulePromise(this);
}
return handler.nextPromise;
},
catch: function(onreject) {
return this.then(undefined, onReject);
}
};
Promise.defer = function() {
return new Deferred();
};
Promise.resolve = function(aValue) {
if (aValue instanceof Promise) {
return aValue;
}
return new Promise(function(aResolve) aResolve(aValue));
};
Promise.reject = function(aReason) {
return new Promise(function(_, aReject) aReject(aReason));
}
|