From dbeb520a2fdb2a93fa6c553aa8a935252372e539 Mon Sep 17 00:00:00 2001 From: rainke Date: Tue, 17 Oct 2023 17:33:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20hoc=20=E7=BB=84=E4=BB=B6=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/renderer-core/src/hoc/index.tsx | 22 +++++++++++++++++--- packages/renderer-core/src/renderer/base.tsx | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/renderer-core/src/hoc/index.tsx b/packages/renderer-core/src/hoc/index.tsx index aae03045e..3e7f5ec6a 100644 --- a/packages/renderer-core/src/hoc/index.tsx +++ b/packages/renderer-core/src/hoc/index.tsx @@ -2,7 +2,12 @@ import { cloneEnumerableProperty } from '@alilc/lowcode-utils'; import adapter from '../adapter'; import { IBaseRendererInstance, IRendererProps } from '../types'; -function patchDidCatch(Comp: any, { baseRenderer }: { baseRenderer: IBaseRendererInstance }) { +interface Options { + baseRenderer: IBaseRendererInstance; + schema: any; +} + +function patchDidCatch(Comp: any, { baseRenderer }: Options) { if (Comp.patchedCatch) { return; } @@ -44,7 +49,9 @@ function patchDidCatch(Comp: any, { baseRenderer }: { baseRenderer: IBaseRendere } } -export function compWrapper(Comp: any, options: { baseRenderer: IBaseRendererInstance }) { +const cache = new Map(); + +export function compWrapper(Comp: any, options: Options) { const { createElement, Component, forwardRef } = adapter.getRuntime(); if ( Comp?.prototype?.isReactComponent || // react @@ -54,6 +61,11 @@ export function compWrapper(Comp: any, options: { baseRenderer: IBaseRendererIns patchDidCatch(Comp, options); return Comp; } + + if (cache.has(options.schema.id)) { + return cache.get(options.schema.id); + } + class Wrapper extends Component { render() { return createElement(Comp, { ...this.props, ref: this.props.forwardRef }); @@ -63,10 +75,14 @@ export function compWrapper(Comp: any, options: { baseRenderer: IBaseRendererIns patchDidCatch(Wrapper, options); - return cloneEnumerableProperty( + const WrapperComponent = cloneEnumerableProperty( forwardRef((props: any, ref: any) => { return createElement(Wrapper, { ...props, forwardRef: ref }); }), Comp, ); + + cache.set(options.schema.id, WrapperComponent); + + return WrapperComponent; } diff --git a/packages/renderer-core/src/renderer/base.tsx b/packages/renderer-core/src/renderer/base.tsx index 2ed476b1c..7b9562fcc 100644 --- a/packages/renderer-core/src/renderer/base.tsx +++ b/packages/renderer-core/src/renderer/base.tsx @@ -615,7 +615,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { }); }); - Comp = compWrapper(Comp, { baseRenderer: this }); + Comp = compWrapper(Comp, { baseRenderer: this, schema }); components[schema.componentName] = Comp; otherProps.ref = (ref: any) => {