Skip to content

RockyF/easy-object-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

easy-object-pool

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published