ManagedObject
ManagedObject is the base class of most other framework classes, including Component.
class ManagedObject
Base class for objects that have their own unique ID, life cycle including active/inactive and destroyed states, and managed references to other instances.
See also: Concepts > Components
Constructor
(): ManagedObject
.addObserver() static
<T extends ManagedObject>(this:
ManagedObjectConstructor
<T>, Observer: new (instance: T) => any):
ManagedObjectConstructor
<T>
Add an observer to all instances of this class and derived classes. The observer class is instantiated for each instance of this (observed) class, and its methods are automatically called when an event or property change occurs on the observed instance.
Methods of the observer class may be decorated using @onPropertyChange
or @onPropertyEvent
, or method names may be in the following format:
onExamplePropertyChange()
– called when the value ofexampleProperty
changes, or if a Change event is emitted on a managed object referenced by this (managed) propertyonExamplePropertyChangeAsync()
– idem, but called asynchronously, and only once if multiple changes occurred before this method was calledonExampleEventName()
– called when an event with nameExampleEventName
is emitted on the object; as an exception,onChange()
is called when any event that derives fromMangedChangeEvent
is emittedonExampleEventNameAsync()
– idem, but called asynchronouslyonEvent()
– called when any event is emitted on the objectonEventAsync()
– idem, but called asynchronously.
Note: Observer classes may be nested inside of the observed class, which provides access to private and protected methods; see @observe
.
.addEventHandler() static
<T extends ManagedObject>(this:
ManagedObjectConstructor
<T>, handler: (this: T, e:
ManagedEvent
) => void):
ManagedObjectConstructor
<T>
Attach an event handler function, to be invoked for all events that are emitted on all instances of this class and derived classes. Given function is invoked in the context (this
variable) of the emitting object, with the emitted event as a single parameter.
.managedId
number
Unique ID of this managed object (read only).
.managedState
The current lifecycle state of this managed object.
Note: This property is read-only. To change the state of a managed object (i.e. to move its lifecycle between active/inactive and destroyed states), use the activateManagedAsync
, deactivateManagedAsync
, and destroyManagedAsync
methods. If any additional logic is required when moving between states, override the onManagedStateActivatingAsync
, onManagedStateActiveAsync
, onManagedStateDeactivatingAsync
, onManagedStateInactiveAsync
and/or onManagedStateDestroyingAsync
methods in any class that derives from ManagedObject
.
Note: This property cannot be observed directly. Observer classes (see addObserver
) should use methods such as onActive
to observe lifecycle state.
.getReferenceCount() protected
(): number
Returns the current number of managed references that point to this object.
Note: Observers (see addObserver
) may use an onReferenceCountChangeAsync
method to observe this value asynchronously.
.getManagedReferrers() protected
(): ManagedObject[]
Returns an array of unique managed objects that contain managed references to this object (see @managed
, @managedChild
, and @component
decorators).
.getManagedParent() protected
<TParent extends ManagedObject = ManagedObject>(ParentClass?:
ManagedObjectConstructor
<TParent>): TParent
Returns the managed object that contains a managed child reference that points to this instance, if any (see @managedChild
and @component
decorators).
If a class argument is specified, parent references are recursed until a parent of given type is found.
The object itself is never returned, even if it contains a managed child reference that points to itself.
Note: The reference to the managed parent (but not its events) can be observed (see addObserver
) using an onManagedParentChange
or onManagedParentChangeAsync
method on the observer.
.emit()
<TEvent extends ManagedEvent = ManagedEvent, TConstructorArgs extends any[] = any[]>(e: string | TEvent | (new (...args: TConstructorArgs) => TEvent), ...constructorArgs: TConstructorArgs): this
Emit an event. If an event constructor is given, a new instance is created using given constructor arguments (rest parameters). If an event name (string) is given, a new plain event is created with given name.
For ways to handle events, see @delegateEvents
(for events that are emitted by referenced objects) or ManagedObject.addEventHandler
and ManagedObject.addObserver
(static methods for class-based event handling).
Note: There is a limit to the number of events that can be emitted recursively; avoid calling this method on the same object from within a synchronous event handler.
.emitChange()
(name?: string): void
Emit a change event (see ManagedChangeEvent
), to signal that the internal state of the emitting object has changed. The name
parameter is optional; if left out, the CHANGE
event (instance) is emitted directly.
.propagateChildEvents() protected deprecated
(...types: ((new (...args: any[]) =>
ManagedEvent
) | ((e:
ManagedEvent
) => any))[]): this
Propagate events from managed child objects that are referenced as properties of this object (see @managedChild
and @component
decorators) by emitting the same events on this object itself.
Deprecated: in favor of @delegateEvents
since version 3.1.
.activateManagedAsync() protected
(): Promise<any>
Activate this object (i.e. change state to ManagedState.ACTIVATING
and then to ManagedState.ACTIVATED
); the onManagedStateActivatingAsync
and onManagedStateActiveAsync
methods are called in this process.
.deactivateManagedAsync() protected
(): Promise<void>
Deactivate this object, if it is currently active (i.e. change state to ManagedState.DEACTIVATING
and then to ManagedState.DEACTIVATED
); the onManagedStateDeactivatingAsync
and onManagedStateInactiveAsync
methods are called in this process.
.destroyManagedAsync() protected
(): Promise<void>
Destroy this managed object (i.e. change state to ManagedState.DESTROYING
and then to ManagedState.DESTROYED
, clear all managed references from and to this object, and destroy all managed children); the onManagedStateDestroyingAsync
method is called in the process.
Note: Managed child objects are automatically destroyed when [1] their parent’s reference (decorated with @managedChild
or @component
) is cleared or otherwise changed, or [2] the child object is removed from a managed list or map that is itself a managed child, or [3] when the parent object itself is destroyed.
.onManagedStateActivatingAsync() protected
(): Promise<void>
Callback invoked when changing state to ‘active’, can be overridden to perform any actions before activating.
.onManagedStateActiveAsync() protected
(): Promise<void>
Callback invoked immediately after state has changed to ‘active’ and before any other state transitions, can be overridden.
.onManagedStateDeactivatingAsync() protected
(): Promise<void>
Callback invoked when changing state to ‘inactive’, can be overridden to perform any actions before deactivating.
.onManagedStateInactiveAsync() protected
(): Promise<void>
Callback invoked immediately after state has changed to ‘inactive’ and before any other state transitions, can be overridden.
.onManagedStateDestroyingAsync() protected
(): Promise<void>
Callback invoked when changing state to ‘destroyed’, can be overridden to perform any actions first.