Skip to content

Commit

Permalink
fix: RHL could update non-relative components
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Dec 19, 2018
1 parent b153755 commit 5d4f226
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/reconciler/componentComparator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getIdByType,
getProxyByType,
isRegisteredComponent,
updateProxyById,
Expand All @@ -13,6 +14,11 @@ import { areSwappable } from './utils'
import { PROXY_KEY, UNWRAP_PROXY } from '../proxy'
import { resolveType } from './resolver'

const getInnerComponentType = component => {
const unwrapper = component[UNWRAP_PROXY]
return unwrapper ? unwrapper() : component
}

const compareComponents = (oldType, newType, setNewType) => {
let defaultResult = oldType === newType

Expand Down Expand Up @@ -49,17 +55,15 @@ const compareComponents = (oldType, newType, setNewType) => {
return defaultResult
}

if (
newType !== oldType &&
areSwappable(newType, oldType) &&
!!oldType[PROXY_KEY] === !!newType[PROXY_KEY]
) {
if (newType !== oldType && areSwappable(newType, oldType)) {
const unwrapFactory = newType[UNWRAP_PROXY]
const oldProxy = unwrapFactory && getProxyByType(unwrapFactory())
if (oldProxy) {
oldProxy.dereference()
updateProxyById(oldType[PROXY_KEY], newType[UNWRAP_PROXY]())
updateProxyById(newType[PROXY_KEY], oldType[UNWRAP_PROXY]())
updateProxyById(
oldType[PROXY_KEY] || getIdByType(oldType),
getInnerComponentType(newType),
)
} else {
setNewType(newType)
}
Expand Down
6 changes: 5 additions & 1 deletion src/reconciler/hotReplacementRender.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PROXY_IS_MOUNTED, PROXY_KEY, UNWRAP_PROXY } from '../proxy'
import {
getIdByType,
isRegisteredComponent,
isTypeBlacklisted,
updateProxyById,
Expand Down Expand Up @@ -354,7 +355,10 @@ const hotReplacementRender = (instance, stack) => {
// they are both registered, or have equal code/displayname/signature

// update proxy using internal PROXY_KEY
updateProxyById(stackChild.type[PROXY_KEY], childType)
updateProxyById(
stackChild.type[PROXY_KEY] || getIdByType(stackChild.type),
childType,
)

next(stackChild.instance)
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/reconciler/proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export const updateFunctionProxyById = (id, type, updater) => {
}

export const updateProxyById = (id, type, options = {}) => {
if (!id) {
return null
}
// Remember the ID.
idsByType.set(type, id)

Expand Down

0 comments on commit 5d4f226

Please sign in to comment.