Skip to content

Commit

Permalink
Fix issue with not applying updates to DOMFiberElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Carson Katri authored and Carson Katri committed May 22, 2022
1 parent 9555e26 commit 7b2d5bd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ let package = Package(
condition: .when(platforms: [.wasi])
),
"OpenCombineJS",
],
exclude: ["index.html"]
]
),
.executableTarget(
name: "TokamakDemo",
Expand Down
7 changes: 4 additions & 3 deletions Sources/TokamakDOM/DOMFiberRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public struct DOMFiberRenderer: FiberRenderer {
public static func isPrimitive<V>(_ view: V) -> Bool where V: View {
view is HTMLConvertible || view is DOMNodeConvertible
}

private func createElement(_ element: DOMElement) -> JSObject {
let result = document.createElement!(element.content.tag).object!
apply(element.content, to: result)
element.reference = result
return result
}

private func apply(_ content: DOMElement.Content, to element: JSObject) {
for (attribute, value) in content.attributes {
if attribute.isUpdatedAsProperty {
Expand Down Expand Up @@ -136,7 +136,7 @@ public struct DOMFiberRenderer: FiberRenderer {
let element = createElement(newElement)
guard let parentElement = parent.reference ?? rootElement.reference
else { fatalError("The root element was not bound (trying to insert element).") }
if Int((parentElement.children.object?.length.number ?? 0)) > index {
if Int(parentElement.children.object?.length.number ?? 0) > index {
_ = parentElement.insertBefore?(element, parentElement.children[index])
} else {
_ = parentElement.appendChild?(element)
Expand All @@ -152,6 +152,7 @@ public struct DOMFiberRenderer: FiberRenderer {
let replacementElement = createElement(replacement)
_ = parentElement.replaceChild?(previousElement, replacementElement)
case let .update(previous, newContent):
previous.update(with: newContent)
guard let previousElement = previous.reference
else { fatalError("The element does not exist (trying to update element).") }
apply(newContent, to: previousElement)
Expand Down

0 comments on commit 7b2d5bd

Please sign in to comment.