import { DisplayObject } from "pixi.js" ; import { IClass } from "@robotlegsjs/core" ; import { IMediatorMapping } from "../api/IMediatorMapping" ; import { IViewHandler } from "../../viewManager/api/IViewHandler" ; import { MediatorFactory } from "./MediatorFactory" ; private export class MediatorViewHandler implements IViewHandler { private _mappings : IMediatorMapping [ ] = [ ] ; private _knownMappings : Map < IClass < any > , IMediatorMapping [ ] | boolean > = new Map < IClass < any > , IMediatorMapping [ ] > ( ) ; private _factory : MediatorFactory ; private constructor ( factory ) { this . _factory = factory ; } private public addMapping ( mapping ) { let index = this . _mappings . indexOf ( mapping ) ; if ( index > - 1 ) { return ; } this . _mappings . push ( mapping ) ; this . _knownMappings . clear ( ) ; } private public removeMapping ( mapping ) { let index = this . _mappings . indexOf ( mapping ) ; if ( index === - 1 ) { return ; } this . _mappings . splice ( index , 1 ) ; this . _knownMappings . clear ( ) ; } private public handleView ( view , type ) { let interestedMappings = this . getInterestedMappingsFor ( view , type ) ; if ( interestedMappings ) { this . _factory . createMediators ( view , type , interestedMappings ) ; } } private public handleItem ( item , type ) { let interestedMappings = this . getInterestedMappingsFor ( item , type ) ; if ( interestedMappings ) { this . _factory . createMediators ( item , type , interestedMappings ) ; } } private getInterestedMappingsFor ( item , type ) { if ( this . _knownMappings . get ( type ) === false ) { return null ; } if ( this . _knownMappings . get ( type ) === undefined ) { this . _knownMappings . set ( type , false ) ; this . _mappings . forEach ( ( mapping ) => { if ( mapping . matcher . matches ( item ) ) { if ( ! this . _knownMappings . get ( type ) ) { this . _knownMappings . set ( type , [ ] ) ; } ( this . _knownMappings . get ( type ) as IMediatorMapping [ ] ) . push ( mapping ) ; } } ) ; if ( this . _knownMappings . get ( type ) === false ) { return null ; } } return this . _knownMappings . get ( type ) as IMediatorMapping [ ] ; } }