Class Pool<V>

Resource pool It does the housekeeping of managing a limited set of resources which are shared by 'users'. All resources in the Pool are meant to be the same kind of object.

An example is an audio sketch driven by TensorFlow. We might want to allocate a sound oscillator per detected human body. A naive implementation would be to make an oscillator for each detected body. However, because poses appear/disappear unpredictably, it's a lot of extra work to maintain the binding between pose and oscillator.

Instead, we might use the Pool to allocate oscillators to poses. This will allow us to limit resources and clean up automatically if they haven't been used for a while.

Resources can be added manually with addResource(), or automatically by providing a generate() function in the Pool options. They can then be accessed via a user key. This is meant to associated with a single 'user' of a resource. For example, if we are associating oscillators with TensorFlow poses, the 'user key' might be the id of the pose.

Type Parameters

  • V

Constructors

  • Constructor.

    By default, no capacity limit, one user per resource

    Type Parameters

    • V

    Parameters

    Returns Pool<V>

Properties

capacity: number
capacityPerResource: number
freeResource?: ((v: V) => void)
fullPolicy: FullPolicy
log: LogSet
resourcesWithoutUserExpireAfterMs: number
userExpireAfterMs: number

Accessors

  • get usersLength(): number
  • Return the number of users

    Returns number

Methods

  • Adds a shared resource to the pool

    Parameters

    • resource: V

    Returns Resource<V>

    Error if the capacity limit is reached or resource is null

  • Returns a debug string of Pool state

    Returns string

  • Returns resources sorted with least used first

    Returns Resource<V>[]

  • Sorts users by longest elapsed time since update

    Returns PoolUser<V>[]

  • Returns true if v has an associted resource in the pool

    Parameters

    • resource: V

    Returns boolean

  • Returns true if a given userKey is in use.

    Parameters

    • userKey: string

    Returns boolean

  • Performs maintenance, removing disposed/expired resources & users. This is called automatically when using a resource.

    Returns void

  • Unassociate a key with a pool item

    Parameters

    • userKey: string
    • Optionalreason: string

    Returns void

  • Iterate over resources in the pool. To iterate over the data associated with each resource, use values.

    Returns Generator<Resource<V>, void, unknown>

  • Gets a pool item based on a 'user' key.

    The same key should return the same pool item, for as long as it still exists.

    If a 'user' already has a resource, it will 'keep alive' their use. If a 'user' does not already have resource

    • if there is capacity, a resource is allocated to user
    • if pool is full
      • fullPolicy = 'error': an error is thrown
      • fullPolicy = 'evictOldestUser': evicts an older user
      • Throw error

    Parameters

    • userKey: string

    Returns PoolUser<V>

    Error If all resources are used and fullPolicy = 'error'

  • 'Uses' a resource, returning the value

    Parameters

    • userKey: string

    Returns V

  • Iterate over resource values in the pool. to iterate over the resources, use resources.

    Note that values may be returned even though there is no active user.

    Returns Generator<V, void, unknown>