export class Pool < T > { private _container : T [ ] = [ ] ; constructor ( private readonly factory ) { } get ( ) { if ( this . _container . length > 0 ) { return this . _container . pop ( ) ; } else { return this . factory ( ) ; } } release ( e ) ; release ( e ) ; release ( e ) { if ( Array . isArray ( e ) ) { this . _container . push ( ... e ) ; } else { this . _container . push ( e ) ; } } } export class CollectionPool < T , TData > { private _currentCollection : T [ ] = null ; private readonly _pool : T [ ] = [ ] ; constructor ( private readonly TFactory , private readonly initializer , private readonly disposer ) { } public get currentCollection ( ) { return this . _currentCollection ; } public get poolSize ( ) { return this . _pool . length ; } map ( source ) { this . cleanup ( ) ; const collection = [ ] ; source . forEach ( s => { let obj ; if ( this . _pool . length > 0 ) { obj = this . _pool . shift ( ) ; } else { obj = new this . TFactory ( ) ; } if ( this . initializer ) { this . initializer ( obj , s ) ; } collection . push ( obj ) ; } ) ; this . _currentCollection = collection ; return this . _currentCollection ; } cleanup ( ) { if ( this . _currentCollection && this . _currentCollection . length > 0 ) { if ( this . disposer ) { this . _currentCollection . forEach ( o => this . disposer ( o ) ) ; } this . _pool . push ( ... this . _currentCollection ) ; } } }