From 7990e3bd852699209074ed3873dd490d8221a184 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 16 Nov 2018 10:33:41 +0000 Subject: [PATCH] Improve struct-based components API, indexed hooks --- Gluon/Source/Classes.swift | 8 ------- Gluon/Source/Default.swift | 12 +++++++++++ Gluon/Source/NoProps.swift | 12 +++++++++++ Gluon/Source/Structs.swift | 43 ++++++++++++++++++++++++++++++++++---- README.md | 4 ++-- 5 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 Gluon/Source/Default.swift create mode 100644 Gluon/Source/NoProps.swift diff --git a/Gluon/Source/Classes.swift b/Gluon/Source/Classes.swift index 7a78141a..afdf3012 100644 --- a/Gluon/Source/Classes.swift +++ b/Gluon/Source/Classes.swift @@ -7,16 +7,8 @@ import Foundation -public protocol Default { - init() -} - public typealias Closure = Unique -public struct NoProps: Equatable, Default { - public init() {} -} - protocol ComponentType { } diff --git a/Gluon/Source/Default.swift b/Gluon/Source/Default.swift new file mode 100644 index 00000000..bffbaedd --- /dev/null +++ b/Gluon/Source/Default.swift @@ -0,0 +1,12 @@ +// +// Default.swift +// Gluon +// +// Created by Max Desiatov on 16/11/2018. +// + +import Foundation + +public protocol Default { + init() +} diff --git a/Gluon/Source/NoProps.swift b/Gluon/Source/NoProps.swift new file mode 100644 index 00000000..6065bedb --- /dev/null +++ b/Gluon/Source/NoProps.swift @@ -0,0 +1,12 @@ +// +// NoProps.swift +// Gluon +// +// Created by Max Desiatov on 16/11/2018. +// + +import Foundation + +public struct NoProps: Equatable, Default { + public init() {} +} diff --git a/Gluon/Source/Structs.swift b/Gluon/Source/Structs.swift index 5608fcc7..31f0abce 100644 --- a/Gluon/Source/Structs.swift +++ b/Gluon/Source/Structs.swift @@ -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(_ 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 diff --git a/README.md b/README.md index 34524b90..bb78d1ab 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Gluon -[![CI Status](https://img.shields.io/travis/Max Desiatov/Gluon.svg?style=flat)](https://travis-ci.org/Max Desiatov/Gluon) +[![CI Status](https://img.shields.io/travis/MaxDesiatov/Gluon.svg?style=flat)](https://travis-ci.org/Max Desiatov/Gluon) [![Version](https://img.shields.io/cocoapods/v/Gluon.svg?style=flat)](https://cocoapods.org/pods/Gluon) [![License](https://img.shields.io/cocoapods/l/Gluon.svg?style=flat)](https://cocoapods.org/pods/Gluon) [![Platform](https://img.shields.io/cocoapods/p/Gluon.svg?style=flat)](https://cocoapods.org/pods/Gluon) @@ -22,7 +22,7 @@ pod 'Gluon' ## Author -Max Desiatov, max@desiatov.com +[Max Desiatov](https://desiatov.com) ## License