Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hoc 组件缓存问题 #2562

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading