+
Skip to content

sajmoni/nano-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nano-pool

A tiny TypeScript object pool

Allows you to pre-instantiate objects and then request them from a pool

✨ Features

  • Tiny bundle size
  • Written in TypeScript
  • Zero dependencies
  • Adheres to the Unity game engine's naming conventions

🔧 Example usage

import { createObjectPool } from 'nano-pool'

const poolSize = 10

const createObject = () => new Sprite()

const objectPool = createObjectPool(poolSize, createObject, {
  onRelease: (sprite) => {
    sprite.scale.set(4)
  },
})

const object1 = objectPool.get()
objectPool.release(object1)
objectPool.releaseAll()

📦 Install

npm install nano-pool

📰 API

createObjectPool<T>(size: number, createObject: (index: number) => T, options?: Options): ObjectPool
type ObjectPool<T> = {
  get: () => T
  release: (object: T) => void
  releaseAll: () => void
  countAll: () => number
}
type Options<T> = {
  /** Improves debug output */
  id?: string
  /** Called when object is released back into the pool
   *
   * Note: This is also called when the object is created, so that any reset logic can be shared
   */
  onRelease?: (object: T) => void
}

size

The amount of objects to create

createObject

The function that will be used to create new objects


Rules

There are no more objects in the pool when take is called

In development: an error will be thrown.

In production: a new object is created and added to the pool.

An object already in the pool is released

In development: an error will be thrown.

In production: no-op.

About

A tiny TypeScript object pool

Resources

License

Stars

Watchers

Forks

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载