Skip to main content

Class: ResourceHeap<T>

A heap which allocates instances of a resource.

Type parameters

Name
T

Constructors

constructor

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

Constructor.

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
factory() => TundefinedA function which creates new instances of this heap's resource.
destructor(resource: T) => voidundefinedA function which destroys instances of this heap's resource.
onAllocated?(resource: T) => voidundefinedA function which is called when an instance of this heap's resource is allocated.
onFreed?(resource: T) => voidundefinedA function which is called when an instance of this heap's resource is freed.
initialSizenumber0The initial size of this heap. Defaults to 0.
maxSizenumberNumber.MAX_SAFE_INTEGERThe maximum size of this heap. Defaults to Number.MAX_SAFE_INTEGER. This heap cannot allocate more resources than its maximum size.
autoShrinkThresholdnumberNumber.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>

Defined in

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

Properties

maxSize

Readonly maxSize: number = Number.MAX_SAFE_INTEGER

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

Defined in

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

Methods

allocate

allocate(): T

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.

Defined in

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


free

free(resource): void

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

Parameters

NameTypeDescription
resourceTThe resource to free.

Returns

void

Defined in

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