import { IClass , ILogger , ITypeFilter } from "@robotlegsjs/core" ; import { IMediatorMapping } from "../api/IMediatorMapping" ; import { IMediatorConfigurator } from "../dsl/IMediatorConfigurator" ; import { IMediatorMapper } from "../dsl/IMediatorMapper" ; import { IMediatorUnmapper } from "../dsl/IMediatorUnmapper" ; import { MediatorViewHandler } from "./MediatorViewHandler" ; import { MediatorMapping } from "./MediatorMapping" ; private export class MediatorMapper implements IMediatorMapper , IMediatorUnmapper { private _mappings : Map < IClass < any > , IMediatorMapping > = new Map < IClass < any > , IMediatorMapping > ( ) ; private _typeFilter : ITypeFilter ; private _handler : MediatorViewHandler ; private _logger : ILogger ; private constructor ( typeFilter , handler , logger ? ) { this . _typeFilter = typeFilter ; this . _handler = handler ; this . _logger = logger ; } inheritDoc public toMediator ( mediatorClass ) { const mapping = this . _mappings . get ( mediatorClass ) ; return mapping ? this . overwriteMapping ( mapping ) : this . createMapping ( mediatorClass ) ; } inheritDoc public fromMediator ( mediatorClass ) { const mapping = this . _mappings . get ( mediatorClass ) ; if ( mapping ) { this . deleteMapping ( mapping ) ; } } inheritDoc public fromAll ( ) { this . _mappings . forEach ( this . deleteMapping , this ) ; } private createMapping ( mediatorClass ) { let mapping = new MediatorMapping ( this . _typeFilter , mediatorClass ) ; this . _handler . addMapping ( mapping ) ; this . _mappings . set ( mediatorClass , mapping ) ; if ( this . _logger ) { this . _logger . debug ( "{0} mapped to {1}" , [ this . _typeFilter , mapping ] ) ; } return mapping ; } private deleteMapping ( mapping ) { this . _handler . removeMapping ( mapping ) ; this . _mappings . delete ( mapping . mediatorClass ) ; if ( this . _logger ) { this . _logger . debug ( "{0} unmapped from {1}" , [ this . _typeFilter , mapping ] ) ; } } private overwriteMapping ( mapping ) { if ( this . _logger ) { this . _logger . warn ( "{0} already mapped to {1}\n" + 'If you have overridden this mapping intentionally you can use "unmap()" ' + "prior to your replacement mapping in order to avoid seeing this message.\n" , [ this . _typeFilter , mapping ] ) ; } this . deleteMapping ( mapping ) ; return this . createMapping ( mapping . mediatorClass ) ; } }