Merge branch 'master' of https://github.com/esengine/ecs-framework
# Conflicts: # extensions/ecs-tween/lib/framework.d.ts
This commit is contained in:
@@ -3,7 +3,7 @@ module es {
|
||||
* 用于池任何对象
|
||||
*/
|
||||
export class Pool {
|
||||
private static _objectQueue = [];
|
||||
private static _objectQueue: Map<any, any[]> = new Map();
|
||||
|
||||
/**
|
||||
* 预热缓存,使用最大的cacheCount对象填充缓存
|
||||
@@ -11,10 +11,11 @@ module es {
|
||||
* @param cacheCount
|
||||
*/
|
||||
public static warmCache<T>(type: new (...args) => T, cacheCount: number) {
|
||||
cacheCount -= this._objectQueue.length;
|
||||
this.checkCreate(type);
|
||||
cacheCount -= this._objectQueue.get(type).length;
|
||||
if (cacheCount > 0) {
|
||||
for (let i = 0; i < cacheCount; i++) {
|
||||
this._objectQueue.unshift(new type());
|
||||
this._objectQueue.get(type).unshift(new type());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,24 +24,27 @@ module es {
|
||||
* 将缓存修剪为cacheCount项目
|
||||
* @param cacheCount
|
||||
*/
|
||||
public static trimCache(cacheCount: number) {
|
||||
while (cacheCount > this._objectQueue.length)
|
||||
this._objectQueue.shift();
|
||||
public static trimCache<T>(type: new (...args) => T, cacheCount: number) {
|
||||
this.checkCreate(type);
|
||||
while (cacheCount > this._objectQueue.get(type).length)
|
||||
this._objectQueue.get(type).shift();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
*/
|
||||
public static clearCache() {
|
||||
this._objectQueue.length = 0;
|
||||
public static clearCache<T>(type: new (...args) => T) {
|
||||
this.checkCreate(type);
|
||||
this._objectQueue.get(type).length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果可以的话,从堆栈中弹出一个项
|
||||
*/
|
||||
public static obtain<T>(type: new (...args) => T): T {
|
||||
if (this._objectQueue.length > 0)
|
||||
return this._objectQueue.shift();
|
||||
this.checkCreate(type);
|
||||
if (this._objectQueue.get(type).length > 0)
|
||||
return this._objectQueue.get(type).shift();
|
||||
|
||||
return new type() as T;
|
||||
}
|
||||
@@ -49,13 +53,19 @@ module es {
|
||||
* 将项推回堆栈
|
||||
* @param obj
|
||||
*/
|
||||
public static free<T>(obj: T) {
|
||||
this._objectQueue.unshift(obj);
|
||||
public static free<T>(type: new (...args) => T, obj: T) {
|
||||
this.checkCreate(type);
|
||||
this._objectQueue.get(type).unshift(obj);
|
||||
|
||||
if (isIPoolable(obj)) {
|
||||
obj["reset"]();
|
||||
}
|
||||
}
|
||||
|
||||
private static checkCreate<T>(type: new (...args) => T) {
|
||||
if (!this._objectQueue.get(type))
|
||||
this._objectQueue.set(type, []);
|
||||
}
|
||||
}
|
||||
|
||||
export interface IPoolable {
|
||||
|
||||
Reference in New Issue
Block a user