easy-object-pool

an easy way to use object pool

Usage no npm install needed!

<script type="module">
  import easyObjectPool from 'https://cdn.skypack.dev/easy-object-pool';
</script>

README

Easy Object Pool

an easy way to use object pool

Install

yarn add easy-object-pool
or
npm i easy-object-pool -S

Usage

import {useObjectPool} from 'easy-object-pool'

class Cat {
}

const [getACat, recycleCat] = useObjectPool<Cat>(() => new Cat())

//create a Cat instance
const cat = getACat()

//recycle a Cat instance
recycleCat(cat)

Api

useObjectPool

function useObjectPool<T>(factoryMethod: (...params: any[]) => T, options?: {
    initializationMethod?: (instance: T, ...params: any[]) => void;
    disposeMethod?: (instance: T, ...params: any[]) => void;
    preInstantiationQuantity?: number;
    limit?: number;
}): [(...params: any[]) => T, (instance: T | T[]) => void, () => void]

Options interface

field type required default description
factoryMethod see Declare factory method to instantiate a instance
initializationMethod see Declare initialize a instance when get a instance
disposeMethod see Declare call if clean instances
preInstantiationQuantity number 0 instantiate some instances at ahead
limit number 0 set a limit quantity of pool

Returns

[getAInstance, recycleInstance, clear]

field type description
getAInstance see Declare get a instance
recycleInstance see Declare recycle a instance or instances
clear see Declare clean all instances

clearAll

function clearAll()