mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Wrap render method created using class properties (2) (#856)
* wrap render method created using class properties * use Boolean instead of double not-operator * patch-react: move a comment
This commit is contained in:
parent
4b257483e2
commit
8b94534260
|
@ -17,8 +17,23 @@ export default (handleError = () => {}) => {
|
||||||
React.createElement = function (Component, ...rest) {
|
React.createElement = function (Component, ...rest) {
|
||||||
if (typeof Component === 'function') {
|
if (typeof Component === 'function') {
|
||||||
const { prototype } = Component
|
const { prototype } = Component
|
||||||
if (prototype && prototype.render) {
|
|
||||||
prototype.render = wrapRender(prototype.render)
|
// assumes it's a class component if render method exists.
|
||||||
|
const isClassComponent = Boolean(prototype && prototype.render) ||
|
||||||
|
// subclass of React.Component or PureComponent with no render method.
|
||||||
|
// There's no render method in prototype
|
||||||
|
// when it's created with class-properties.
|
||||||
|
prototype instanceof React.Component ||
|
||||||
|
prototype instanceof React.PureComponent
|
||||||
|
|
||||||
|
if (isClassComponent) {
|
||||||
|
if (prototype.render) {
|
||||||
|
prototype.render = wrapRender(prototype.render)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wrap the render method in runtime when the component initialized
|
||||||
|
// for class-properties.
|
||||||
|
Component = wrap(Component, withWrapOwnRender)
|
||||||
} else {
|
} else {
|
||||||
// stateless component
|
// stateless component
|
||||||
Component = wrapRender(Component)
|
Component = wrapRender(Component)
|
||||||
|
@ -39,24 +54,42 @@ export default (handleError = () => {}) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapRender (render) {
|
function wrapRender (render) {
|
||||||
if (render.__wrapped) {
|
return wrap(render, withHandleError)
|
||||||
return render.__wrapped
|
}
|
||||||
|
|
||||||
|
function withHandleError (fn, ...args) {
|
||||||
|
try {
|
||||||
|
return fn.apply(this, args)
|
||||||
|
} catch (err) {
|
||||||
|
handleError(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const _render = function (...args) {
|
function withWrapOwnRender (fn, ...args) {
|
||||||
try {
|
const result = fn.apply(this, args)
|
||||||
return render.apply(this, args)
|
if (this.render && this.hasOwnProperty('render')) {
|
||||||
} catch (err) {
|
this.render = wrapRender(this.render)
|
||||||
handleError(err)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
// copy all properties
|
|
||||||
Object.assign(_render, render)
|
|
||||||
|
|
||||||
render.__wrapped = _render.__wrapped = _render
|
|
||||||
|
|
||||||
return _render
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function wrap (fn, around) {
|
||||||
|
if (fn.__wrapped) {
|
||||||
|
return fn.__wrapped
|
||||||
|
}
|
||||||
|
|
||||||
|
const _fn = function (...args) {
|
||||||
|
return around.call(this, fn, ...args)
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy all properties
|
||||||
|
Object.assign(_fn, fn)
|
||||||
|
|
||||||
|
_fn.prototype = fn.prototype
|
||||||
|
|
||||||
|
_fn.__wrapped = fn.__wrapped = _fn
|
||||||
|
|
||||||
|
return _fn
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue