import { IContext , IExtension , IInjector , ILogger } from "@robotlegsjs/core" ; import { ContainerRegistry } from "./impl/ContainerRegistry" ; import { StageObserver } from "./impl/StageObserver" ; let installCount = 0 ; /**
 * This extension install an automatic Stage Observer
 */ export class StageObserverExtension implements IExtension { private static _stageObserver : StageObserver = null ; private _injector : IInjector ; private _logger : ILogger ; inheritDoc public extend ( context ) { context . whenInitializing ( this . whenInitializing . bind ( this ) ) ; context . whenDestroying ( this . whenDestroying . bind ( this ) ) ; installCount ++ ; this . _injector = context . injector ; this . _logger = context . getLogger ( this ) ; } private whenInitializing ( ) { if ( ! StageObserverExtension . _stageObserver ) { let containerRegistry = this . _injector . get < ContainerRegistry > ( ContainerRegistry ) ; this . _logger . debug ( "Creating genuine StageObserver Singleton" ) ; StageObserverExtension . _stageObserver = new StageObserver ( containerRegistry ) ; } } private whenDestroying ( ) { installCount -- ; if ( installCount === 0 ) { this . _logger . debug ( "Destroying genuine StageObserver Singleton" ) ; StageObserverExtension . _stageObserver . destroy ( ) ; StageObserverExtension . _stageObserver = null ; } } }