import { AdvertiseEvent , Container , ControllerOptions , CoreTypes , Log , LogHost , LogLevel , toLocalIsoString , } from ".." ; import { IDisposable } from "../runtime/disposable" ; /**
 * Defines lifecycle methods for controllers.
 */ export interface IController extends IDisposable { /**
     * Called when the container has completely set up and injected all
     * dependency components, including all its controllers.
     */ onInit ( ) ; /**
     * Called when the Communication Manager is about to start.
     */ onCommunicationManagerStarting ( ) ; /**
     * Called when the Communication Manager is about to stop.
     */ onCommunicationManagerStopping ( ) ; } /**
 * Defines static constructor signature for controllers that implement the
 * IController interface. To be used for constructor dependency injection
 * in a Coaty container.
 */ export type IControllerStatic < T extends IController > = new ( container , options , controllerName ) => T ; /**
 * The base controller class.
 */ export abstract class Controller implements IController { internal constructor ( private _container , private _options , private _controllerName ) { } /**
     * Gets the container object of this controller.
     */ get container ( ) { return this . _container ; } /**
     * Gets the container's Runtime object.
     */ get runtime ( ) { return this . _container . runtime ; } /**
     * Gets the controller's options as specified in the configuration options.
     */ get options ( ) : Readonly < ControllerOptions > { return this . _options ; } /**
     * Gets the container's communication manager.
     */ get communicationManager ( ) { return this . container . communicationManager ; } /**
     * Gets the registered name of this controller.
     *
     * The registered name is either defined by the corresponding key in the
     * `Components.controllers` object in the container configuration, or by
     * invoking `Container.registerController` method with this name.
     */ get registeredName ( ) { return this . _controllerName ; } param message param tags logDebug ( message , ... tags ) { this . _log ( LogLevel . Debug , message , tags ) ; } param message param tags logInfo ( message , ... tags ) { this . _log ( LogLevel . Info , message , tags ) ; } param message param tags logWarning ( message , ... tags ) { this . _log ( LogLevel . Warning , message , tags ) ; } param error param message param tags logError ( error , message , ... tags ) { const msg = `${ message }: ${ error }` ; this . _log ( LogLevel . Error , msg , tags ) ; } param error param message param tags logErrorWithStacktrace ( error , message , ... tags ) { const msg = `${ message }: ${ ( error && typeof error === "object" && error . stack ) ? error . stack : error }` ; this . _log ( LogLevel . Error , msg , tags ) ; } param error param message param tags logFatal ( error , message , ... tags ) { let msg = `${ message }: ${ error }` ; if ( error && typeof error === "object" && error . stack ) { msg += `\n{{error.stack}}` ; } this . _log ( LogLevel . Fatal , msg , tags ) ; } /**
     * Called when the container has completely set up and injected all
     * dependency components, including all its controllers.
     *
     * Override this method to perform initializations in your custom controller
     * class instead of defining a constructor. Although the base implementation
     * does nothing it is good practice to call `super.onInit()` in your
     * override method; especially if your custom controller class does not
     * extend from the base `Controller` class directly.
     */ onInit ( ) { } /**
     * Called when the communication manager is about to start or restart.
     *
     * Override this method to implement side effects here. Ensure that
     * `super.onCommunicationManagerStarting` is called in your override. The
     * base implementation does nothing.
     */ onCommunicationManagerStarting ( ) { } /**
     * Called when the communication manager is about to stop.
     *
     * Override this method to implement side effects here. Ensure that
     * `super.onCommunicationManagerStopping` is called in your override. The
     * base implementation does nothing.
     */ onCommunicationManagerStopping ( ) { } /**
     * Called by the container when this instance should be disposed.
     *
     * Implement cleanup side effects here. Ensure that `super.onDispose` is
     * called in your override. The base implementation does nothing.
     */ onDispose ( ) { } param log protected extendLogObject ( log ) { } private _log ( logLevel , message , tags ) { const agentInfo = this . runtime . commonOptions ?. agentInfo ; let hostInfo ; let pid ; let userAgent ; if ( this . runtime . isCommonJsPlatform ) { pid = process ? process . pid : undefined ; } if ( this . runtime . isWebPlatform ) { userAgent = navigator ? navigator . userAgent : undefined ; } hostInfo = { agentInfo , pid , userAgent } ; const log = { objectId : this . runtime . newUuid ( ) , objectType : CoreTypes . OBJECT_TYPE_LOG , coreType : "Log" , name : `${ this . _controllerName }` , logLevel : logLevel , logMessage : message , logDate : toLocalIsoString ( new Date ( ) , true ) , logTags : tags , logHost : hostInfo , } ; this . extendLogObject ( log ) ; this . communicationManager . publishAdvertise ( AdvertiseEvent . withObject ( log ) ) ; } }