2020-07-23 11:00:46 +08:00
|
|
|
|
module es {
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
2020-07-23 11:00:46 +08:00
|
|
|
|
* 可以用于列表池的简单类
|
2020-06-16 09:10:09 +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-06-16 09:10:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-06-16 09:10:09 +08:00
|
|
|
|
|
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-06-16 09:10:09 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 如果可以的话,从堆栈中弹出一个项
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static obtain<T>(): T[] {
|
|
|
|
|
|
if (this._objectQueue.length > 0)
|
|
|
|
|
|
return this._objectQueue.shift();
|
2020-06-16 09:10:09 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return [];
|
|
|
|
|
|
}
|
2020-06-16 09:10:09 +08:00
|
|
|
|
|
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-06-16 09:10:09 +08:00
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|