- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
import { ObjectPool } from './ObjectPool';
/**
* This stores existing object pools created for class-constructed objects.
*
* @ignore
*/
const poolMap: Map<{ new(): any }, ObjectPool<any>> = new Map();
/**
* Factory for creating pools of objects with default constructors. It will store the pool of
* a given type and reuse it on further builds.
*
* @class
* @public
* @example
* ```js
* import { ObjectPool, ObjectPoolFactory } from '@pixi-essentials/object-pool';
*
* class AABB {}
*
* const opool: ObjectPool<AABB> = ObjectPoolFactory.build(AABB) as ObjectPool<AABB>;
*
* const temp = opool.borrowObject();
* // do something
* opool.returnObject(temp);
* ```
*/
export class ObjectPoolFactory
{
/**
* Builds an object-pool for objects constructed from the given class with a default constructor. If an
* object pool for that class was already created, an existing instance is returned.
*
* @param classConstructor
*/
static build<T>(Type: { new(): T }): ObjectPool<T>
{
let pool = poolMap.get(Type);
if (pool)
{
return pool;
}
pool = new (class DefaultObjectPool extends ObjectPool<any>
{
create(): any
{
return new Type();
}
})();
poolMap.set(Type, pool);
return pool;
}
/**
* Builds an object-pool for objects built using a factory function. The factory function's context will be the
* object-pool.
*
* These types of pools are not cached and should only be used on internal data structures.
*
* @param factoryFunction
*/
static buildFunctional<T>(factoryFunction: () => T): ObjectPool<T>
{
return new (class DefaultObjectPool extends ObjectPool<T>
{
create = factoryFunction;
})();
}
}