AssetsSnapshot

View source

Serializable representation of one typed Assets pool.

It preserves the asset type ID and index-aligned slots so a pool can be serialized and restored with the same layout. Restored asset values use the world’s TypeRegistry; scene restoration records handles in the asset scene map for scene-local references.

The asset type must be registered with a runtime constructor before restoration. Populated serialized slots also need a deserialize method in their TypeRegistry entry.

Examples

import { Assets, AssetsSnapshot } from '@wimaengine/asset'
import { World } from '@wimaengine/ecs'
import { TypeRegistry } from '@wimaengine/reflect'

const world = new World()
world.setResource(new TypeRegistry())

const strings = new Assets(String)
strings.add('player')
const snapshot = Assets.toSnapshot(strings, world)
const serial = AssetsSnapshot.serialize(snapshot)

if (AssetsSnapshot.validateSerial(serial)) {
  const restoredSnapshot = AssetsSnapshot.deserialize(serial)
  console.log(restoredSnapshot.assets[0]) // 'player'
}

Constructor


constructor<T>(type: TypeId, assets: T[])

View source

Creates a snapshot for an asset family.

The supplied slot array is stored as provided; this constructor does not clone or serialize it. Use Assets#toSnapshot when capturing a live pool so registered asset serialization methods are applied.

Type Parameters

T

Parameters

NameDescriptionDefault
type——
assets

Dense asset slots, including empty slots.

—

Properties


assets: T[]

View source

Dense, index-aligned slots copied from the source Assets pool.

An undefined slot represents reserved or empty storage and is preserved so restored handles keep their original indexes. Populated slots contain the asset value or its serialized form, depending on whether the snapshot was produced by Assets#toSnapshot or supplied directly.

Methods


statictypeId(assetType: TypeId): TypeId

View source

Builds the reflection type ID for one asset family’s snapshot.

The result is AssetsSnapshot<assetType>. Use this ID when associating snapshot metadata and hooks with TypeRegistry; this helper only derives the ID and does not register anything.

Examples

import { AssetsSnapshot } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'

class Texture {}
const snapshotType = AssetsSnapshot.typeId(typeid(Texture))

console.log(snapshotType) // 'AssetsSnapshot<Texture>'

Parameters

NameDescriptionDefault
assetType

Type ID of the asset constructor.

—

Returns

The type ID used for the asset family’s snapshot entry.

staticpatch(snapshot: AssetsSnapshot<unknown>, world: World, sceneAssetId: AssetId): boolean

View source

Patches an existing typed Assets pool from a scene snapshot.

The method resolves the asset constructor through the world’s TypeRegistry. If the type entry is missing or has no runtime constructor, it returns false without clearing the scene map or changing the asset pool. Otherwise the world must already contain the matching typed Assets resource and an asset scene map resource.

The scene-map entries for sceneAssetId and this asset type are cleared, then each snapshot index is restored by reserving an undefined slot or adding a truthy result from the registered deserialize hook. The existing Assets pool is not cleared; the method adds or reserves through that pool and maps the snapshot indexes to the resulting handles. Missing or falsey deserialization results are skipped with a warning. It returns true once this patch process has run, even when some slots were skipped.

The scene restoration flow invokes this method when a matching resource already exists.

Parameters

NameDescriptionDefault
snapshot——
world——
sceneAssetId——

Returns

Whether the snapshot type was registered and patched.

staticserialize(value: AssetsSnapshot<unknown>): AssetsSnapshotSerial

View source

Converts a snapshot to its plain serializable shape.

The returned object contains the snapshot’s type and assets values as they are currently stored. It creates a new outer object but preserves the exact assets array reference; neither the array nor its elements are cloned. It does not invoke TypeRegistry; call Assets#toSnapshot before this method when live asset values need type-specific serialization.

Examples

import { AssetsSnapshot } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'

class Texture {}
const slots = [undefined, { source: 'player.png' }]
const snapshot = new AssetsSnapshot(typeid(Texture), slots)
const serial = AssetsSnapshot.serialize(snapshot)

console.log(serial.assets === slots) // true

Parameters

NameDescriptionDefault
value——

staticdeserialize(value: AssetsSnapshotSerial, out?: AssetsSnapshot<unknown>): AssetsSnapshot<unknown>

View source

Copies a serial snapshot into a new or supplied AssetsSnapshot.

This method assigns type and assets directly from value and returns the output object. When out is supplied, that same snapshot is populated; otherwise a new snapshot is created. It does not call AssetsSnapshot.validateSerial, clone the array, or deserialize individual asset values. The default snapshot’s initial Object type and empty array are replaced immediately by the supplied fields.

Parameters

NameDescriptionDefault
value——
out?—...

staticvalidateSerial(value: unknown): value is AssetsSnapshotSerial

View source

Performs the shallow shape check accepted as an assets snapshot serial.

The check accepts a truthy value whose typeof is object, with type and assets properties whose values are respectively a string and an array. Property membership includes inherited properties, and arrays are not rejected if they have the required properties. It does not inspect array elements or confirm that the type is registered; restoration still depends on the world’s TypeRegistry.

Examples

import { AssetsSnapshot } from '@wimaengine/asset'

const serial = {
  type: 'Texture',
  assets: [{ source: 'player.png' }]
}

console.log(AssetsSnapshot.validateSerial(serial)) // true
console.log(AssetsSnapshot.validateSerial({
  type: 'Texture',
  assets: 'not an array'
})) // false

Parameters

NameDescriptionDefault
value

Value to check.

—

Returns

Whether the outer shape is valid.