easy-object-pool
TypeScript icon, indicating that this package has built-in type declarations

1.0.12 • Public • Published

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()

Package Sidebar

Install

npm i easy-object-pool

Weekly Downloads

11

Version

1.0.12

License

MIT

Unpacked Size

10.9 kB

Total Files

10

Last publish

Collaborators

  • rockyf