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

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-08-28 19:12:21 +08:00
module es {
/**
*
*/
export class Pool<T> {
private static _objectQueue = new Array(10);
/**
* 使cacheCount对象填充缓存
* @param type
* @param cacheCount
*/
public static warmCache(type: any, cacheCount: number){
cacheCount -= this._objectQueue.length;
if (cacheCount > 0) {
for (let i = 0; i < cacheCount; i++) {
this._objectQueue.unshift(new type());
}
}
}
/**
* cacheCount项目
* @param cacheCount
*/
public static trimCache(cacheCount: number){
while (cacheCount > this._objectQueue.length)
this._objectQueue.shift();
}
/**
*
*/
public static clearCache() {
this._objectQueue.length = 0;
}
/**
*
*/
public static obtain<T>(type: any): T {
if (this._objectQueue.length > 0)
return this._objectQueue.shift();
return new type() as T;
}
/**
*
* @param obj
*/
public static free<T>(obj: T) {
this._objectQueue.unshift(obj);
if (egret.is(obj, "IPoolable")){
obj["reset"]();
}
}
}
export interface IPoolable {
/**
*
*/
reset();
}
}