UNPKG

@alilc/lowcode-renderer-core

Version:
90 lines 3.72 kB
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import _extends from "@babel/runtime/helpers/extends"; import { cloneEnumerableProperty } from '@alilc/lowcode-utils'; import adapter from '../adapter'; function patchDidCatch(Comp, _ref) { var baseRenderer = _ref.baseRenderer; if (Comp.patchedCatch) { return; } Comp.patchedCatch = true; var _adapter$getRuntime = adapter.getRuntime(), PureComponent = _adapter$getRuntime.PureComponent; // Rax 的 getDerivedStateFromError 有 BUG,这里先用 componentDidCatch 来替代 // @see https://github.com/alibaba/rax/issues/2211 var originalDidCatch = Comp.prototype.componentDidCatch; Comp.prototype.componentDidCatch = function didCatch(error, errorInfo) { this.setState({ engineRenderError: true, error: error }); if (originalDidCatch && typeof originalDidCatch === 'function') { originalDidCatch.call(this, error, errorInfo); } }; var engine = baseRenderer.context.engine; var originRender = Comp.prototype.render; Comp.prototype.render = function () { if (this.state && this.state.engineRenderError) { this.state.engineRenderError = false; return engine.createElement(engine.getFaultComponent(), _extends({}, this.props, { error: this.state.error, componentName: this.props._componentName })); } return originRender.call(this); }; if (!(Comp.prototype instanceof PureComponent)) { var originShouldComponentUpdate = Comp.prototype.shouldComponentUpdate; Comp.prototype.shouldComponentUpdate = function (nextProps, nextState) { if (nextState && nextState.engineRenderError) { return true; } return originShouldComponentUpdate ? originShouldComponentUpdate.call(this, nextProps, nextState) : true; }; } } var cache = new Map(); export function compWrapper(Comp, options) { var _Comp$prototype, _Comp$prototype2, _cache$get; var _adapter$getRuntime2 = adapter.getRuntime(), createElement = _adapter$getRuntime2.createElement, Component = _adapter$getRuntime2.Component, forwardRef = _adapter$getRuntime2.forwardRef; if (Comp !== null && Comp !== void 0 && (_Comp$prototype = Comp.prototype) !== null && _Comp$prototype !== void 0 && _Comp$prototype.isReactComponent || // react Comp !== null && Comp !== void 0 && (_Comp$prototype2 = Comp.prototype) !== null && _Comp$prototype2 !== void 0 && _Comp$prototype2.setState || // rax (Comp === null || Comp === void 0 ? void 0 : Comp.prototype) instanceof Component) { patchDidCatch(Comp, options); return Comp; } if (cache.has(options.schema.id) && ((_cache$get = cache.get(options.schema.id)) === null || _cache$get === void 0 ? void 0 : _cache$get.Comp) === Comp) { var _cache$get2; return (_cache$get2 = cache.get(options.schema.id)) === null || _cache$get2 === void 0 ? void 0 : _cache$get2.WrapperComponent; } var Wrapper = /*#__PURE__*/function (_Component) { function Wrapper() { return _Component.apply(this, arguments) || this; } _inheritsLoose(Wrapper, _Component); var _proto = Wrapper.prototype; _proto.render = function render() { return createElement(Comp, _extends({}, this.props, { ref: this.props.forwardRef })); }; return Wrapper; }(Component); Wrapper.displayName = Comp.displayName; patchDidCatch(Wrapper, options); var WrapperComponent = cloneEnumerableProperty(forwardRef(function (props, ref) { return createElement(Wrapper, _extends({}, props, { forwardRef: ref })); }), Comp); cache.set(options.schema.id, { WrapperComponent: WrapperComponent, Comp: Comp }); return WrapperComponent; }