2022-01-21 08:28:41 +00:00
|
|
|
"use strict";
|
|
|
|
var __extends = (this && this.__extends) || (function () {
|
|
|
|
var extendStatics = function (d, b) {
|
|
|
|
extendStatics = Object.setPrototypeOf ||
|
|
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
2022-06-08 10:36:39 +00:00
|
|
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
2022-01-21 08:28:41 +00:00
|
|
|
return extendStatics(d, b);
|
2022-06-08 10:36:39 +00:00
|
|
|
};
|
2022-01-21 08:28:41 +00:00
|
|
|
return function (d, b) {
|
2022-06-08 10:36:39 +00:00
|
|
|
if (typeof b !== "function" && b !== null)
|
|
|
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
2022-01-21 08:28:41 +00:00
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2022-06-08 10:36:39 +00:00
|
|
|
exports.AsyncAction = void 0;
|
2022-01-21 08:28:41 +00:00
|
|
|
var Action_1 = require("./Action");
|
2022-06-08 10:36:39 +00:00
|
|
|
var intervalProvider_1 = require("./intervalProvider");
|
|
|
|
var arrRemove_1 = require("../util/arrRemove");
|
2022-01-21 08:28:41 +00:00
|
|
|
var AsyncAction = (function (_super) {
|
|
|
|
__extends(AsyncAction, _super);
|
|
|
|
function AsyncAction(scheduler, work) {
|
|
|
|
var _this = _super.call(this, scheduler, work) || this;
|
|
|
|
_this.scheduler = scheduler;
|
|
|
|
_this.work = work;
|
|
|
|
_this.pending = false;
|
|
|
|
return _this;
|
|
|
|
}
|
|
|
|
AsyncAction.prototype.schedule = function (state, delay) {
|
|
|
|
if (delay === void 0) { delay = 0; }
|
|
|
|
if (this.closed) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
this.state = state;
|
|
|
|
var id = this.id;
|
|
|
|
var scheduler = this.scheduler;
|
|
|
|
if (id != null) {
|
|
|
|
this.id = this.recycleAsyncId(scheduler, id, delay);
|
|
|
|
}
|
|
|
|
this.pending = true;
|
|
|
|
this.delay = delay;
|
|
|
|
this.id = this.id || this.requestAsyncId(scheduler, this.id, delay);
|
|
|
|
return this;
|
|
|
|
};
|
2022-06-08 10:36:39 +00:00
|
|
|
AsyncAction.prototype.requestAsyncId = function (scheduler, _id, delay) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (delay === void 0) { delay = 0; }
|
2022-06-08 10:36:39 +00:00
|
|
|
return intervalProvider_1.intervalProvider.setInterval(scheduler.flush.bind(scheduler, this), delay);
|
2022-01-21 08:28:41 +00:00
|
|
|
};
|
2022-06-08 10:36:39 +00:00
|
|
|
AsyncAction.prototype.recycleAsyncId = function (_scheduler, id, delay) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (delay === void 0) { delay = 0; }
|
2022-06-08 10:36:39 +00:00
|
|
|
if (delay != null && this.delay === delay && this.pending === false) {
|
2022-01-21 08:28:41 +00:00
|
|
|
return id;
|
|
|
|
}
|
2022-06-08 10:36:39 +00:00
|
|
|
intervalProvider_1.intervalProvider.clearInterval(id);
|
2022-01-21 08:28:41 +00:00
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
AsyncAction.prototype.execute = function (state, delay) {
|
|
|
|
if (this.closed) {
|
|
|
|
return new Error('executing a cancelled action');
|
|
|
|
}
|
|
|
|
this.pending = false;
|
|
|
|
var error = this._execute(state, delay);
|
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
else if (this.pending === false && this.id != null) {
|
|
|
|
this.id = this.recycleAsyncId(this.scheduler, this.id, null);
|
|
|
|
}
|
|
|
|
};
|
2022-06-08 10:36:39 +00:00
|
|
|
AsyncAction.prototype._execute = function (state, _delay) {
|
2022-01-21 08:28:41 +00:00
|
|
|
var errored = false;
|
2022-06-08 10:36:39 +00:00
|
|
|
var errorValue;
|
2022-01-21 08:28:41 +00:00
|
|
|
try {
|
|
|
|
this.work(state);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
errored = true;
|
2022-06-08 10:36:39 +00:00
|
|
|
errorValue = e ? e : new Error('Scheduled action threw falsy error');
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
if (errored) {
|
|
|
|
this.unsubscribe();
|
|
|
|
return errorValue;
|
|
|
|
}
|
|
|
|
};
|
2022-06-08 10:36:39 +00:00
|
|
|
AsyncAction.prototype.unsubscribe = function () {
|
|
|
|
if (!this.closed) {
|
|
|
|
var _a = this, id = _a.id, scheduler = _a.scheduler;
|
|
|
|
var actions = scheduler.actions;
|
|
|
|
this.work = this.state = this.scheduler = null;
|
|
|
|
this.pending = false;
|
|
|
|
arrRemove_1.arrRemove(actions, this);
|
|
|
|
if (id != null) {
|
|
|
|
this.id = this.recycleAsyncId(scheduler, id, null);
|
|
|
|
}
|
|
|
|
this.delay = null;
|
|
|
|
_super.prototype.unsubscribe.call(this);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return AsyncAction;
|
|
|
|
}(Action_1.Action));
|
|
|
|
exports.AsyncAction = AsyncAction;
|
|
|
|
//# sourceMappingURL=AsyncAction.js.map
|