Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi committed Apr 24, 2024
1 parent 4ce6e54 commit 9702b51
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions Sources/React/Renderer/ReactRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,10 @@ public final class ReactRoot {
var doesRenderChildren = true

if let newTree {
let oldInstance = oldTree?.instance
oldTree?.instance = nil
let instance = oldInstance ?? Instance()
newTree.instance = instance
let isFirst = oldInstance == nil

try renderDOM(tree: newTree, instance: instance)
try moveDOM(instance: instance)
try updateContextValue(tree: newTree, instance: instance)
prepareHooks(component: newTree.component, instance: instance, isFirst: isFirst)
subscribeHooks(instance: instance)
let instance = transferInstance(newTree: newTree, oldTree: oldTree)
let isFirst = oldTree == nil

try preRender(tree: newTree, instance: instance, isFirst: isFirst)

// short circuit
doesRenderChildren = instance.consumeDirty() ||
Expand All @@ -186,11 +179,27 @@ public final class ReactRoot {
}
} else if let oldTree {
if let instance = oldTree.instance {
try cleanup(instance: instance)
try postRenderCleanup(instance: instance)
}
}
}

private func transferInstance(newTree: VNode, oldTree: VNode?) -> Instance {
let oldInstance = oldTree?.instance
oldTree?.instance = nil
let instance = oldInstance ?? Instance()
newTree.instance = instance
return instance
}

private func preRender(tree: VNode, instance: Instance, isFirst: Bool) throws {
try renderDOM(tree: tree, instance: instance)
try moveDOM(instance: instance)
try updateContextValue(tree: tree, instance: instance)
prepareHooks(component: tree.component, instance: instance, isFirst: isFirst)
subscribeHooks(instance: instance)
}

private func renderDOM(tree: VNode, instance: Instance) throws {
switch tree.component {
case let newTag as HTMLElement:
Expand Down Expand Up @@ -288,8 +297,9 @@ public final class ReactRoot {
}

private func isChanged(new: VNode, old: VNode?) -> Bool {
guard let newDeps = new.component.deps else { return false }
return newDeps != old?.component.deps
if let newDeps = new.component.deps,
newDeps == old?.component.deps { return false }
return true
}

private func postRender(instance: Instance) throws {
Expand All @@ -300,7 +310,7 @@ public final class ReactRoot {
}
}

private func cleanup(instance: Instance) throws {
private func postRenderCleanup(instance: Instance) throws {
try instance.dom?.remove()

for effect in instance.effectHooks {
Expand Down

0 comments on commit 9702b51

Please sign in to comment.