Improve struct-based components API, indexed hooks

This commit is contained in:
Max Desiatov 2018-11-16 10:33:41 +00:00
parent 335bc25783
commit 7990e3bd85
No known key found for this signature in database
GPG Key ID: FE08EBF9CF58CBA2
5 changed files with 65 additions and 14 deletions

View File

@ -7,16 +7,8 @@
import Foundation
public protocol Default {
init()
}
public typealias Closure<T> = Unique<T>
public struct NoProps: Equatable, Default {
public init() {}
}
protocol ComponentType {
}

View File

@ -0,0 +1,12 @@
//
// Default.swift
// Gluon
//
// Created by Max Desiatov on 16/11/2018.
//
import Foundation
public protocol Default {
init()
}

View File

@ -0,0 +1,12 @@
//
// NoProps.swift
// Gluon
//
// Created by Max Desiatov on 16/11/2018.
//
import Foundation
public struct NoProps: Equatable, Default {
public init() {}
}

View File

@ -7,15 +7,50 @@
import Foundation
private protocol BaseComponentType {
protocol BaseComponent {
}
protocol Component {
associatedtype Props: Equatable
associatedtype Children: Equatable
static func render(_ props: Props, _ children: Children)
}
protocol LeafComponent: Component where Children == NoProps {
static func render(_ props: Props)
}
extension LeafComponent {
static func render(_ props: Props, _ children: NoProps) {
render(props)
}
}
struct Hooks {
func state<T>(_ initial: T,
id: String = "\(#file)\(#line)") -> (T, (T) -> ()) {
return (initial, { _ in })
}
}
private let _hooks = Hooks()
extension Component {
var hooks: Hooks {
return _hooks
}
}
protocol BaseComponentType {
var children: [Node] { get }
init?(props: AnyEquatable, children: [Node])
}
private struct Node: Equatable {
// FIXME: is compiler not being able to derive `Equatable` for this a bug?
static func == (lhs: Node, rhs: Node) -> Bool {
public struct Node: Equatable {
/// Equatable can't be automatically derived for recursive types
public static func == (lhs: Node, rhs: Node) -> Bool {
return lhs.type == rhs.type &&
lhs.children == rhs.children &&
lhs.props == rhs.props

View File

@ -22,7 +22,7 @@ pod 'Gluon'
## Author
Max Desiatov, max@desiatov.com
[Max Desiatov](https://desiatov.com)
## License