2017-01-05 17:27:39 +00:00
|
|
|
// monkeypatch React for fixing https://github.com/facebook/react/issues/2461
|
|
|
|
// based on https://gist.github.com/Aldredcz/4d63b0a9049b00f54439f8780be7f0d8
|
|
|
|
|
|
|
|
import React from 'react'
|
|
|
|
|
|
|
|
let patched = false
|
|
|
|
|
|
|
|
export default (handleError = () => {}) => {
|
|
|
|
if (patched) {
|
|
|
|
throw new Error('React is already monkeypatched')
|
|
|
|
}
|
|
|
|
|
|
|
|
patched = true
|
|
|
|
|
|
|
|
const { createElement } = React
|
|
|
|
|
|
|
|
React.createElement = function (Component, ...rest) {
|
|
|
|
if (typeof Component === 'function') {
|
2017-01-28 13:50:46 +00:00
|
|
|
// We need to get the prototype which has the render method.
|
|
|
|
// It's possible to have render inside a deeper prototype due to
|
|
|
|
// class extending.
|
|
|
|
const prototypeWithRender = getRenderPrototype(Component)
|
2017-01-05 17:27:39 +00:00
|
|
|
const { prototype } = Component
|
2017-01-22 18:11:56 +00:00
|
|
|
|
|
|
|
// assumes it's a class component if render method exists.
|
2017-01-28 13:50:46 +00:00
|
|
|
const isClassComponent = Boolean(prototypeWithRender) ||
|
2017-01-22 18:11:56 +00:00
|
|
|
// 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
|
|
|
|
|
2017-01-28 13:50:46 +00:00
|
|
|
let dynamicWrapper = withWrapOwnRender
|
|
|
|
|
2017-01-22 18:11:56 +00:00
|
|
|
if (isClassComponent) {
|
2017-01-28 13:50:46 +00:00
|
|
|
if (prototypeWithRender) {
|
|
|
|
// Sometimes render method is created with only a getter.
|
|
|
|
// In that case we can't override it with a prototype. We need to
|
|
|
|
// do it dynamically.
|
|
|
|
if (canOverrideRender(prototypeWithRender)) {
|
|
|
|
prototypeWithRender.render = wrapRender(prototypeWithRender.render)
|
|
|
|
} else {
|
|
|
|
dynamicWrapper = withWrapRenderAlways
|
|
|
|
}
|
2017-01-22 18:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap the render method in runtime when the component initialized
|
|
|
|
// for class-properties.
|
2017-01-28 13:50:46 +00:00
|
|
|
Component = wrap(Component, dynamicWrapper)
|
2017-01-05 17:27:39 +00:00
|
|
|
} else {
|
|
|
|
// stateless component
|
|
|
|
Component = wrapRender(Component)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return createElement.call(this, Component, ...rest)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { Component: { prototype: componentPrototype } } = React
|
|
|
|
const { forceUpdate } = componentPrototype
|
|
|
|
|
|
|
|
componentPrototype.forceUpdate = function (...args) {
|
|
|
|
if (this.render) {
|
|
|
|
this.render = wrapRender(this.render)
|
|
|
|
}
|
|
|
|
return forceUpdate.apply(this, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapRender (render) {
|
2017-01-22 18:11:56 +00:00
|
|
|
return wrap(render, withHandleError)
|
|
|
|
}
|
2017-01-05 17:27:39 +00:00
|
|
|
|
2017-01-22 18:11:56 +00:00
|
|
|
function withHandleError (fn, ...args) {
|
|
|
|
try {
|
|
|
|
return fn.apply(this, args)
|
|
|
|
} catch (err) {
|
|
|
|
handleError(err)
|
|
|
|
return null
|
2017-01-05 17:27:39 +00:00
|
|
|
}
|
2017-01-22 18:11:56 +00:00
|
|
|
}
|
2017-01-05 17:27:39 +00:00
|
|
|
|
2017-01-22 18:11:56 +00:00
|
|
|
function withWrapOwnRender (fn, ...args) {
|
|
|
|
const result = fn.apply(this, args)
|
|
|
|
if (this.render && this.hasOwnProperty('render')) {
|
|
|
|
this.render = wrapRender(this.render)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2017-01-28 13:50:46 +00:00
|
|
|
|
|
|
|
function withWrapRenderAlways (fn, ...args) {
|
|
|
|
const result = fn.apply(this, args)
|
|
|
|
if (this.render) {
|
|
|
|
this.render = wrapRender(this.render)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2017-01-22 18:11:56 +00:00
|
|
|
}
|
2017-01-05 17:27:39 +00:00
|
|
|
|
2017-01-22 18:11:56 +00:00
|
|
|
function wrap (fn, around) {
|
|
|
|
if (fn.__wrapped) {
|
|
|
|
return fn.__wrapped
|
|
|
|
}
|
2017-01-05 17:27:39 +00:00
|
|
|
|
2017-01-22 18:11:56 +00:00
|
|
|
const _fn = function (...args) {
|
|
|
|
return around.call(this, fn, ...args)
|
2017-01-05 17:27:39 +00:00
|
|
|
}
|
2017-01-22 18:11:56 +00:00
|
|
|
|
|
|
|
// copy all properties
|
|
|
|
Object.assign(_fn, fn)
|
|
|
|
|
|
|
|
_fn.prototype = fn.prototype
|
|
|
|
|
|
|
|
_fn.__wrapped = fn.__wrapped = _fn
|
|
|
|
|
|
|
|
return _fn
|
2017-01-05 17:27:39 +00:00
|
|
|
}
|
2017-01-28 13:50:46 +00:00
|
|
|
|
|
|
|
function getRenderPrototype (Component) {
|
|
|
|
let proto = Component.prototype
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (proto.hasOwnProperty('render')) return proto
|
|
|
|
proto = Object.getPrototypeOf(proto)
|
|
|
|
if (!proto) return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function canOverrideRender (prototype) {
|
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(prototype, 'render')
|
|
|
|
if (!descriptor) return true
|
|
|
|
|
|
|
|
return descriptor.writable
|
|
|
|
}
|