Skip to content

Commit

Permalink
fix: hoc 组件缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
rainke committed Oct 17, 2023
1 parent a96961f commit dbeb520
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions packages/renderer-core/src/hoc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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 });
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion packages/renderer-core/src/renderer/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit dbeb520

Please sign in to comment.