import Component , { IComponentConstructor } from "./Component" ; import ClearScreen from "./render/ClearScreen" ; /**
 * A Registry of Component classes.
 *
 * ComponentRegistry maintains a map from Component name
 * to Component class constructors. Typically used through the [[Main.componentRegistry]]
 */ export default class ComponentRegistry { private components : { [ name ] : IComponentConstructor ; } = { } ; private tags : string [ ] = [ ] ; param componentClasses constructor ( componentClasses ? ) { if ( componentClasses ) { this . addComponent ( componentClasses ) ; } } param componentClasses public addComponent ( componentClasses ) { if ( ! Array . isArray ( componentClasses ) ) { componentClasses = [ componentClasses ] ; } componentClasses . forEach ( ( componentClass ) => { const name = componentClass . getComponentName ( ) ; if ( name in this . components ) { throw new Error ( `Component ${ name } already exists in the registry` ) ; } this . components [ name ] = componentClass ; if ( this . tags . indexOf ( componentClass . getComponentTag ( ) ) === - 1 ) { this . tags . push ( componentClass . getComponentTag ( ) ) ; } } ) ; } param name public getComponentClass ( name ) { return this . components [ name ] ; } param tag public getComponentClassesByTag ( tag ) { return Object . keys ( this . components ) . map ( ( name ) => this . components [ name ] ) . filter ( ( componentClass ) => componentClass . getComponentTag ( ) === tag ) ; } }