import { IVirtualNode, IVirtualNodeProps } from './IVirtualNode'; import { Component } from './Component'; import Fragment from './Fragment'; export default class ComponentNode implements IVirtualNode { componentConstructor: new (props?: T, children?: any[]) => Component; props: T & IVirtualNodeProps; children: any; key: string | number; component: Component; constructor( componentConstructor: new (props?: T & IVirtualNodeProps, children?: any[]) => Component, props?: T, children?: Array ) { this.componentConstructor = componentConstructor; this.props = props || ({} as any); this.key = this.props.key; this.children = children; // Push this to the current context let context = Component.getContext() if (context) { context.push(this); } } toComponent(): Component { this.component = new this.componentConstructor(this.props, this.children); if (!(this.component instanceof Fragment)) { this.component.init(); } return this.component; } toNode(namespace: string): Node { if (!this.component) { this.toComponent(); } return this.component.toNode(namespace); } dispose() { if (this.component) { this.component.dispose(); } } }