import { ReadonlyReactive } from "@conterra/reactivity-core"; import { PackageIntl } from "../i18n"; import { ServiceMetadata } from "../metadata"; import { MarkedServiceFactoryConstructor, Service, ServiceConstructor, ServiceOptions } from "../Service"; import { InterfaceSpec, ReferenceSpec } from "./InterfaceSpec"; export type ServiceState = "not-constructed" | "constructing" | "constructed" | "destroyed"; export type ServiceDependency = ReferenceSpec & { referenceName: string; }; export interface ServiceInstanceFactory { create(options: ServiceOptions): Service; destroy(srv: Service): void; } export interface ServiceReprOptions { name: string; packageName: string; factory: ServiceInstanceFactory; intl: ReadonlyReactive; dependencies?: ServiceDependency[]; interfaces?: InterfaceSpec[]; properties?: Record; } /** * Represents metadata and state of a service in the runtime. * `this.instance` is the actual service instance (when constructed). */ export declare class ServiceRepr { static create(packageName: string, data: ServiceMetadata, intl: ReadonlyReactive, properties?: Record): ServiceRepr; /** Unique id of this service. Contains the package name and the service name. */ readonly id: string; /** Name of this service in it's package. */ readonly name: string; /** Name of the parent package. */ readonly packageName: string; /** Locale-dependant i18n messages. */ readonly intl: ReadonlyReactive; /** Service properties made available via the service's constructor. */ readonly properties: Readonly>; /** Dependencies required by the service constructor. */ readonly dependencies: readonly ServiceDependency[]; /** Interfaces provided by the service. */ readonly interfaces: readonly Readonly[]; /** Number of references to this service. */ private _useCount; /** Service factory to construct an instance. */ private factory; /** Current state of this service. "constructed" -> instance is available. */ private _state; /** Service instance, once constructed. */ private _instance; /** Used in dev mode only. */ private _hmrState; constructor(options: ServiceReprOptions); /** Returns the current service instance or undefined if the service has not been constructed. */ get instance(): Service | undefined; /** Returns the current state of the service. */ get state(): ServiceState; /** Returns the current reference count. */ get useCount(): number; /** * Same as `instance`, but throws when the instance has not been constructed. */ getInstanceOrThrow(): import("..").ServiceLifecycleHooks; /** * Called before `create()` to place the service into the `constructing` state, * which is currently used to detect cycles. */ beforeCreate(): void; /** * Instantiates the service by invoking the service constructor * with the given `options`. * * The service's use count is initialized to `1`, so every `create()` * should be paired with a `removeRef()`. * * `destroy()` can be invoked once the final `removeRef()` has returned zero. */ create(options: Pick): import("..").ServiceLifecycleHooks; destroy(): void; /** * Adds a use to the service's use count. * References to a service are tracked: it should only be destroyed when it is no longer being used. */ addRef(): number; /** * Removes a use from the service's use count. * Returns the new use count. */ removeRef(): number; } export declare function createConstructorFactory(clazz: ServiceConstructor | MarkedServiceFactoryConstructor): ServiceInstanceFactory; export declare function createFactoryForServiceFactory(facClazz: MarkedServiceFactoryConstructor): ServiceInstanceFactory; export declare function createFunctionFactory(create: (options: ServiceOptions) => Service): ServiceInstanceFactory;