Skip to content

Commit

Permalink
fix: improve decorators support (autobind)
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey authored and gregberge committed Dec 27, 2017
1 parent ae129fe commit 56883c9
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions packages/react-stand-in/src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@ function mergeComponents(

const mergedAttrs = Object.assign({}, proxyInstance, nextInstance)
const hasRegenerate = proxyInstance[REGENERATE_METHOD]
const ownKeys = Reflect.ownKeys(
Object.getPrototypeOf(ProxyComponent.prototype),
)
Object.keys(mergedAttrs).forEach(key => {
if (key.startsWith(PREFIX)) return
const nextAttr = nextInstance[key]
const prevAttr = proxyInstance[key]
if (prevAttr && nextAttr) {
if (isNativeFunction(nextAttr) || isNativeFunction(prevAttr)) {
// this is bound method
if (
nextAttr.length === prevAttr.length &&
ProxyComponent.prototype[key]
) {
injectedCode[
const isSameArity = nextAttr.length === prevAttr.length
const existsInPrototype =
ownKeys.indexOf(key) >= 0 || ProxyComponent.prototype[key]
if (isSameArity && existsInPrototype) {
injectedCode[key] = `Object.getPrototypeOf(this)['${
key
] = `Object.getPrototypeOf(this)['${key}'].bind(this)`
}'].bind(this)`
} else {
console.error(
'React-stand-in:',
Expand All @@ -61,6 +64,9 @@ function mergeComponents(
key,
nextAttr,
'. Unable to reproduce, use arrow functions instead.',
`(arity: ${nextAttr.length}/${prevAttr.length}, proto: ${
existsInPrototype ? 'yes' : 'no'
}`,
)
}
return
Expand Down Expand Up @@ -98,18 +104,29 @@ function mergeComponents(
return injectedCode
}

function areDescriptorEqual(a, b) {
for (const key in a) {
if (String(a[key]) !== String(b[key])) {
return false
}
}
return true
}

function checkLifeCycleMethods(ProxyComponent, NextComponent) {
try {
const p1 = ProxyComponent.prototype
const p1 = Object.getPrototypeOf(ProxyComponent.prototype)
const p2 = NextComponent.prototype
reactLifeCycleMethods.forEach(key => {
if (String(p1[key]) !== String(p2[key])) {
const d1 = Object.getOwnPropertyDescriptor(p1, key) || { value: p1[key] }
const d2 = Object.getOwnPropertyDescriptor(p2, key) || { value: p2[key] }
if (!areDescriptorEqual(d1, d2)) {
console.error(
'React-stand-in:',
'You did update',
ProxyComponent.name,
's lifecycle method',
p2[key],
key,
'. Unable to repeat',
)
}
Expand Down

0 comments on commit 56883c9

Please sign in to comment.