ResourceCommands

View source

Builds resource changes for deferred application to a world.

add(), remove(), and setAlias() append changes to the world’s deferred queue in call order and return this builder for chaining. The world applies those changes later.

Examples

import { ResourceCommands } from '@wimaengine/commands'
import { typeid } from '@wimaengine/type'

class Settings {}
class SettingsAlias {}

// `world` must already support deferred resource changes.
const commands = new ResourceCommands(world)
commands
  .add(new Settings())
  .setAlias(typeid(Settings), SettingsAlias)

Constructor


constructor(world: World)

View source

Creates a resource-command builder for a world.

The builder uses the world’s existing deferred queue; it does not create one. The world must support deferred resource changes before this call.

Parameters

NameDescriptionDefault
world

World whose resource changes will be queued.

—

Methods


add(resource: object): ResourceCommands

View source

Queues a resource to be installed in the world.

When the change is applied, a resource with the same type replaces the existing value.

Parameters

NameDescriptionDefault
resource

Resource instance to install.

—

Returns

This builder.

set(resourceType: ConstructorFn, value: unknown, path?: (string | number)[]): ResourceCommands

View source

Sets a resource or one value inside an existing resource.

An empty path replaces the resource. Non-empty paths address nested live values, for example ['enabled'] or ['display', 'theme'].

Parameters

NameDescriptionDefault
resourceType——
value——
path?—[]

setByTypeId(resourceType: TypeId, value: unknown, path?: (string | number)[]): ResourceCommands

View source

Sets a resource or one value inside an existing resource by type ID.

An empty path replaces the resource. Non-empty paths address nested live values.

Parameters

NameDescriptionDefault
resourceType——
value——
path?—[]

remove(resourceType: ConstructorFn): ResourceCommands

View source

Queues removal of a resource by its constructor.

When the change is applied, the resource is removed. An alias registered under the same constructor is removed as well; aliases registered under other constructors remain.

Parameters

NameDescriptionDefault
resourceType

Constructor of the resource to remove.

—

Returns

This builder.

setAlias(id: TypeId, alias: ConstructorFn): ResourceCommands

View source

Queues an alias for a resource type ID.

Once applied, lookups through alias resolve to the resource identified by id. The alias may be queued before that resource is added, and a later alias for the same constructor replaces its target.

Parameters

NameDescriptionDefault
id

Type ID of the resource the alias names.

—
alias

Constructor used to look up the resource.

—

Returns

This builder.