@kaltura-ng/kaltura-client
Version:
Kaltura Typescript client
56 lines • 2 kB
JavaScript
var CancelableAction = (function () {
function CancelableAction(executor) {
this._executors = [];
this._onCancel = executor(this._onResolve.bind(this), this._onReject.bind(this));
}
CancelableAction.prototype._onResolve = function (value) {
this._notifyExecutor(value, true);
};
CancelableAction.prototype._onReject = function (reason) {
this._notifyExecutor(reason, false);
};
CancelableAction.prototype._notifyExecutor = function (value, isResolved) {
var nextExecutor = this._executors.length > 0 ? this._executors.splice(0, 1)[0] : null;
var newValue;
if (nextExecutor) {
try {
if (isResolved) {
newValue = nextExecutor.resolve(value);
}
else {
if (nextExecutor.reject) {
newValue = nextExecutor.reject(value);
}
else {
this._handleErrorOriginatedFromExecuter(value);
}
}
newValue = typeof newValue === 'undefined' ? value : newValue;
this._notifyExecutor(newValue, true);
}
catch (e) {
this._handleErrorOriginatedFromExecuter(e);
}
}
};
CancelableAction.prototype._handleErrorOriginatedFromExecuter = function (reason) {
if (this._executors.length > 0) {
this._notifyExecutor(reason, false);
}
else {
throw reason;
}
};
CancelableAction.prototype.cancel = function () {
if (this._onCancel) {
this._onCancel();
}
};
CancelableAction.prototype.then = function (resolve, reject) {
this._executors.push({ resolve: resolve, reject: reject });
return this;
};
return CancelableAction;
}());
export { CancelableAction };
//# sourceMappingURL=cancelable-action.js.map