import * as React from "react" ; import ComponentRegistry from "./ComponentRegistry" ; export class Mapping { public resourceType : string ; public vanillaClass : typeof React . Component ; public componentClass : any ; constructor ( resourceType , componentClass , vanillaClass ) { this . resourceType = resourceType ; this . componentClass = componentClass ; this . vanillaClass = vanillaClass ; } } export default class RootComponentRegistry { private registries : ComponentRegistry [ ] ; private resourceTypeToComponent : { [ name ] : typeof React . Component ; } = { } ; private componentToResourceType : { [ componentClassName ] : string ; } = { } ; private vanillaToWrapper : { [ componentClassName ] : typeof React . Component ; } = { } ; constructor ( ) { this . registries = [ ] ; } public add ( registry ) { this . registries . push ( registry ) ; } public getResourceType ( component ) { if ( component instanceof React . Component ) { let componentClassName = Object . getPrototypeOf ( component ) . constructor . name ; return this . componentToResourceType [ componentClassName ] ; } else { let componentClassName = ( component as any ) . name ; return this . componentToResourceType [ componentClassName ] ; } } public getComponent ( resourceType ) { return this . resourceTypeToComponent [ resourceType ] ; } public register ( mapping ) { let componentClassName = ( mapping . componentClass as any ) [ "name" ] ; if ( ! mapping . vanillaClass ) { this . componentToResourceType [ componentClassName ] = mapping . resourceType ; } else { let vanillaClassName = ( mapping . vanillaClass as any ) [ "name" ] ; this . vanillaToWrapper [ vanillaClassName ] = mapping . componentClass ; } this . resourceTypeToComponent [ mapping . resourceType ] = mapping . componentClass ; } public init ( ) { this . registries . forEach ( ( registry ) => { registry . mappings . forEach ( ( mapping ) => { this . register ( mapping ) ; } , this ) ; } , this ) ; } public getVanillaWrapper ( component ) { let name = ( component as any ) [ "name" ] ; return this . vanillaToWrapper [ name ] ; } }