Skip to main content

Class: ResourceHeap<T>

Defined in: src/sdk/utils/resource/ResourceHeap.ts:4

A heap which allocates instances of a resource.

Type Parameters

Type Parameter
T

Constructors

Constructor

new ResourceHeap<T>(factory, destructor, onAllocated?, onFreed?, initialSize?, maxSize?, autoShrinkThreshold?): ResourceHeap<T>

Defined in: src/sdk/utils/resource/ResourceHeap.ts:21

Constructor.

Parameters

ParameterTypeDefault valueDescription
factory() => TundefinedA function which creates new instances of this heap's resource.
destructor(resource) => voidundefinedA function which destroys instances of this heap's resource.
onAllocated?(resource) => voidundefinedA function which is called when an instance of this heap's resource is allocated.
onFreed?(resource) => voidundefinedA function which is called when an instance of this heap's resource is freed.
initialSize?number0The initial size of this heap. Defaults to 0.
maxSize?numberNumber.MAX_SAFE_INTEGERThe maximum size of this heap. Defaults to Number.MAX_SAFE_INTEGER. This heap cannot allocate more resources than its maximum size.
autoShrinkThreshold?numberNumber.MAX_SAFE_INTEGERThe size above which this heap will attempt to automatically reduce its size when resources are freed. The heap will never reduce its size below this threshold. Defaults to Number.MAX_SAFE_INTEGER.

Returns

ResourceHeap<T>

Properties

maxSize

readonly maxSize: number = Number.MAX_SAFE_INTEGER

Defined in: src/sdk/utils/resource/ResourceHeap.ts:27

The maximum size of this heap. Defaults to Number.MAX_SAFE_INTEGER. This heap cannot allocate more resources than its maximum size.

Methods

allocate()

allocate(): T

Defined in: src/sdk/utils/resource/ResourceHeap.ts:41

Allocates a resource instance from this heap. If this heap has an existing free resource available, one will be returned. Otherwise, a new resource instance will be created, added to the heap, and returned.

Returns

T

A resource.

Throws

Error if this heap has reached its allocation limit.


free()

free(resource): void

Defined in: src/sdk/utils/resource/ResourceHeap.ts:67

Frees a resource instance allocated from this heap, allowing it to be re-used.

Parameters

ParameterTypeDescription
resourceTThe resource to free.

Returns

void