import { Container } from "pixi.js" ; import { IClass , IEvent } from "@robotlegsjs/core" ; import { ContainerBinding } from "./ContainerBinding" ; import { ContainerRegistry } from "./ContainerRegistry" ; import { ContainerRegistryEvent } from "./ContainerRegistryEvent" ; private export class StageObserver { private _registry : ContainerRegistry ; private constructor ( containerRegistry ) { this . _registry = containerRegistry ; this . _registry . addEventListener ( ContainerRegistryEvent . ROOT_CONTAINER_ADD , this . onRootContainerAdd , this ) ; this . _registry . addEventListener ( ContainerRegistryEvent . ROOT_CONTAINER_REMOVE , this . onRootContainerRemove , this ) ; this . _registry . rootBindings . forEach ( ( binding ) => { this . addRootListener ( binding . container ) ; } ) ; } private public destroy ( ) { this . _registry . removeEventListener ( ContainerRegistryEvent . ROOT_CONTAINER_ADD , this . onRootContainerAdd , this ) ; this . _registry . removeEventListener ( ContainerRegistryEvent . ROOT_CONTAINER_REMOVE , this . onRootContainerRemove , this ) ; this . _registry . rootBindings . forEach ( ( binding ) => { this . removeRootListener ( binding . container ) ; } ) ; } private onRootContainerAdd ( event ) { this . addRootListener ( event . container ) ; } private onRootContainerRemove ( event ) { this . removeRootListener ( event . container ) ; } private addRootListener ( container ) { container . addEventListener ( "added" , this . onViewAddedToStage , this ) ; } private removeRootListener ( container ) { container . removeEventListener ( "added" , this . onViewAddedToStage , this ) ; } private onViewAddedToStage ( event ) { let view = event . target ; let type = < IClass < any > > view . constructor ; let binding = this . _registry . findParentBinding ( view ) ; while ( binding ) { binding . handleView ( view , type ) ; binding = binding . parent ; } } }