Reorganize files, reconciler cleanups
This commit is contained in:
parent
6bb08a67f0
commit
4f3639254f
|
@ -31,7 +31,7 @@ final class MountedCompositeComponent: MountedComponent {
|
|||
var mountedChild: MountedComponent?
|
||||
var state = [Int: Any]()
|
||||
|
||||
fileprivate init(_ node: Node, _ type: AnyCompositeComponent.Type) {
|
||||
init(_ node: Node, _ type: AnyCompositeComponent.Type) {
|
||||
self.type = type
|
||||
|
||||
super.init(key: node.key, props: node.props, children: node.children)
|
||||
|
@ -42,7 +42,7 @@ final class MountedBaseComponent: MountedComponent {
|
|||
let type: AnyBaseComponent.Type
|
||||
var target: Any?
|
||||
|
||||
fileprivate init(_ node: Node, _ type: AnyBaseComponent.Type) {
|
||||
init(_ node: Node, _ type: AnyBaseComponent.Type) {
|
||||
self.type = type
|
||||
|
||||
super.init(key: node.key, props: node.props, children: node.children)
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
//
|
||||
// Node.swift
|
||||
// Gluon
|
||||
//
|
||||
// Created by Max Desiatov on 30/11/2018.
|
||||
//
|
||||
|
||||
public struct Node: Equatable {
|
||||
/// Equatable can't be automatically derived for `type` property?
|
||||
public static func == (lhs: Node, rhs: Node) -> Bool {
|
||||
return lhs.key == rhs.key &&
|
||||
lhs.type == rhs.type &&
|
||||
lhs.children == rhs.children &&
|
||||
lhs.props == rhs.props
|
||||
}
|
||||
|
||||
let key: String?
|
||||
let props: AnyEquatable
|
||||
let children: AnyEquatable
|
||||
let type: ComponentType
|
||||
}
|
||||
|
||||
extension BaseComponent {
|
||||
public static func node(key: String? = nil,
|
||||
_ props: Props,
|
||||
_ children: Children) -> Node {
|
||||
return Node(key: key,
|
||||
props: AnyEquatable(props),
|
||||
children: AnyEquatable(children),
|
||||
type: .base(self))
|
||||
}
|
||||
}
|
||||
|
||||
extension Node: ChildrenType {
|
||||
}
|
||||
|
||||
extension Array: ChildrenType where Element == Node {
|
||||
}
|
||||
|
||||
extension String: ChildrenType {
|
||||
}
|
|
@ -28,8 +28,11 @@ final class StackReconciler {
|
|||
rootTarget = target
|
||||
|
||||
rootComponent = MountedComponent.make(node)
|
||||
if let component = rootComponent as? MountedBaseComponent,
|
||||
let type = component.type as? RendererBaseComponent.Type {
|
||||
if let component = rootComponent as? MountedBaseComponent {
|
||||
guard let type = component.type as? RendererBaseComponent.Type else {
|
||||
assertionFailure("base component not supported by any renderer")
|
||||
return
|
||||
}
|
||||
component.target = renderer.mountTarget(to: target, with: type)
|
||||
}
|
||||
}
|
||||
|
@ -77,8 +80,19 @@ final class StackReconciler {
|
|||
|
||||
private func reconcile(component: MountedCompositeComponent,
|
||||
with node: Node) {
|
||||
let parentTarget = rootTarget
|
||||
|
||||
switch (component.mountedChild, node.type) {
|
||||
case let (child, .composite(nodeType)) as (MountedCompositeComponent, ComponentType):
|
||||
case let (nil, .base(type)):
|
||||
guard let type = type as? RendererBaseComponent.Type else {
|
||||
assertionFailure("base component not supported by any renderer")
|
||||
return
|
||||
}
|
||||
let newChild = MountedBaseComponent(node, type)
|
||||
renderer.mountTarget(to: parentTarget, with: component)
|
||||
|
||||
case let (child, .composite(nodeType))
|
||||
as (MountedCompositeComponent, ComponentType):
|
||||
guard child.type == nodeType &&
|
||||
child.props == node.props &&
|
||||
child.children == node.children else {
|
||||
|
|
|
@ -93,41 +93,6 @@ enum ComponentType: Equatable {
|
|||
}
|
||||
}
|
||||
|
||||
public struct Node: Equatable {
|
||||
/// Equatable can't be automatically derived for `type` property?
|
||||
public static func == (lhs: Node, rhs: Node) -> Bool {
|
||||
return lhs.key == rhs.key &&
|
||||
lhs.type == rhs.type &&
|
||||
lhs.children == rhs.children &&
|
||||
lhs.props == rhs.props
|
||||
}
|
||||
|
||||
let key: String?
|
||||
let props: AnyEquatable
|
||||
let children: AnyEquatable
|
||||
let type: ComponentType
|
||||
}
|
||||
|
||||
extension BaseComponent {
|
||||
public static func node(key: String? = nil,
|
||||
_ props: Props,
|
||||
_ children: Children) -> Node {
|
||||
return Node(key: key,
|
||||
props: AnyEquatable(props),
|
||||
children: AnyEquatable(children),
|
||||
type: .base(self))
|
||||
}
|
||||
}
|
||||
|
||||
extension Node: ChildrenType {
|
||||
}
|
||||
|
||||
extension Array: ChildrenType where Element == Node {
|
||||
}
|
||||
|
||||
extension String: ChildrenType {
|
||||
}
|
||||
|
||||
public struct View: BaseComponent {
|
||||
public typealias Children = [Node]
|
||||
|
||||
|
|
|
@ -8,12 +8,12 @@
|
|||
import Foundation
|
||||
|
||||
protocol Renderer: class {
|
||||
func mountTarget(to parent: Any, with component: RendererBaseComponent.Type) -> Any
|
||||
func update(target: Any, with component: RendererBaseComponent.Type)
|
||||
func umount(target: Any, from parent: Any, with component: RendererBaseComponent.Type)
|
||||
func mountTarget(to parent: Any, with component: RendererBaseComponent) -> Any
|
||||
func update(target: Any, with component: RendererBaseComponent)
|
||||
func umount(target: Any, from parent: Any, with component: RendererBaseComponent)
|
||||
}
|
||||
|
||||
protocol RendererBaseComponent {
|
||||
protocol RendererBaseComponent: AnyBaseComponent {
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue