From 6bb08a67f05245cccb6b46debd347eab04d56147 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 30 Nov 2018 08:52:34 +0000 Subject: [PATCH] Reconciler work in progress --- Gluon/Source/MountedComponent.swift | 1 + Gluon/Source/StackReconciler.swift | 34 +++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Gluon/Source/MountedComponent.swift b/Gluon/Source/MountedComponent.swift index 44a818aa..d3b3f99e 100644 --- a/Gluon/Source/MountedComponent.swift +++ b/Gluon/Source/MountedComponent.swift @@ -28,6 +28,7 @@ class MountedComponent { final class MountedCompositeComponent: MountedComponent { let type: AnyCompositeComponent.Type + var mountedChild: MountedComponent? var state = [Int: Any]() fileprivate init(_ node: Node, _ type: AnyCompositeComponent.Type) { diff --git a/Gluon/Source/StackReconciler.swift b/Gluon/Source/StackReconciler.swift index 8e14a017..1bff5e84 100644 --- a/Gluon/Source/StackReconciler.swift +++ b/Gluon/Source/StackReconciler.swift @@ -46,13 +46,12 @@ final class StackReconciler { } } - private func render(composite type: AnyCompositeComponent.Type, - component: MountedCompositeComponent) -> Node? { + private func render(component: MountedCompositeComponent) -> Node? { _hooks.currentReconciler = self _hooks.currentComponent = component - let result = type.render(props: component.props, - children: component.children) + let result = component.type.render(props: component.props, + children: component.children) _hooks.currentComponent = nil _hooks.currentReconciler = nil @@ -62,19 +61,32 @@ final class StackReconciler { private func updateStateAndReconcile() { for (component, id, state) in queuedState { - guard let renderedNode = render(composite: component.type, - component: component) else { + component.state[id] = state + + guard let node = render(component: component) else { assertionFailure(""" - state update scheduled for a component that's not composite or - has props and children types that don't match + state update scheduled for a component with props and children types + that don't match """) continue } - component.state[id] = state - reconcile(node: component, with: renderedNode) + + reconcile(component: component, with: node) } } - private func reconcile(node reference: MountedComponent, with node: Node) { + private func reconcile(component: MountedCompositeComponent, + with node: Node) { + switch (component.mountedChild, node.type) { + case let (child, .composite(nodeType)) as (MountedCompositeComponent, ComponentType): + guard child.type == nodeType && + child.props == node.props && + child.children == node.children else { + // FIXME: continue? + return + } + default: + assertionFailure("unhandled case to reconcile") + } } }