import { IContext , IExtension , IInjector } from "@robotlegsjs/core" ; import { IMediatorMap } from "./api/IMediatorMap" ; import { MediatorMap } from "./impl/MediatorMap" ; import { IViewManager } from "../viewManager/api/IViewManager" ; /**
 * This extension installs a shared IMediatorMap into the context
 */ export class MediatorMapExtension implements IExtension { private _injector : IInjector ; private _mediatorMap : MediatorMap ; private _viewManager : IViewManager ; inheritDoc public extend ( context ) { context . beforeInitializing ( this . beforeInitializing . bind ( this ) ) . beforeDestroying ( this . beforeDestroying . bind ( this ) ) . whenDestroying ( this . whenDestroying . bind ( this ) ) ; this . _injector = context . injector ; this . _injector . bind ( IMediatorMap ) . to ( MediatorMap ) . inSingletonScope ( ) ; } private beforeInitializing ( ) { this . _mediatorMap = this . _injector . get < MediatorMap > ( IMediatorMap ) ; if ( this . _injector . isBound ( IViewManager ) ) { this . _viewManager = this . _injector . get < IViewManager > ( IViewManager ) ; this . _viewManager . addViewHandler ( this . _mediatorMap ) ; } } private beforeDestroying ( ) { this . _mediatorMap . unmediateAll ( ) ; if ( this . _injector . isBound ( IViewManager ) ) { this . _viewManager = this . _injector . get < IViewManager > ( IViewManager ) ; this . _viewManager . removeViewHandler ( this . _mediatorMap ) ; } } private whenDestroying ( ) { if ( this . _injector . isBound ( IMediatorMap ) ) { this . _injector . unbind ( IMediatorMap ) ; } } }