新增poolcomponent 对象池组件

This commit is contained in:
yhh
2020-06-30 22:22:52 +08:00
parent 099aa749e1
commit 088891c5ca
11 changed files with 217 additions and 60 deletions

View File

@@ -0,0 +1,22 @@
class ComponentPool<T extends PooledComponent>{
private _cache: T[];
private _type: any;
constructor(typeClass: any){
this._type = typeClass;
this._cache = [];
}
public obtain(): T{
try {
return this._cache.length > 0 ? this._cache.shift() : new this._type();
} catch(err){
throw new Error(this._type + err);
}
}
public free(component: T){
component.reset();
this._cache.push(component);
}
}

View File

@@ -0,0 +1,4 @@
/** 回收实例的组件类型。 */
abstract class PooledComponent extends Component {
public abstract reset();
}