ecsy
Version:
Entity Component System in JS
60 lines (47 loc) • 1.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ObjectPool = void 0;
class ObjectPool {
// @todo Add initial size
constructor(T, initialSize) {
this.freeList = [];
this.count = 0;
this.T = T;
this.isObjectPool = true;
if (typeof initialSize !== "undefined") {
this.expand(initialSize);
}
}
acquire() {
// Grow the list by 20%ish if we're out
if (this.freeList.length <= 0) {
this.expand(Math.round(this.count * 0.2) + 1);
}
var item = this.freeList.pop();
return item;
}
release(item) {
item.reset();
this.freeList.push(item);
}
expand(count) {
for (var n = 0; n < count; n++) {
var clone = new this.T();
clone._pool = this;
this.freeList.push(clone);
}
this.count += count;
}
totalSize() {
return this.count;
}
totalFree() {
return this.freeList.length;
}
totalUsed() {
return this.count - this.freeList.length;
}
}
exports.ObjectPool = ObjectPool;