import { Component, FunctionComponent, ReactNode, Consumer } from "react"; import { Emitter, EventType } from "@Model/Emitter"; type RenderComponent = (new (...p: any) => Component) | FunctionComponent; function superConnectWithEvent, E extends Record>( consumer: Consumer, keyName: string ) { return (...events: Array) => { return (components: R): R => { const Components = components as any; const Consumer = consumer; return class extends Component { private status: C | undefined; private isEventMount: boolean = false; private propsObject: Record = {}; 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 { return {(status: C) => { this.status = status; this.propsObject[keyName] = status; this.mountEvent(); return ; }} } public componentWillUnmount() { this.unmountEvent(); } } as any; } } } function superConnect>(consumer: Consumer, keyName: string) { return (components: R): R => { return ((props: any) => { const Components = components as any; const Consumer = consumer; return {(status: C) => } }) as any; } } export { superConnectWithEvent, superConnect };