Documentation

ManagedService

Services contain data and logic that are not related to a particular activity or view.

class ManagedService abstract

extends ManagedObject

Managed service base class. Represents a service that can be referenced by other classes using the @service decorator after registering the object with a well known name.

Registering services

A single instance of a service needs to be registered for other components to be able to reference the service using its name.

The preferred format for service names is Namespace.CamelCaseName.

Example

class MyService extends ManagedService {
  name = "App.MyService";
  doSomething() {
    console.log("I am " + this.name);
  }
}

// register a singleton to be able to reference it
new MyService().register();

// reference a service from any managed object
class MyComponent extends Component {
  @service("App.MyService")
  svc?: MyService;

  foo() {
    if (this.svc) this.svc.doSomething()
  }
}

let c = new MyComponent();
c.foo();  // => I am App.MyService

Observing services

Note: service reference properties (on other components) are only set when they are first accessed by your code. Before the property is read, the service is not referenced at all. This means that events are also only observable after the service reference has been read at least once.

If your activity relies on a service to emit an event (e.g. a session/login service, to communicate that the user has connected to your application back end), it is necessary to read the service property once, for example in the activity constructor or onManagedStateActiveAsync method.

Constructor

(name?: string): ManagedService

.find() static

(name: string): ManagedService

Retrieve the currently active service with given name (case insensitive).

Note: Services may be registered and destroyed. To obtain a reference that is always up to date, use the @service decorator on a class property.

.name abstract

string

The name of this service, set only once by the service constructor. The preferred format for service names is Namespace.CamelCaseName.

.register()

(): void

Register this service, making it available through properties decorated with the @service decorator until the service object is destroyed (either directly, or when another service is registered with the same name).

.managedId

number

Inherited from ManagedObject.managedId.

.managedState

ManagedState

Inherited from ManagedObject.managedState.

.getReferenceCount() protected

(): number

Inherited from ManagedObject.getReferenceCount.

.getManagedReferrers() protected

(): ManagedObject[]

Inherited from ManagedObject.getManagedReferrers.

.getManagedParent() protected

[1]. (): ManagedObject
[2]. <TParent extends ManagedObject>(ParentClass: ManagedObjectConstructor<TParent>): TParent

Inherited from ManagedObject.getManagedParent.

.emit()

<TEvent extends ManagedEvent = ManagedEvent, TConstructorArgs extends any[] = any[]>(e: string | TEvent | (new (...args: TConstructorArgs) => TEvent), ...constructorArgs: TConstructorArgs): this

Inherited from ManagedObject.emit.

.propagateChildEvents() protected

[1]. (f?: (this: this, e: ManagedEvent, propertyName: string) => void | ManagedEvent | ManagedEvent[]): this
[2]. (...types: (ManagedEvent | (new (...args: any[]) => ManagedEvent))[]): this

Inherited from ManagedObject.propagateChildEvents.

.activateManagedAsync() protected

(): Promise<any>

Inherited from ManagedObject.activateManagedAsync.

.deactivateManagedAsync() protected

(): Promise<void>

Inherited from ManagedObject.deactivateManagedAsync.

.destroyManagedAsync() protected

(): Promise<void>

Inherited from ManagedObject.destroyManagedAsync.

.onManagedStateActivatingAsync() protected

(): Promise<void>

Inherited from ManagedObject.onManagedStateActivatingAsync.

.onManagedStateActiveAsync() protected

(): Promise<void>

Inherited from ManagedObject.onManagedStateActiveAsync.

.onManagedStateDeactivatingAsync() protected

(): Promise<void>

Inherited from ManagedObject.onManagedStateDeactivatingAsync.

.onManagedStateInactiveAsync() protected

(): Promise<void>

Inherited from ManagedObject.onManagedStateInactiveAsync.

.onManagedStateDestroyingAsync() protected

(): Promise<void>

Inherited from ManagedObject.onManagedStateDestroyingAsync.