import Generator from './Generator' ; import ElementsLibraryTargetMapping from './ElementsLibraryTargetMapping' ; import ElementsLibrary from './ElementsLibrary' ; import { ElementMapping } from '../types' ; export default class Target { public name : string ; /**
     * all mappings defined for this target
     */ public mappings : { [ key ] : ElementsLibraryTargetMapping ; } = { } ; /**
     * all mappings indexed by their elements library defined for this target
     */ public mappingsByLibrary : { [ key ] : ElementsLibraryTargetMapping ; } = { } ; private targetGenerator : Generator ; constructor ( name ) { this . name = name ; } param mapping public useMapping ( mapping ) { this . mappings [ mapping . name ] = mapping ; this . mappingsByLibrary [ ( mapping . library as ElementsLibrary ) . name || ( mapping . library as string ) ] = mapping ; mapping . setTarget ( this ) ; } public setGenerator ( generator ) { if ( this . targetGenerator ) throw new Error ( `A Generator for target ${ this . name } is already registered` ) ; this . targetGenerator = generator ; } param mappingName public mapping ( mappingName ) { return this . mappings [ mappingName ] ; } param libraryName public mapLibrary ( libraryName ) { return this . mappingsByLibrary [ libraryName ] ; } param source param type public map ( source , type ) { const mapping = this . mapLibrary ( source ) ; if ( ! mapping ) return null ; return mapping . map ( type ) ; } get generator ( ) : Generator { if ( ! this . targetGenerator ) throw new Error ( `No generator registered for target ${ this . name }` ) ; return this . targetGenerator ; } }