Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SlowPool<TObject>

An object pool allows to reuse the same instance of an object multiple times without the relatively costly operation of instantiating it. It's especially useful when you need to create and destroy objects quickly (eg. bullets/particles) or create a lot of them at once (eg. loading a level).

This class will prevent the following incorrect usages:

  • Hydrating the pool with an instance that's already stored in the pool.
  • Releasing an instance that's already released/never was retrieved.
  • Releasing an instance that has not been retrieved form the pool in the first place.

Type parameters

  • TObject: object

    Type of the objects stored in the pool

Hierarchy

  • SlowPool

Implements

Index

Constructors

constructor

  • Parameters

    • onHydrate: PoolHydrateCallback<TObject>

      A function that's called when hydrating the pool, which must return new instances of objects to populate it. It must return a different instance each time.

    • Optional onRelease: PoolReleaseCallback<TObject>

      An optional function called when an object is released.

    • Optional config: PoolConfig

      Configuration

    Returns SlowPool

Accessors

availableObjects

  • get availableObjects(): number
  • The number of objects left in the pool

    Returns number

totalObjects

  • get totalObjects(): number
  • The number of used objects in the pool

    Returns number

usedObjects

  • get usedObjects(): number
  • The total number of objects created by this pool

    Returns number

Methods

getOne

  • getOne(): TObject
  • Retrieves a single object. If the pool has no available objects it first hydrates the pool.

    Returns TObject

    The object retrieved from the pool

hydrate

  • hydrate(objectsToCreate: number): void
  • Enlarges the list of available objects by the specified number.

    throws

    {Error} Thrown when the onHydrate callback provided in the constructor returns an instance that is already in the pool.

    Parameters

    • objectsToCreate: number

      Number of objects to create

    Returns void

release

  • release(object: TObject): void
  • Releases an object back into the pool

    throws

    {Error} Thrown when releasing an object that's not been retrieved from the pool in the first place.

    throws

    {Error} Thrown when releasing an object that's already available for use.

    Parameters

    • object: TObject

      The object to be released. It should be an object previously retrieved from the pool.)

    Returns void

Generated using TypeDoc