Skip to content

Commit

Permalink
refactor wip
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi committed Apr 23, 2024
1 parent 5b5a205 commit 94f43f0
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 282 deletions.
19 changes: 19 additions & 0 deletions Sources/React/Element/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,22 @@ extension Component {
extractGhostDefault(input)
}
}

enum Components {
static func extractHooks(_ value: Any) -> [any _AnyHookWrapper] {
var hooks: [any _AnyHookWrapper] = []

let mirror = Mirror(reflecting: value)
for mc in mirror.children {
switch mc.value {
case let hook as any _AnyHookWrapper:
hooks.append(hook)
case let hook as any Hook:
hooks += extractHooks(hook)
default: break
}
}

return hooks
}
}
10 changes: 9 additions & 1 deletion Sources/React/Hooks/Context/ContextValueProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extension ContextValue {
public typealias Provider = ContextValueProvider<Self>
}

public struct ContextValueProvider<Value: ContextValue>: Component {
public struct ContextValueProvider<Value: ContextValue>: Component & _AnyContextValueProvider {
public init(
key: AnyHashable? = nil,
value: Value,
Expand All @@ -29,9 +29,17 @@ public struct ContextValueProvider<Value: ContextValue>: Component {
children
}

var _contextValueType: any ContextValue.Type { Value.self }
var _contextValue: any ContextValue { value }

public static func _extractGhost(_ input: GhostInput<Self>) -> Ghost {
var ghost = extractGhostDefault(input)
ghost.contextValue = (type: Value.self, value: input.component.value)
return ghost
}
}

internal protocol _AnyContextValueProvider {
var _contextValueType: any ContextValue.Type { get }
var _contextValue: any ContextValue { get }
}
5 changes: 5 additions & 0 deletions Sources/React/Image/Image.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public struct Image {

}


92 changes: 92 additions & 0 deletions Sources/React/Renderer/Instance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import SRTDOM

internal final class Instance {
final class ListenerBridge {
init() {}

var js: JSEventListener?
var swift: EventListener?
}

init() {

}

weak var owner: VNode?
var hooks: [any _AnyHookWrapper]?
var dom: JSNode?
var attributes: Attributes = [:]
var listeners: [String: ListenerBridge] = [:]
var contextValueHolder: ContextValueHolder?
var isDirty: Bool = false

func renderDOMAttributes(
attributes newAttributes: Attributes
) throws {
let dom = try (self.dom?.asHTMLElement()).unwrap("dom.asHTMLElement")
let oldAttributes = self.attributes

for name in self.attributes.keys {
if newAttributes[name] == nil {
try dom.removeAttribute(name)
}
}

for (name, newValue) in newAttributes {
if newValue != oldAttributes[name] {
try dom.setAttribute(name, newValue)
}
}

self.attributes = newAttributes
}

func renderDOMListeners(
listeners newListeners: EventListeners
) throws {
let dom = try (self.dom?.asHTMLElement()).unwrap("dom.asHTMLElement")

// コピー必要なんだっけ?
for (type, bridge) in Array(self.listeners) {
if bridge.swift != newListeners[type] {
if let js = bridge.js {
try dom.removeEventListener(type, js)
}
self.listeners[type] = nil
}
}

for (type, newListener) in newListeners {
if let bridge = self.listeners[type] {
if newListener == bridge.swift {
continue
}

bridge.swift = newListener
} else {
let bridge = ListenerBridge()
self.listeners[type] = bridge

let js = JSEventListener { [weak bridge] (event) in
guard let bridge, let swift = bridge.swift else { return }
swift(event)
}

bridge.js = js
bridge.swift = newListener

try dom.addEventListener(type, js)
}
}
}

func markDirty() {
isDirty = true
}

func consumeDirty() -> Bool {
defer { isDirty = false }
return isDirty
}

}
Loading

0 comments on commit 94f43f0

Please sign in to comment.