import { Emitter, EventType } from "@Model/Emitter"; import { Component, FunctionComponent, ReactNode, Consumer } from "react"; type RenderComponent = (new (...p: any) => Component) | FunctionComponent; function superConnect, E extends Record>( consumer: Consumer ) { return (...events: Array) => { return (components: R): R => { const C = components as any; return class extends Component { private status: C | undefined; private isEventMount: boolean = false; private handelChange = () => { this.forceUpdate(); } private mountEvent() { if (this.status && !this.isEventMount) { this.isEventMount = true; console.log("Component dep event mount: " + events.join(", ")); for (let i = 0; i < events.length; i++) { this.status.on(events[i], this.handelChange); } } } private unmountEvent() { if (this.status) { for (let i = 0; i < events.length; i++) { this.status.off(events[i], this.handelChange); } } } public render(): ReactNode { const Consumer = consumer; return {(status: C) => { this.status = status; this.mountEvent(); return ; }} } public componentWillUnmount() { this.unmountEvent(); } } as any; } } } export { superConnect };