mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-11 18:42:56 +00:00
fix: hoc 组件缓存
This commit is contained in:
parent
a96961f588
commit
7677ad0166
@ -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;
|
||||
}
|
||||
|
||||
@ -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) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user