AssetServer

View source

World resource for typed asset pools and path-based asset I/O.

AssetServerPlugin provides this resource. Register each asset family with a typed Assets pool and register matching Importer and Exporter converters. The server selects a converter by asset type and exact path extension, records load metadata, and queues load and save requests for later processing.

New load metadata starts in LoadState.Loading. Later processing stores a successfully imported value and changes its state to LoadState.Loaded, or changes it to LoadState.Failed when loading or importing fails.

Examples

import { AssetServer } from '@wimaengine/asset'

function queueTextureLoad(world, Texture) {
  const server = world.getResource(AssetServer)
  const handle = server.load(Texture, '/textures/player.png')

  return handle
}

// The queued request is processed after this call returns.

Constructor


constructor()

Methods


registerAsset<T>(assets: Assets<T>): void

View source

Registers a typed Assets pool with this server.

Registers the pool using its type constructor as the runtime type ID. The pool is available for loading and saving that asset type after registration. Registering another pool for the same type replaces the previously selected pool.

Examples

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

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

// `textures` is now the pool used for `Texture` loads and saves.

Type Parameters

T

Parameters

NameDescriptionDefault
assets

Pool for one typed asset family.

—

Returns

Always returns undefined after storing the pool.

registerImporter<T>(type: Constructor<T>, importer: Importer<T>): void

View source

Registers an Importer for its asset type and supported file extensions.

The type argument does not determine selection. The server uses the constructor in Importer#asset and each exact label returned by Importer#getExtensions. Keep type aligned with that constructor for a consistent registration API. Labels must match the extension extracted from a load path exactly; no normalization is applied. Registering an existing type-and-extension pair warns and selects the new importer.

Examples

import { AssetServer, Importer } from '@wimaengine/asset'

class Texture {}
class TextureImporter extends Importer {
  constructor() { super(Texture) }
  getExtensions() { return ['png'] }
}

const server = new AssetServer()
server.registerImporter(Texture, new TextureImporter())
// A load path ending in `.png` can now select this importer for `Texture`.

Type Parameters

T

Parameters

NameDescriptionDefault
type

Asset constructor passed alongside the importer; it should match Importer#asset.

—
importer

Importer to register.

—

Returns

Always returns undefined after registration.

registerExporter<T>(type: Constructor<T>, exporter: Exporter<T>): void

View source

Registers an Exporter for its asset type and supported file extensions.

The type argument does not determine selection. The server uses the constructor in Exporter#asset and each exact label returned by Exporter#getExtensions. Keep type aligned with that constructor for a consistent registration API. Labels must match the extension extracted from a save path exactly; no normalization is applied. Registering an existing type-and-extension pair warns and selects the new exporter.

Examples

import { AssetServer, Exporter } from '@wimaengine/asset'

class Texture {}
class TextureExporter extends Exporter {
  constructor() { super(Texture) }
  getExtensions() { return ['png'] }
}

const server = new AssetServer()
server.registerExporter(Texture, new TextureExporter())
// A save path ending in `.png` can now select this exporter for `Texture`.

Type Parameters

T

Parameters

NameDescriptionDefault
type

Asset constructor passed alongside the exporter; it should match Exporter#asset.

—
exporter

Exporter to register.

—

Returns

Always returns undefined after registration.

load<T>(type: Constructor<T>, path: string): Handle<T>

View source

Reserves a typed asset handle and queues a load request for a path.

The server requires an Assets pool registered for type and throws if that pool is missing. If a type-specific base path is present, it is prefixed to path by direct string concatenation. The resulting complete path is used for metadata lookup and the queued request; path separators are not normalized.

For a new complete path, the pool reserves an asset ID and the returned Handle owns its first reference. A later call for the same complete path retains the recorded ID through Assets#upgrade and returns another handle without queuing another request. If that ID is stale because its pool slot was recycled, the upgrade returns undefined, which this method also returns.

Fetching and importing happen during later asset processing. Register a matching Importer before processing begins.

Path metadata is matched by complete path rather than by type. The normal plugin setup therefore expects a path to identify one asset family.

Examples

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

class Texture {}
const server = new AssetServer()
const textures = new Assets(Texture)
server.registerAsset(textures)
const path = '/textures/player.png'
const handle = server.load(Texture, path)
const assetId = handle.id()

Type Parameters

T

Parameters

NameDescriptionDefault
type

Constructor of the asset family to load.

—
path

Asset path, including its importer extension.

—

Throws

If no Assets pool is registered for type.

Returns

Reserved or upgraded handle, or undefined when existing metadata points at a stale slot.

loadUntyped(typeId: TypeId, path: string, typeName?: string): UntypedHandle

View source

Queues a load request using an already computed asset type ID.

AssetServer#load uses this form after deriving the type ID. The optional typeName affects only the missing-pool error message. The path is combined with the server’s type-specific base path before metadata lookup. If metadata already exists for that complete path, the registered pool upgrades its asset ID; otherwise this method reserves an ID, records metadata in the initial LoadState.Loading state, and queues a load request.

Parameters

NameDescriptionDefault
typeId——
path——
typeName?——

Throws

If no asset pool is registered for typeId.

save<T>(handle: Handle<T>, path?: string): void

View source

Queues an asset for saving to a path.

The request stores the handle’s type ID and packed Handle#id. When path is supplied, that exact non-empty string is used. The server does not prepend a base path or normalize it. When path is omitted, the server uses the path recorded for that asset ID by AssetServer#load. If no target path is available, the server queues a failed save request using '<unknown>' as its path and returns without throwing.

This method does not verify that the handle still resolves in its typed Assets pool, or look up an Exporter, serialize, or send the asset immediately. Saving is queued for later asset processing. During that processing, a stale or missing asset is reported as a saving AssetLoadFail event.

Examples

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

class Texture {}
const server = new AssetServer()
const textures = new Assets(Texture)
server.registerAsset(textures)
const handle = textures.add(new Texture())
const path = '/exports/player.json'
server.save(handle, path)
// A registered exporter processes the queued request later.

Type Parameters

T

Parameters

NameDescriptionDefault
handle

Handle identifying the asset to save.

—
path?

Destination path; defaults to the recorded path.

—

getImporter<T>(typeId: TypeId, path: string): Importer<T>

View source

Returns the registered Importer for a type ID and path-derived extension.

The extension is the text after the final ., or the entire path string when no . is present; a path ending in . produces an empty string. The result is not lowercased, trimmed, or otherwise normalized. The server matches it exactly against the extensions returned by Importer#getExtensions and the supplied typeId.

Examples

import { AssetServer, Importer } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'

class Texture {}
class TextureImporter extends Importer {
  constructor() { super(Texture) }
  getExtensions() { return ['png'] }
}

const server = new AssetServer()
server.registerImporter(Texture, new TextureImporter())
const importer = server.getImporter(typeid(Texture), '/textures/player.png')

Type Parameters

T

Parameters

NameDescriptionDefault
typeId

Runtime ID of the asset family.

—
path

Path passed to the extension extractor.

—

Throws

If the extracted extension is unknown or the asset type is not registered for it.

If the selected importer cannot be returned.

Returns

Importer selected for the type and extracted extension.

getExporter<T>(typeId: TypeId, path: string): Exporter<T>

View source

Returns the registered Exporter for a type ID and path-derived extension.

The extension is the text after the final ., or the entire path string when no . is present; a path ending in . produces an empty string. The result is not lowercased, trimmed, or otherwise normalized. The server matches it exactly against the extensions returned by Exporter#getExtensions and the supplied typeId.

Examples

import { AssetServer, Exporter } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'

class Texture {}
class TextureExporter extends Exporter {
  constructor() { super(Texture) }
  getExtensions() { return ['png'] }
}

const server = new AssetServer()
server.registerExporter(Texture, new TextureExporter())
const exporter = server.getExporter(typeid(Texture), '/textures/player.png')

Type Parameters

T

Parameters

NameDescriptionDefault
typeId

Runtime ID of the asset family.

—
path

Path passed to the extension extractor.

—

Throws

If the extracted extension is unknown or the asset type is not registered for it.

If the selected exporter cannot be returned.

Returns

Exporter selected for the type and extracted extension.

getAssets(typeId: TypeId): Assets<unknown>

View source

Returns the Assets pool registered under an exact runtime type ID.

Use typeid with the same constructor used by AssetServer#registerAsset when starting from an asset constructor. No pool is created or inferred here; an unregistered type ID returns undefined.

Examples

import { AssetServer, Assets } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'

class Texture {}
const server = new AssetServer()
const textures = new Assets(Texture)
server.registerAsset(textures)
const registered = server.getAssets(typeid(Texture))

Parameters

NameDescriptionDefault
typeId

Runtime ID of the asset family.

—

Returns

Registered pool, if available.

getAssetInfo<T>(handle: Handle<T>): AssetInfo

View source

Returns metadata associated with a handle’s packed Handle#id.

The lookup does not query the handle’s Assets pool or validate its type, slot, or generation. A stale handle can therefore still return metadata until AssetServer#dropAssetInfo removes that ID. If the ID has no metadata, the method returns undefined.

Examples

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

class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
const handle = server.load(Texture, '/textures/player.png')
const info = server.getAssetInfo(handle)

Type Parameters

T

Parameters

NameDescriptionDefault
handle

Handle whose packed ID is looked up.

—

Returns

Metadata, if the ID is registered.

getAssetInfoByAssetId(assetId: AssetId): AssetInfo

View source

Returns metadata associated with an exact packed AssetId.

The lookup uses only assetId; it does not accept or validate an asset type and does not query a typed Assets pool. Use AssetServer#getAssetInfo when a Handle is available. If the ID has no metadata, the method returns undefined.

Parameters

NameDescriptionDefault
assetId

Packed asset identity to look up.

—

Returns

Metadata, if the ID is registered.

getAssetInfoByPath(path: string): AssetInfo

View source

Returns the metadata indexed by an exact recorded path.

The key is the complete path stored when AssetServer#load queued the request. This method does not derive a path from a handle, normalize the string, prepend a base path, or search by asset type. Pass the same complete path used by the request; an unindexed path returns undefined.

Parameters

NameDescriptionDefault
path

Complete path recorded for an asset request.

—

Returns

Metadata, if the path is registered.

dropAssetInfo(id: AssetId): void

View source

Removes the metadata associated with an asset ID.

This deletes the ID and path metadata for the asset. The handle, typed Assets pool, queued requests, and pool reference count remain unchanged. A later load can therefore create fresh metadata for the same path. If id has no metadata, the method does nothing.

Examples

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

class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
const handle = server.load(Texture, '/textures/player.png')
server.dropAssetInfo(handle.id())

// The handle and asset pool are unchanged; only server metadata is gone.

Parameters

NameDescriptionDefault
id

Packed asset identity whose metadata is removed.

—

Returns

Always returns undefined; an unknown ID is a no-op.

flushLoadRequests(): AssetLoadRequest[]

View source

Takes the pending load requests as one FIFO batch and starts a fresh queue.

The returned array is the exact queue instance that was pending when this method was called, so its entries retain the order in which AssetServer#load queued them. The server replaces its pending batch before returning; requests queued afterwards are therefore collected separately. This method does not fetch, import, retry, or otherwise process the returned requests.

Examples

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

class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
server.load(Texture, '/textures/player.png')

const firstBatch = server.flushLoadRequests()
server.load(Texture, '/textures/enemy.png')
const secondBatch = server.flushLoadRequests()
// Each batch preserves enqueue order and belongs to its own queue.
console.log(firstBatch[0].path, secondBatch[0].path)

Returns

The exact pending load queue, in FIFO order.

flushSaveRequests(): AssetSaveRequest[]

View source

Takes the pending save requests as one FIFO batch and starts a fresh queue.

The returned array is the exact queue instance that was pending when this method was called, so its entries retain the order in which AssetServer#save queued them. The server replaces its pending batch before returning; requests queued afterwards are therefore collected separately. This method does not serialize, send, retry, or otherwise process the returned requests.

Returns

The exact pending save queue, in FIFO order.

setLoadState(info: AssetInfo, state: number): void

View source

Replaces the load state stored in an asset metadata entry.

This method only assigns state to info.loadstate. Changing the metadata here does not fetch, enqueue, import, serialize, retry, or otherwise process the asset. The value is stored without validation, so use one of the constants from LoadState.

Examples

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

class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
const handle = server.load(Texture, '/textures/player.png')
const info = server.getAssetInfo(handle)
if (info) server.setLoadState(info, LoadState.Failed)
// Only the metadata changes; no fetch or retry is started.

Parameters

NameDescriptionDefault
info

Metadata entry to update.

—
state

Numeric state to store; normally a value from LoadState.

—

Returns

Always returns undefined after updating the metadata.