Options
All
  • Public
  • Public/Protected
  • All
Menu

Implementation of an object pool structure

Type parameters

  • T = unknown

    pool resource

Hierarchy

  • default

Index

Constructors

Properties

availableResources: Set<Resource<Resource<T>>> = ...

Set of all available resources

borrowEventsInQueue: Map<string, string> = ...

Map of active borrow events

borrowedResourceStore: Map<string, Resource<T>> = ...

Store of borrowed pool resources

emitter: EventEmitter2 = ...

Event emitter to broadcast pool events

see

[[EventEmitter]]

events: Map<string, default<string>> = ...

Queue of active events

hashFn: HashFn

A function to calculate a hash string for the specified arguments

maxSize: number = Infinity

The maximum number of resources that the pool can contain

onBorrow?: ResourceHook<T>

Handler: taking some resource via borrow methods

onClear?: PoolHook<T>

Handler: clearing of all pool resources

onFree?: ResourceHook<T>

Handler: releasing of some resource

onTake?: ResourceHook<T>

Handler: taking some resource via take methods

resourceDestructor?: ResourceDestructor<T>

A function to destroy one resource from the pool

resourceFactory: ResourceFactory<T>

A factory to create a new resource for the pool. The function take arguments that are passed to takeOrCreate, borrowAndCreate, etc.

resourceStore: Map<string, Resource<T>[]> = ...

Store of pool resources

unavailableResources: Set<Resource<Resource<T>>> = ...

Set of all unavailable resources

Accessors

  • get available(): number
  • get size(): number

Methods

  • Borrows an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.

    When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers. Mind, you can’t take this resource from the pool when it’s borrowed.

    The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the structure value field will be nullish.

    Parameters

    • Rest ...args: unknown[]

    Returns OptionalWrappedResource<T>

  • Borrows an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.

    When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers. Mind, you can’t take this resource from the pool when it’s borrowed.

    The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, it creates a new resource and returns it.

    Parameters

    • Rest ...args: unknown[]

    Returns WrappedResource<T>

  • Returns a promise with a borrowed resource from the pull. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.

    When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers. Mind, you can’t take this resource from the pool when it’s borrowed.

    The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the promise will wait till it release.

    Parameters

    • Rest ...args: unknown[]

    Returns default<WrappedResource<T>>

  • canBorrow(...args: unknown[]): boolean
  • Checks if you can borrow a resource. The passed arguments will be used to calculate a resource hash.

    Parameters

    • Rest ...args: unknown[]

    Returns boolean

  • canTake(...args: unknown[]): number
  • Returns how many elements of the specified kind you can take. The method takes arguments that will be used to calculate a resource hash.

    Parameters

    • Rest ...args: unknown[]

    Returns number

  • clear(...args: unknown[]): void
  • Clears the pool, i.e. drops all created resource. The method takes arguments that will be provided to hook handlers.

    Parameters

    • Rest ...args: unknown[]

    Returns void

  • createResource(...args: unknown[]): Resource<T>
  • Creates a resource and stores it in the pool. The method takes arguments that will be provided to a resource factory.

    Parameters

    • Rest ...args: unknown[]

    Returns Resource<T>

  • free(resource: Resource<T>, ...args: unknown[]): void
  • Releases the specified resource. The method takes arguments that will be provided to hook handlers.

    Parameters

    • resource: Resource<T>
    • Rest ...args: unknown[]

    Returns void

  • Returns an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.

    The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the structure value field will be nullish.

    Parameters

    • Rest ...args: unknown[]

    Returns OptionalWrappedResource<T>

  • Returns an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.

    The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, it creates a new resource and returns it.

    Parameters

    • Rest ...args: unknown[]

    Returns WrappedResource<T>

  • Returns a promise with an available resource from the pull. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.

    The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the promise will wait till it release.

    Parameters

    • Rest ...args: unknown[]

    Returns default<WrappedResource<T>>