Handle

View source

Represents a live, typed reference to an asset entry.

A handle identifies one entry in an Assets pool without containing the asset itself. Copies share that identity, ownership changes are deferred, and an older handle becomes stale if the entry is recycled.

Examples

import { AssetServer, Assets } from '@wimaengine/asset'
import { World } from '@wimaengine/ecs'

class Texture {}
const world = new World()
const textures = new Assets(Texture)
const server = new AssetServer()
world.setResource(server)
server.registerAsset(textures)

const handle = server.load(Texture, '/textures/player.png')
const copy = handle.clone()
const snapshot = Handle.toSnapshot(handle, world)
console.log(snapshot.asset) // '/textures/player.png'

copy.drop()
handle.drop()
textures.update()

Constructor


constructor<T>(channel: AssetChannel<T>, type: Constructor<T>, index: number, generation: number)

View source

Creates a handle for a typed asset-pool slot.

This low-level constructor stores the supplied operation state, type, index, and generation directly. It does not validate the slot or change its reference count; Assets pool operations normally create handles for callers.

Type Parameters

T

Parameters

NameDescriptionDefault
channel

Deferred-operation state for the owning asset pool.

—
type

Constructor of the asset family.

—
index

Index of the pool slot.

—
generation

Generation associated with the slot.

—

Properties


readonlytype: Constructor<T>

View source

Constructor identifying the asset family represented by this handle.

This is the constructor associated with the owning Assets pool.

readonlyindex: number

View source

Index of this handle’s entry in the owning Assets pool.

The index can be reused after an entry is released, so it is not a complete asset identity by itself. Pair it with Handle#generation, or use Handle#id, when identifying the entry.

readonlygeneration: number

View source

Generation associated with the pool entry at Handle#index.

The owning Assets pool increments the generation when it reuses an index. A handle or asset ID with an older generation is stale and does not resolve to the replacement entry.

Default: 0

Methods


id(): AssetId

View source

Returns this handle’s packed asset identity.

The slot index and generation are combined into an AssetId. This method does not inspect the owning pool, so a stale handle continues to return the same identity.

Returns

Packed slot-and-generation identity.

clone(): Handle<T>

View source

Queues an additional reference for this identity and returns a handle for it.

The returned handle has the same type, index, generation, and deferred operation state as this handle. The reference count changes only when the owning Assets pool processes pending operations. If the identity is stale at that time, the queued acquisition is ignored. The returned handle is not marked as dropped.

Returns

A separate handle for the same identity.

statictoSnapshot<U>(handle: Handle<U>, world: World): HandleSnapshot

View source

Creates a HandleSnapshot for this handle.

The world must contain an AssetServer resource. If the server has truthy path metadata for this handle’s packed ID, the snapshot stores that path; otherwise it stores the handle’s pool index for scene-local restoration. The snapshot always stores the asset type ID. This method does not validate the handle’s generation or fetch the asset.

Type Parameters

U

Parameters

NameDescriptionDefault
handle

Live handle being snapshotted.

—
world

World containing the AssetServer resource.

—

Throws

If the world does not contain an AssetServer resource.

Returns

Path-backed or scene-local reference data.

staticfromSnapshot(snapshot: HandleSnapshot, world: World, sceneAssetId?: AssetId): UntypedHandle

View source

Restores a live handle from a snapshot value.

Parameters

NameDescriptionDefault
snapshot——
world——
sceneAssetId?——

drop(): void

View source

Queues this handle’s release once.

The first call marks this handle as dropped and queues its packed identity; repeated calls do nothing. The owning Assets pool applies the release later, decrementing the matching generation’s reference count and recycling the slot when no references remain. A stale identity is ignored, and this method does not change the pool immediately.

Returns

Nothing; the release is deferred to the asset pool.