0.1.0 • Published 4 months ago

nano-pool v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

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

:sparkles: Features

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

:wrench: Example usage

import { createObjectPool } from 'nano-pool'

const poolSize = 10

const createObject = () => {
  const sprite = new Sprite()
  sprite.scale.set(4)
  return sprite
}

const objectPool = createObjectPool(poolSize, createObject)

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

:package: Install

npm install nano-pool

:newspaper: API

createObjectPool<T>(size: number, createObject: (index: number) => T): ObjectPool
type ObjectPool<T> = {
  get: () => T
  release: (object: T) => void
  releaseAll: () => void
  countAll: () => number
}

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.

0.1.0

4 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

2 years ago