Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't reload functional properties #1002

Merged
merged 1 commit into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/proxy/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ function mergeComponents(

const nextString = String(nextAttr)
const injectedBefore = injectedMembers[key]
const isFunction =
nextString.indexOf('function') >= 0 || nextString.indexOf('=>') >= 0
const isArrow = nextString.indexOf('=>') >= 0
const isFunction = nextString.indexOf('function') >= 0 || isArrow
const referToThis = nextString.indexOf('this') >= 0
if (
nextString !== String(prevAttr) ||
(injectedBefore && nextString !== String(injectedBefore)) ||
isFunction
(isArrow && referToThis)
) {
if (!hasRegenerate) {
if (!isFunction) {
Expand Down Expand Up @@ -150,15 +151,13 @@ function inject(target, currentGeneration, injectedMembers) {
Object.keys(injectedMembers).forEach(key => {
try {
if (hasRegenerate) {
const usedThis =
String(injectedMembers[key]).match(/_this([\d])/gi) || []
target[REGENERATE_METHOD](
key,
`(function REACT_HOT_LOADER_SANDBOX () {
var _this = this; // common babel transpile
var _this2 = this; // common babel transpile
var _this3 = this; // common babel transpile
var _this4 = this; // common babel transpile
var _this5 = this; // common babel transpile
var _this6 = this; // common babel transpile
${usedThis.map(name => `var ${name} = this;`)}

return ${injectedMembers[key]};
}).call(this)`,
)
Expand Down
53 changes: 46 additions & 7 deletions test/proxy/consistency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,35 @@ describe('consistency', () => {
/* eslint-enable */
})

it('should reflect external dependencies', () => {
it('should reflect external dependencies(broken, regression)', () => {
/* eslint-disable */
const externalValue = 42
let gen = 0
const generator2 = () => {
const g = gen++
return () => g
}
const generator3 = () => {
const g = gen++
return () => g
}
class BaseClass extends React.Component {
arrow = () => externalValue
secret1 = 1
secret2 = generator2()
secret3 = generator3()
arrow1 = () => externalValue
arrow2 = () => this.secret1 + externalValue

render() {
return this.arrow()
return (
this.arrow1() +
':' +
this.arrow2() +
':' +
this.secret2() +
':' +
this.secret3()
)
}

__reactstandin__regenerateByEval(key, code) {
Expand All @@ -286,15 +307,27 @@ describe('consistency', () => {
const proxy = createProxy(BaseClass)
const Proxy = proxy.get()
const instance = new Proxy()
expect(instance.render()).toBe(42)
expect(instance.render()).toBe(42 + ':' + 43 + ':' + 0 + ':' + 1)

{
const externalValue = 24
class Update1Class extends React.Component {
arrow = () => externalValue
secret = 1
secret2 = generator2()
secret3 = generator3()
arrow1 = () => externalValue
arrow2 = () => this.secret1 + externalValue

render() {
return this.arrow()
return (
this.arrow1() +
':' +
this.arrow2() +
':' +
this.secret2() +
':' +
this.secret3()
)
}

__reactstandin__regenerateByEval(key, code) {
Expand All @@ -306,7 +339,13 @@ describe('consistency', () => {
}
/* eslint-enable */

expect(instance.render()).toBe(24)
// Arrow1 function refer to external variable
// Will not be updated
// while Arrow2 function will

// secret 3 should not be regenrated
// secret 4(this inside) should be regenrated
expect(instance.render()).toMatch(/([\d]+):25:0:1/)
})

it('should stand-for all class members', () => {
Expand Down