@awayjs/stage
Version:
Stage for AwayJS
60 lines (59 loc) • 2.3 kB
JavaScript
import { UnloadService } from '../managers/UnloadManager';
import { Settings } from '../Settings';
var BufferPool = /** @class */ (function () {
function BufferPool(_tCtrl, maxStoreSize) {
if (maxStoreSize === void 0) { maxStoreSize = Settings.MAX_BUFFER_POOL_SIZE; }
this._tCtrl = _tCtrl;
this.maxStoreSize = maxStoreSize;
this._store = [];
if (Settings.ENABLE_UNLOAD_BUFFER) {
this._manager = UnloadService.createManager({
name: _tCtrl.name,
keepAliveTime: Settings.MAX_BUFFER_ALIVE_TIME
});
}
}
BufferPool.prototype.create = function (context, arg1, arg2, arg3) {
var el = this._store.pop();
if (!el) {
el = new this._tCtrl(context, arg1, arg2, arg3);
el.id = BufferPool.GLOBAL_POOL_ID++;
//console.debug('[Buffer Pool], Contstuct new buffer:', this._tCtrl.name, el.id);
}
else if (Settings.ENABLE_UNLOAD_BUFFER) {
this._manager.removeTask(el);
el.lastUsedTime = this._manager.correctedTime;
}
el.apply && el.apply(context, arg1, arg2, arg3);
return el;
};
BufferPool.prototype.store = function (el) {
if (!Settings.ENABLE_BUFFER_POOLING || this._store.length === this.maxStoreSize)
return false;
this._store.push(el);
if (Settings.ENABLE_UNLOAD_BUFFER) {
el.lastUsedTime = this._manager.correctedTime;
this._manager.addTask(el);
//console.debug('[Buffer Pool], Store buffer and task to unload:', el.id, this._store.length);
}
else {
//console.debug('[Buffer Pool], Store buffer without unload:', el.id, this._store.length);
}
return true;
};
BufferPool.prototype.remove = function (el) {
var index = this._store.indexOf(el);
if (index > -1) {
//console.debug('[Buffer Pool], remove element from pool:', el.id);
this._store.splice(index, 1);
return true;
}
return false;
};
BufferPool.prototype.clear = function () {
this._manager && this._manager.clear();
};
BufferPool.GLOBAL_POOL_ID = 0;
return BufferPool;
}());
export { BufferPool };