import { DisplayObject } from "pixi.js" ; import { IMediatorMapping } from "../api/IMediatorMapping" ; import { MediatorFactory } from "./MediatorFactory" ; private export class MediatorManager { private _factory : MediatorFactory ; private constructor ( factory ) { this . _factory = factory ; } private public addMediator ( mediator , item , mapping ) { if ( item instanceof DisplayObject && mapping . autoRemoveEnabled ) { ( < any > item ) . _onRemovedFromStage = this . onRemovedFromStage . bind ( this , item ) ; item . on ( "removed" , ( < any > item ) . _onRemovedFromStage , this ) ; } this . initializeMediator ( mediator , item ) ; } private public removeMediator ( mediator , item , mapping ) { if ( item instanceof DisplayObject ) { item . off ( "removed" , ( < any > item ) . _onRemovedFromStage ) ; } this . destroyMediator ( mediator ) ; } private onRemovedFromStage ( displayObject , fromContainer ) { this . _factory . removeMediators ( displayObject ) ; } private initializeMediator ( mediator , mediatedItem ) { if ( "preInitialize" in mediator ) { mediator . preInitialize ( ) ; } if ( "view" in mediator ) { mediator . view = mediatedItem ; } if ( "initialize" in mediator ) { mediator . initialize ( ) ; } if ( "postInitialize" in mediator ) { mediator . postInitialize ( ) ; } } private destroyMediator ( mediator ) { if ( "preDestroy" in mediator ) { mediator . preDestroy ( ) ; } if ( "destroy" in mediator ) { mediator . destroy ( ) ; } if ( "view" in mediator ) { mediator . view = null ; } if ( "postDestroy" in mediator ) { mediator . postDestroy ( ) ; } } }