-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
317 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public struct Image { | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
Oops, something went wrong.