@awayjs/stage
Version:
Stage for AwayJS
122 lines (121 loc) • 4.33 kB
JavaScript
function sortManagersFunct(a, b) {
return a.priority - b.priority;
}
var UnloadService = /** @class */ (function () {
function UnloadService() {
}
UnloadService.createManager = function (options) {
options = Object.assign({
name: UnloadManager.name + ':' + this.managers.length
}, options || {});
var manager = new UnloadManager(options);
this.managers.push(manager);
this.managers.sort(sortManagersFunct);
return manager;
};
UnloadService.executeAll = function () {
for (var _i = 0, _a = this.managers; _i < _a.length; _i++) {
var m = _a[_i];
var count = m.execute();
if (count) {
console.debug("[UnloadService:".concat(m.name, ", tick: ").concat(this.tick, "] unloaded:"), count);
}
}
this.tick++;
};
UnloadService.clearAll = function () {
for (var _i = 0, _a = this.managers; _i < _a.length; _i++) {
var m = _a[_i];
m.clear();
}
this.tick = 0;
this.managers.length = 0;
};
UnloadService.tick = 0;
UnloadService.managers = [];
return UnloadService;
}());
export { UnloadService };
var UnloadManager = /** @class */ (function () {
function UnloadManager(options) {
var _a;
this._tasks = new Set();
this.name = UnloadManager.name;
this.priority = 0;
this.keepAliveTime = 10000;
this.exectuionTimeout = 5000;
this.unloadTaskShift = 1000;
this.maxUnloadTask = 100;
this._lastExecutionTime = 0;
this._lastCorrectedTime = -1;
if (options) {
this.name = options.name || this.name;
this.priority = (_a = options.priority) !== null && _a !== void 0 ? _a : this.priority;
this.keepAliveTime = options.keepAliveTime || this.keepAliveTime;
this.maxUnloadTask = options.maxUnloadTasks || this.maxUnloadTask;
this.exectuionTimeout = options.exectionPeriod || this.exectuionTimeout;
}
}
Object.defineProperty(UnloadManager.prototype, "correctedTime", {
get: function () {
var time = performance.now();
if (this._lastCorrectedTime < 0 || this._lastCorrectedTime - time >= UnloadManager.MIN_QUANT) {
time += UnloadManager.MIN_QUANT;
}
return this._lastCorrectedTime = time;
},
enumerable: false,
configurable: true
});
UnloadManager.prototype.addTask = function (task) {
if (!task || this._tasks.has(task)) {
return false;
}
this._tasks.add(task);
return true;
};
UnloadManager.prototype.removeTask = function (task) {
return this._tasks.delete(task);
};
UnloadManager.prototype.execute = function (force) {
if (force === void 0) { force = false; }
if (!this._tasks.size) {
return;
}
var time = this.correctedTime;
if (!force) {
if (!this._lastExecutionTime) {
this._lastExecutionTime = time;
return;
}
if (time - this._lastExecutionTime < this.exectuionTimeout) {
return;
}
}
this._lastExecutionTime = time;
var unloaded = [];
var values = this._tasks.values();
for (var i = 0, l = this._tasks.size; i < l; i++) {
var t = values.next().value;
if (t.canUnload && time - t.lastUsedTime > (t.keepAliveTime || this.keepAliveTime)) {
t.unload();
unloaded[unloaded.length] = t;
if (unloaded.length >= this.maxUnloadTask) {
this._lastExecutionTime -= (this.exectuionTimeout - this.unloadTaskShift);
break;
}
}
}
for (var _i = 0, unloaded_1 = unloaded; _i < unloaded_1.length; _i++) {
var u = unloaded_1[_i];
this._tasks.delete(u);
}
return unloaded.length;
};
UnloadManager.prototype.clear = function () {
this._tasks.clear();
};
UnloadManager.MIN_QUANT = 32; // minimal time between task
return UnloadManager;
}());
export { UnloadManager };