arcade2d
Class

AbstractComponentHost

abstract-component-host.ts:55

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
#
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): void

Removes a component from the host object. Care should be taken when manually removing components, as methods like getComponent() will throw if components do not exist. Removal is idempotent and will do nothing if the component does not exist i.e. was already removed, or never existed.

Parameters

  • key string

Returns

void
ESC