Class
App
View sourceApplication 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
Methods
getWorld(label?: ConstructorFn): World
View sourceReturns 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.
hasPlugin(pluginId: TypeId): boolean
View sourceChecks 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.
setWorld(label: ConstructorFn): App
View sourcedefaultWorld(label: ConstructorFn): App
View sourceSelects 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.
createSchedule(config: ScheduleConfig): App
View sourceStages 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.
registerCrossWorldSchedule(config: CrossWorldScheduleConfig): App
View sourceStages 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.
setRunner(runner: Runner): App
View sourceSets 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.
run(): App
View sourceCompletes app startup and invokes its callback.
Startup proceeds in this order:
- Registered plugins run their
Plugin#registerhooks. - Plugin dependencies are checked, then plugins run their
Plugin#finishhooks. - Staged component hooks and resource aliases are applied to every world; staged resources are applied to their default or labeled worlds.
- Staged schedules and scheduled work are prepared for execution.
- 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 sourceRegisters 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.
registerDebugger(debug: Plugin): App
View sourceRegisters a debugger plugin through App#registerPlugin.
It has the same initialization guard and duplicate-ID behavior as a regular plugin registration.
registerSystem(config: SystemConfig): App
View sourceStages world-bound scheduled work.
The configuration is prepared during App#run; registering the
work does not execute it immediately.
registerCrossworldSystem(config: CrossWorldSystemConfig): App
View sourceStages scheduled work that can use more than one world.
The configuration is prepared during App#run and is not executed
by this registration call.
registerSystemGroup(config: SystemGroupConfig): App
View sourceStages a group configuration for scheduled work.
The group is prepared during App#run. Use
App#registerSystem or App#registerCrossworldSystem to
stage work for the group.
setComponentHooks<T>(component: object, hooks: ComponentHooks): App
View sourcesetResource<T extends object>(resource: T, world?: ConstructorFn): App
View sourcesetResourceByTypeId<T>(id: TypeId, resource: T, world?: ConstructorFn): App
View sourcesetResourceAlias<T>(id: TypeId, alias: Constructor<T>): App
View sourceStages 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.
