-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,72 @@ | ||
import { cloneEnumerableProperty } from '@alilc/lowcode-utils'; | ||
import adapter from '../adapter'; | ||
import { IBaseRendererInstance, IRendererProps } from '../types'; | ||
|
||
export function compWrapper(Comp: any) { | ||
function patchDidCatch(Comp: any, { baseRenderer }: { baseRenderer: IBaseRendererInstance }) { | ||
if (Comp.patchedCatch) { | ||
return; | ||
} | ||
Comp.patchedCatch = true; | ||
const { PureComponent } = adapter.getRuntime(); | ||
// Rax 的 getDerivedStateFromError 有 BUG,这里先用 componentDidCatch 来替代 | ||
// @see https://github.com/alibaba/rax/issues/2211 | ||
const originalDidCatch = Comp.prototype.componentDidCatch; | ||
Comp.prototype.componentDidCatch = function didCatch(this: any, error: Error, errorInfo: any) { | ||
this.setState({ engineRenderError: true, error }); | ||
if (originalDidCatch && typeof originalDidCatch === 'function') { | ||
originalDidCatch.call(this, error, errorInfo); | ||
} | ||
}; | ||
|
||
const { engine } = baseRenderer.context; | ||
const originRender = Comp.prototype.render; | ||
Comp.prototype.render = function () { | ||
if (this.state && this.state.engineRenderError) { | ||
this.state.engineRenderError = false; | ||
return engine.createElement(engine.getFaultComponent(), { | ||
...this.props, | ||
error: this.state.error, | ||
componentName: this.props._componentName, | ||
}); | ||
} | ||
return originRender.call(this); | ||
}; | ||
if (!(Comp.prototype instanceof PureComponent)) { | ||
const originShouldComponentUpdate = Comp.prototype.shouldComponentUpdate; | ||
Comp.prototype.shouldComponentUpdate = function (nextProps: IRendererProps, nextState: any) { | ||
if (nextState && nextState.engineRenderError) { | ||
return true; | ||
} | ||
return originShouldComponentUpdate | ||
? originShouldComponentUpdate.call(this, nextProps, nextState) | ||
: true; | ||
}; | ||
} | ||
} | ||
|
||
export function compWrapper(Comp: any, options: { baseRenderer: IBaseRendererInstance }) { | ||
const { createElement, Component, forwardRef } = adapter.getRuntime(); | ||
if ( | ||
Comp?.prototype?.isReactComponent || // react | ||
Comp?.prototype?.setState || // rax | ||
Comp?.prototype instanceof Component | ||
) { | ||
patchDidCatch(Comp, options); | ||
return Comp; | ||
} | ||
class Wrapper extends Component { | ||
// constructor(props: any, context: any) { | ||
// super(props, context); | ||
// } | ||
|
||
render() { | ||
return createElement(Comp, this.props); | ||
return createElement(Comp, { ...this.props, ref: this.props.forwardRef }); | ||
} | ||
} | ||
(Wrapper as any).displayName = Comp.displayName; | ||
|
||
return cloneEnumerableProperty(forwardRef((props: any, ref: any) => { | ||
return createElement(Wrapper, { ...props, forwardRef: ref }); | ||
}), Comp); | ||
patchDidCatch(Wrapper, options); | ||
|
||
return cloneEnumerableProperty( | ||
forwardRef((props: any, ref: any) => { | ||
return createElement(Wrapper, { ...props, forwardRef: ref }); | ||
}), | ||
Comp, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters