Skip to main content

ArenaPool <Type, Args>

This pool works like an arena allocator, it preallocates a large list of objects and passes them out sequentially It is meant to be freed all at once, so things that can be reclaimed after a frame are the ideal type.

If you need to take singular objects and put singular objects back RentalPool is ideal.

The builder can take arguments: ArenaPool.getWith passes them to the builder for a new instance or to the recycler for a reused one. get(), preallocate() and done() still build with no arguments, so give the builder's parameters defaults.

const enemies = new ArenaPool(
  (type: EnemyType = EnemyType.Basic) => new Enemy(type),
  (enemy, type = EnemyType.Basic) => enemy.reset(type)
);
const rocket = enemies.getWith(EnemyType.Rocket);

Index

Constructors

constructor

  • ArenaPool<Type, Args>(builder: (...args: [] | Args) => Type, recycler?: NoInfer<(instance: Type, ...args: Args) => Type>, maxObjects?: number): ArenaPool<Type, Args>
  • Type parameters

    • Type
    • Args: unknown[] = []

    Parameters

    • builder: (...args: [] | Args) => Type
      • optionalrecycler: NoInfer<(instance: Type, ...args: Args) => Type>
      • maxObjects: number = 100

      Returns ArenaPool<Type, Args>

    Properties

    publicbuilder

    builder: (...args: [] | Args) => Type

    Type declaration

      • (...args: [] | Args): Type
      • Parameters

        • rest...args: [] | Args

        Returns Type

    publicdisableWarnings

    disableWarnings: boolean = false

    publicindex

    index: number = 0

    publicmaxObjects

    maxObjects: number = 100

    publicobjects

    objects: Type[] = []

    publicoptionalrecycler

    recycler?: NoInfer<(instance: Type, ...args: Args) => Type>

    publictotalAllocations

    totalAllocations: number = 0

    Methods

    borrow

    • borrow(context: (object: Type) => void): void
    • Use a single instance out of th pool and immediately return it to the pool


      Parameters

      • context: (object: Type) => void

        Returns void

      dispose

      • dispose(): void
      • Returns void

      done

      • done(...objects: Type[]): Type[]
      • done(): void
      • Signals we are done with the pool objects for now, Reclaims all objects in the pool.

        If a list of pooled objects is passed to done they are un-hooked from the pool and are free to be passed to consumers


        Parameters

        • rest...objects: Type[]

          A list of object to separate from the pool

        Returns Type[]

      get

      • get(): Type
      • Retrieve a value from the pool, will allocate a new instance if necessary or recycle from the pool


        Returns Type

      getWith

      • getWith(...args: Args): Type
      • Retrieve a value from the pool like ArenaPool.get, passing arguments along. A new instance gets them from the builder, a reused instance is passed to the recycler with them. Without a recycler a reused instance comes back as it was.

        The argument types come from the builder's parameters.


        Parameters

        • rest...args: Args

          Arguments for the builder or recycler

        Returns Type

      preallocate

      • preallocate(): void
      • Returns void

      using

      • using(context: (pool: ArenaPool<Type, Args>) => void | Type[]): Type[]
      • Use many instances out of the in the context and return all to the pool.

        By returning values out of the context they will be un-hooked from the pool and are free to be passed to consumers


        Parameters

        • context: (pool: ArenaPool<Type, Args>) => void | Type[]

          Returns Type[]