App

View source

Application container for configuring worlds and resources before startup.

Stage worlds, resources, component hooks, schedules, scheduled work, and plugins before calling App#run. Use App#setRunner to provide the app callback invoked after startup setup. The callback can drive execution through the prepared runtime and read resources from the app’s worlds.

Examples

import { App, Plugin } from '@wimaengine/app'

class MainWorld {}
class Settings {}

class GameplayPlugin extends Plugin {
  register(app) {
    app
      .setWorld(MainWorld)
      .defaultWorld(MainWorld)
      .setResource(new Settings())
  }
}

new App()
  .registerPlugin(new GameplayPlugin())
  .setRunner((_scheduler, worlds) => {
    const world = worlds.getWorld(MainWorld)
    console.log(world.getResource(Settings))
  })
  .run()

Constructor


constructor()

Methods


getWorld(label?: ConstructorFn): World

View source

Returns the app’s default world or the world identified by a label.

With no label, this uses the label selected by App#defaultWorld. A missing default or requested world causes the lookup to throw.

Parameters

NameDescriptionDefault
label?——

hasPlugin(pluginId: TypeId): boolean

View source

Checks whether a plugin type ID is already registered with the app.

This is useful from a plugin’s Plugin#requires contract; it does not return the plugin instance.

Parameters

NameDescriptionDefault
pluginId——

setWorld(label: ConstructorFn): App

View source

Registers a world identified by a constructor label.

The label is converted with typeid; calling this again for the same label has no effect. The world is created immediately, while staged resources, aliases, and component hooks are applied during App#run.

Parameters

NameDescriptionDefault
label——

defaultWorld(label: ConstructorFn): App

View source

Selects the label used as the app’s default world.

This records the label and does not create or validate the world. Create it first with App#setWorld; a later lookup of the missing world will assert.

Parameters

NameDescriptionDefault
label——

createSchedule(config: ScheduleConfig): App

View source

Stages a schedule configuration for app startup.

The schedule becomes available to the app callback after App#run; this method records the configuration and does not execute scheduled work.

Parameters

NameDescriptionDefault
config——

registerCrossWorldSchedule(config: CrossWorldScheduleConfig): App

View source

Stages a schedule configuration for work shared across worlds.

The schedule becomes available to the app callback after App#run; this method does not execute scheduled work.

Parameters

NameDescriptionDefault
config——

setRunner(runner: Runner): App

View source

Sets the callback that drives app execution.

App#run calls this function with the prepared execution state as its first argument and the app’s worlds as its second, after plugin registration, world configuration, resource application, and scheduled work setup. The runner is required; run throws if it is still unset. This method itself is not blocked after initialization, although it should be called before run.

Parameters

NameDescriptionDefault
runner——

run(): App

View source

Completes app startup and invokes its callback.

Startup proceeds in this order:

  1. Registered plugins run their Plugin#register hooks.
  2. Plugin dependencies are checked, then plugins run their Plugin#finish hooks.
  3. Staged component hooks and resource aliases are applied to every world; staged resources are applied to their default or labeled worlds.
  4. Staged schedules and scheduled work are prepared for execution.
  5. The configured runner callback receives the prepared execution state and the app’s worlds.

The app is marked initialized immediately before the runner is invoked. If no runner was set, run throws before that flag is set. If the runner throws, that exception propagates. Configuration methods guarded by the initialized state reject calls after startup; App#getWorld, App#hasPlugin, and App#setRunner are not among those guarded methods. run itself has no initialized-state guard, so calling it again repeats plugin lifecycle and runner invocation with the app’s current state. The guarded configuration methods are App#setWorld, App#defaultWorld, App#createSchedule, App#registerCrossWorldSchedule, App#registerPlugin, App#registerDebugger, App#registerSystem, App#registerCrossworldSystem, App#registerSystemGroup, App#setComponentHooks, App#setResource, App#setResourceByTypeId, and App#setResourceAlias.

registerPlugin(plugin: Plugin): App

View source

Registers a plugin instance for the app’s startup lifecycle.

Registered plugins are invoked in insertion order during App#run; duplicate plugin type IDs are rejected. A plugin’s Plugin#register and Plugin#finish hooks can use the same app configuration methods as direct callers.

Parameters

NameDescriptionDefault
plugin——

registerDebugger(debug: Plugin): App

View source

Registers a debugger plugin through App#registerPlugin.

It has the same initialization guard and duplicate-ID behavior as a regular plugin registration.

Parameters

NameDescriptionDefault
debug——

registerSystem(config: SystemConfig): App

View source

Stages world-bound scheduled work.

The configuration is prepared during App#run; registering the work does not execute it immediately.

Parameters

NameDescriptionDefault
config——

registerCrossworldSystem(config: CrossWorldSystemConfig): App

View source

Stages scheduled work that can use more than one world.

The configuration is prepared during App#run and is not executed by this registration call.

Parameters

NameDescriptionDefault
config——

setComponentHooks<T>(component: object, hooks: ComponentHooks): App

View source

Type Parameters

T

Stages component lifecycle hooks to be installed for this component in every world when App#run applies world configuration.

Hooks are keyed by the component’s typeid and a later registration for the same component replaces the earlier staged entry.

Parameters

NameDescriptionDefault
component——
hooks——

setResource<T extends object>(resource: T, world?: ConstructorFn): App

View source

Type Parameters

T extends object

Stages a resource instance for a world.

The resource constructor supplies its type ID. With no world label the resource is applied to the default world; otherwise it is applied to the labeled world during App#run. For explicit type IDs or generic resource values, use App#setResourceByTypeId.

Parameters

NameDescriptionDefault
resource——
world?——

setResourceByTypeId<T>(id: TypeId, resource: T, world?: ConstructorFn): App

View source

Type Parameters

T

Stages a resource value under an explicit type ID.

The outer staging key selects the default world when world is omitted or a labeled world otherwise. A later value with the same type ID and world replaces the earlier staged value; values are applied to worlds by App#run.

Parameters

NameDescriptionDefault
id——
resource——
world?——

setResourceAlias<T>(id: TypeId, alias: Constructor<T>): App

View source

Stages a resource alias for every world created in this app.

id is the type ID of the resource the alias should resolve to, while alias is the constructor callers can use for lookup. The alias is installed on each world during App#run; another registration for the same alias constructor replaces the staged entry.

Type Parameters

T

Parameters

NameDescriptionDefault
id——
alias——