Files
esengine/source/src/Utils/ListPool.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-07-23 11:00:46 +08:00
module es {
/**
2020-07-23 11:00:46 +08:00
*
*/
2020-07-23 11:00:46 +08:00
export class ListPool {
private static readonly _objectQueue = [];
/**
* 使cacheCount对象填充缓存
* @param cacheCount
*/
2020-07-28 16:25:20 +08:00
public static warmCache(cacheCount: number) {
2020-07-23 11:00:46 +08:00
cacheCount -= this._objectQueue.length;
2020-07-28 16:25:20 +08:00
if (cacheCount > 0) {
for (let i = 0; i < cacheCount; i++) {
2020-07-23 11:00:46 +08:00
this._objectQueue.unshift([]);
}
}
}
2020-07-23 11:00:46 +08:00
/**
* cacheCount项目
* @param cacheCount
*/
2020-07-28 16:25:20 +08:00
public static trimCache(cacheCount) {
2020-07-23 11:00:46 +08:00
while (cacheCount > this._objectQueue.length)
this._objectQueue.shift();
}
2020-07-23 11:00:46 +08:00
/**
*
*/
2020-07-28 16:25:20 +08:00
public static clearCache() {
2020-07-23 11:00:46 +08:00
this._objectQueue.length = 0;
}
2020-07-23 11:00:46 +08:00
/**
*
*/
public static obtain<T>(): T[] {
if (this._objectQueue.length > 0)
return this._objectQueue.shift();
2020-07-23 11:00:46 +08:00
return [];
}
2020-07-23 11:00:46 +08:00
/**
*
* @param obj
*/
2020-07-28 16:25:20 +08:00
public static free<T>(obj: Array<T>) {
2020-07-23 11:00:46 +08:00
this._objectQueue.unshift(obj);
obj.length = 0;
}
}
2020-07-23 11:00:46 +08:00
}