arcade2d
Class

AbstractComponentHost

abstract-component-host.ts:83

Implements ComponentHost<THost>

Abstract base class for component hosts able to extend their own base class. Implements the ComponentHost interface with behaviour aligned to the documentation of those methods.

Constructors

#
constructor(): AbstractComponentHost<THost>

Properties

protected readonly #
components: Map<string, Component<THost, unknown>>
#
enabled: boolean

Master gate on every component update phase this host runs. When false, the host's onPreUpdate, onUpdate, and onPostUpdate iterations short-circuit at a single check — useful for freezing a single object during a cutscene, pausing a UI widget while a menu is up, or temporarily disabling a debug overlay without tearing the components down.

The flag is not propagated to onAdded or onDestroy. Those always fire so a host can never end up with half-attached components, and a disabled object is still cleanly torn down when destroyed.

Defaults to true (active). Flip back to true and the host resumes ticking from its preserved state on the next world update().

Methods

abstract protected #
_createDependencyResolver(component: Component<THost>, key: string): unknown

Subclass hook that produces the concrete dependency resolver the host hands to a component's resolveDependencies. The World hosts a resolver scoped to siblings only; a WorldObject hosts one that also exposes cross-tier lookups against the parent world.

Engine-internal — never called by user code.

Parameters

Returns

unknown
protected #
_handleComponentDestroyError(error: unknown, key: string): void

Hook for subclasses to intercept errors thrown by a component's onDestroy during AbstractComponentHost.removeAllComponents. Default behaviour is to log and swallow — a single bad component must not prevent the rest of the host's components from being torn down. Subclasses may override to route errors through their own reporting channel.

Parameters

  • error unknown
  • key string

Returns

void
abstract protected #
_reportPhaseError(error: unknown, key: string, errorPhase: ComponentUpdatePhase): void

Routes an error thrown by a component's update hook to this host's error-reporting channel. A World reports against itself; a WorldObject delegates to its parent world. Mirrors the AbstractComponentHost._handleComponentDestroyError seam for the onDestroy phase.

Parameters

  • error unknown
  • key string
  • errorPhase ComponentUpdatePhase

Returns

void
protected #
_runComponentPhase(method: 'onPreUpdate' | 'onUpdate' | 'onPostUpdate', errorPhase: ComponentUpdatePhase, update: WorldUpdate): void

Drives every enabled component through one update phase, isolating each invocation so a single throwing component can't abort the rest of the host's components (or the wider tick). The host-level AbstractComponentHost.enabled gate short-circuits the whole phase before any component is touched; a per-component enabled === false skips that one; a component that doesn't implement the optional hook is skipped at a single property read. The cached dependencies are threaded in as the trailing argument.

Failures are routed to AbstractComponentHost._reportPhaseError, which each host kind implements to reach its error-reporting channel.

Parameters

  • method 'onPreUpdate' | 'onUpdate' | 'onPostUpdate'
  • errorPhase ComponentUpdatePhase
  • update WorldUpdate

Returns

void
#
addComponent(key: string, component: Component<THost>, options: AddComponentOptions): Component<THost>

Adds a new component to the host object. Throws if a component with the specified key already exists. Calls onAdded() on the component once registered with its host.

Parameters

Returns

Component<THost>
#
addComponentFromFactory(key: string, factory: ComponentFactory<THost>, options: AddComponentOptions): Component<THost>

Adds a new component to the host object using a factory function. Internally produces the new component using the factory function, then calls addComponent() with the result.

The advantage of using this method over addComponent() is that the factory function is provided with the host.

Parameters

Returns

Component<THost>
#
addComponents(components: ComponentMap<THost>, options: AddComponentOptions): ComponentMap<THost>

Adds a new set of components to the host object. Throws if a component with the specified key already exists. Calls onAdded() on each component after they are all registered with the host, rather than one by one. This is important for components that may want to reference each other during the addition phase via host.getComponent() or similar methods.

It is recommended to use this method rather than addComponent() in situations like initialization of a new host object.

Parameters

Returns

ComponentMap<THost>
#
addComponentsFromFactories(map: ComponentFactoryMap<THost>, options?: AddComponentOptions): ComponentMap<THost>

Adds a new set of components to the host object based on an input map of component keys to factory functions. Behavious is equivalent to addComponents() using the key and output of each factory function.

Parameters

Returns

ComponentMap<THost>
#
getComponent(key: string): T

Gets a component from the host object using the key it was registered with. Throws if the component does not exist. Performs an efficient lookup on a local Map instance.

Parameters

  • key string

Returns

T
#
getComponentByType(type: ComponentHostConstructor<T>): T

Gets a component from the host object using its type. Throws if no component of the type exists, or if more than one component of the type exists — in the multi-match case, getComponentByType deliberately does not pick one for you. Use ComponentHost.getComponentsByType when you genuinely expect multiple matches, or look the component up by its string key.

Performs an O(n) lookup once per type initially, then caches the resolved key for O(1) lookups on subsequent calls. The cache is invalidated whenever a component is removed.

Parameters

Returns

T
#
getComponentsByType(type: ComponentHostConstructor<T>): readonly T[]

Gets every component on the host of the given type, in the order they were originally registered. Returns an empty array if no components match.

Unlike ComponentHost.getComponentByType, this method never throws on multiplicity — it is the explicit "I expect more than one" accessor.

Parameters

Returns

readonly T[]
abstract protected #
getHostReference(): THost

Gets a reference to the host object that this component is attached to. Required for the compiler to be able to resolve the type of the host object correctly in some internal function calls (e.g. when resolving the type of the host object from a factory function).

Returns

THost
#
getNullableComponent(key: string): null | T

Gets a component from the host object using the key it was registered with. Returns null if the component does not exist, rather than throwing an error. Useful for referencing transient or optional components without manually handling errors.

Parameters

  • key string

Returns

null | T
#
getNullableComponentByType(type: ComponentHostConstructor<T>): null | T

Gets a component from the host object using its type. Returns null if the component does not exist or if more than one component of the type is registered (i.e. the lookup is ambiguous) — the nullable variant collapses both "not found" and "ambiguous" into a single null. Use ComponentHost.getComponentsByType when you need to distinguish them.

Parameters

Returns

null | T
#
hasComponent(key: string): boolean

Checks if the host object has a component with the specified key.

Parameters

  • key string

Returns

boolean
#
hasComponentByType(type: ComponentHostConstructor<T>): boolean

Checks if the host object has a component with the specified type.

Parameters

Returns

boolean
#
removeAllComponents(): void

Removes all components from the host object. Typically called internally when the lifecycle of the host object is terminated. Differs from individually removing components in that it first calls onDestroy() on each component, then removes references from the host object in a separate step. This allows cleaner teardown of components that may reference each other.

Returns

void
#
removeComponent(key: string): null | Component<THost, unknown>

Removes a component from the host object and returns it, so callers can keep inspecting the detached component (or move it to another host). Care should be taken when manually removing components, as methods like getComponent() will throw if components do not exist. Removal is idempotent: removing a key that does not exist (already removed, or never present) is a no-op that returns null.

The component's onDestroy runs while it is still registered (so it can reach its siblings during teardown); a throw from onDestroy is routed to the host's error channel rather than propagating, and the component is removed from the host either way.

Parameters

  • key string

Returns

null | Component<THost, unknown>

The removed component, or null if no component was registered under key.

#
removeComponents(keys: readonly string[]): void

Removes several components in one batch, mirroring the four addComponents* variants on the add side. Unlike calling ComponentHost.removeComponent in a loop, this runs every targeted component's onDestroy first and only then deletes them — so two interdependent components can still reach each other during teardown, the same guarantee ComponentHost.removeAllComponents provides, scoped to the named subset. Unknown keys are skipped silently.

Parameters

  • keys readonly string[]

Returns

void
ESC