HandleSnapshot

View source

Represents serializable data used to restore a typed Handle.

It identifies an asset without retaining the live value. The snapshot is data only: it does not load the asset, and scene-local data can be resolved only with the owning scene context.

Examples

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

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

const handle = server.load(Texture, '/textures/player.png')
const serial = HandleSnapshot.serialize(Handle.toSnapshot(handle, world))
if (HandleSnapshot.validateSerial(serial)) {
  const restored = Handle.fromSnapshot(HandleSnapshot.deserialize(serial), world)
  restored?.drop()
}

Constructor


constructor(typeId: TypeId, asset: string | number)

View source

Creates an asset handle snapshot from its type ID and locator.

The values are stored directly. The constructor does not inspect AssetServer, validate the type ID, or verify a scene mapping. Use Handle#toSnapshot to convert a live handle and select a locator from its available path metadata or pool index.

Parameters

NameDescriptionDefault
typeId

Type ID of the asset family.

—
asset

Asset server path or scene-local pool index.

—

Properties


asset: string | number

View source

Asset path or scene-local asset pool index.

A string is an asset path resolved through AssetServer. A number is the source handle’s pool index and requires a scene asset ID during restoration; it is not a globally valid AssetId.

Methods


staticserialize(value: HandleSnapshot): HandleSnapshotSerial

View source

Converts a snapshot to its plain serializable representation.

This returns a new object rather than a JSON string. It copies type and asset as-is; it does not validate them or resolve a live handle. The asset field therefore remains either the path or the scene-local numeric index.

Parameters

NameDescriptionDefault
value

Snapshot to flatten.

—

staticdeserialize(value: HandleSnapshotSerial, out?: HandleSnapshot): HandleSnapshot

View source

Rehydrates a snapshot from its plain serializable representation.

The optional out value is updated and returned, allowing callers to reuse an existing snapshot object. When omitted, a placeholder snapshot is created first and immediately populated. This method copies the fields directly; it does not restore a live handle, validate the type, path, pool index, or scene mapping, or clone the locator. Call HandleSnapshot.validateSerial before deserializing untrusted data when structural validation is needed.

Parameters

NameDescriptionDefault
value

Plain snapshot data.

—
out?

Snapshot object to populate and return.

...

Returns

The populated snapshot.

staticvalidateSerial(value: unknown): value is HandleSnapshotSerial

View source

Checks whether a value has the serializable shape expected by HandleSnapshot.deserialize.

This is a structural check only. It accepts a non-null object with accessible type and asset properties when type is a string and asset is a string or number. It does not check registration, path existence, pool bounds, scene mappings, or whether a numeric value is finite. Invalid shapes return false; this method does not throw for non-object values.

Parameters

NameDescriptionDefault
value

Value to inspect.

—

Returns

Whether the outer serial shape is accepted.