|
@@ -8280,10 +8280,19 @@ var BABYLON;
|
|
|
PromiseStates[PromiseStates["Fulfilled"] = 1] = "Fulfilled";
|
|
|
PromiseStates[PromiseStates["Rejected"] = 2] = "Rejected";
|
|
|
})(PromiseStates || (PromiseStates = {}));
|
|
|
- var Promise = /** @class */ (function () {
|
|
|
- function Promise(resolver) {
|
|
|
+ var FulFillmentAgregator = /** @class */ (function () {
|
|
|
+ function FulFillmentAgregator() {
|
|
|
+ this.count = 0;
|
|
|
+ this.target = 0;
|
|
|
+ this.results = [];
|
|
|
+ }
|
|
|
+ return FulFillmentAgregator;
|
|
|
+ }());
|
|
|
+ var InternalPromise = /** @class */ (function () {
|
|
|
+ function InternalPromise(resolver) {
|
|
|
var _this = this;
|
|
|
this._state = PromiseStates.Pending;
|
|
|
+ this._rejectWasConsumed = false;
|
|
|
if (!resolver) {
|
|
|
return;
|
|
|
}
|
|
@@ -8298,59 +8307,58 @@ var BABYLON;
|
|
|
this._reject(e.message);
|
|
|
}
|
|
|
}
|
|
|
- Object.defineProperty(Promise.prototype, "state", {
|
|
|
+ Object.defineProperty(InternalPromise.prototype, "state", {
|
|
|
get: function () {
|
|
|
return this._state;
|
|
|
},
|
|
|
enumerable: true,
|
|
|
configurable: true
|
|
|
});
|
|
|
- Promise.prototype.isFulfilled = function () {
|
|
|
+ InternalPromise.prototype.isFulfilled = function () {
|
|
|
return this._state === PromiseStates.Fulfilled;
|
|
|
};
|
|
|
- Promise.prototype.isRejected = function () {
|
|
|
+ InternalPromise.prototype.isRejected = function () {
|
|
|
return this._state === PromiseStates.Rejected;
|
|
|
};
|
|
|
- Promise.prototype.isPending = function () {
|
|
|
+ InternalPromise.prototype.isPending = function () {
|
|
|
return this._state === PromiseStates.Pending;
|
|
|
};
|
|
|
- Promise.prototype.value = function () {
|
|
|
+ InternalPromise.prototype.value = function () {
|
|
|
if (!this.isFulfilled()) {
|
|
|
throw new Error("Promise is not fulfilled");
|
|
|
}
|
|
|
return this._result;
|
|
|
};
|
|
|
- Promise.prototype.reason = function () {
|
|
|
+ InternalPromise.prototype.reason = function () {
|
|
|
if (!this.isRejected()) {
|
|
|
throw new Error("Promise is not rejected");
|
|
|
}
|
|
|
return this._reason;
|
|
|
};
|
|
|
- Promise.prototype.catch = function (onRejected) {
|
|
|
+ InternalPromise.prototype.catch = function (onRejected) {
|
|
|
return this.then(undefined, onRejected);
|
|
|
};
|
|
|
- Promise.prototype.then = function (onFulfilled, onRejected) {
|
|
|
- var newPromise = new Promise();
|
|
|
+ InternalPromise.prototype.then = function (onFulfilled, onRejected) {
|
|
|
+ var newPromise = new InternalPromise();
|
|
|
newPromise._onFulfilled = onFulfilled;
|
|
|
newPromise._onRejected = onRejected;
|
|
|
// Composition
|
|
|
this._child = newPromise;
|
|
|
- switch (this._state) {
|
|
|
- case PromiseStates.Fulfilled:
|
|
|
+ if (this._state !== PromiseStates.Pending) {
|
|
|
+ if (this._state === PromiseStates.Fulfilled || this._rejectWasConsumed) {
|
|
|
var returnedPromise = newPromise._resolve(this._result);
|
|
|
if (returnedPromise) {
|
|
|
newPromise._child = returnedPromise;
|
|
|
newPromise = returnedPromise;
|
|
|
}
|
|
|
- break;
|
|
|
- case PromiseStates.Rejected:
|
|
|
+ }
|
|
|
+ else {
|
|
|
newPromise._reject(this._reason);
|
|
|
- newPromise._state = PromiseStates.Fulfilled;
|
|
|
- break;
|
|
|
+ }
|
|
|
}
|
|
|
return newPromise;
|
|
|
};
|
|
|
- Promise.prototype._resolve = function (value) {
|
|
|
+ InternalPromise.prototype._resolve = function (value) {
|
|
|
try {
|
|
|
this._state = PromiseStates.Fulfilled;
|
|
|
this._result = value;
|
|
@@ -8368,31 +8376,69 @@ var BABYLON;
|
|
|
}
|
|
|
return null;
|
|
|
};
|
|
|
- Promise.prototype._reject = function (reason) {
|
|
|
+ InternalPromise.prototype._reject = function (reason) {
|
|
|
this._state = PromiseStates.Rejected;
|
|
|
this._reason = reason;
|
|
|
if (this._onRejected) {
|
|
|
this._onRejected(reason);
|
|
|
+ this._rejectWasConsumed = true;
|
|
|
}
|
|
|
if (this._child) {
|
|
|
- this._child._resolve(null);
|
|
|
+ if (this._rejectWasConsumed) {
|
|
|
+ this._child._resolve(null);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ this._child._reject(reason);
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
- Promise.resolve = function (value) {
|
|
|
- var newPromise = new Promise();
|
|
|
+ InternalPromise.resolve = function (value) {
|
|
|
+ var newPromise = new InternalPromise();
|
|
|
newPromise._resolve(value);
|
|
|
return newPromise;
|
|
|
};
|
|
|
- return Promise;
|
|
|
+ InternalPromise._RegisterForFulfillment = function (promise, agregator, index) {
|
|
|
+ promise.then(function (value) {
|
|
|
+ agregator.results[index] = value;
|
|
|
+ agregator.count++;
|
|
|
+ if (agregator.count === agregator.target) {
|
|
|
+ agregator.rootPromise._resolve(agregator.results);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }, function (reason) {
|
|
|
+ if (!agregator.rootPromise.isRejected) {
|
|
|
+ agregator.rootPromise._reject(reason);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ };
|
|
|
+ InternalPromise.all = function (promises) {
|
|
|
+ var newPromise = new InternalPromise();
|
|
|
+ var agregator = new FulFillmentAgregator();
|
|
|
+ agregator.target = promises.length;
|
|
|
+ agregator.rootPromise = newPromise;
|
|
|
+ for (var index = 0; index < promises.length; index++) {
|
|
|
+ InternalPromise._RegisterForFulfillment(promises[index], agregator, index);
|
|
|
+ }
|
|
|
+ return newPromise;
|
|
|
+ };
|
|
|
+ return InternalPromise;
|
|
|
}());
|
|
|
+ /**
|
|
|
+ * Helper class that provides a small promise polyfill
|
|
|
+ */
|
|
|
var PromisePolyfill = /** @class */ (function () {
|
|
|
function PromisePolyfill() {
|
|
|
}
|
|
|
+ /**
|
|
|
+ * Static function used to check if the polyfill is required
|
|
|
+ * If this is the case then the function will inject the polyfill to window.Promise
|
|
|
+ * @param force defines a boolean used to force the injection (mostly for testing purposes)
|
|
|
+ */
|
|
|
PromisePolyfill.Apply = function (force) {
|
|
|
if (force === void 0) { force = false; }
|
|
|
if (force || typeof Promise === 'undefined') {
|
|
|
var root = window;
|
|
|
- root.Promise = Promise;
|
|
|
+ root.Promise = InternalPromise;
|
|
|
}
|
|
|
};
|
|
|
return PromisePolyfill;
|