-
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.
Merge pull request #83 from omochi/refactor-render
リファクタリング
- Loading branch information
Showing
16 changed files
with
381 additions
and
363 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 was deleted.
Oops, something went wrong.
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,104 @@ | ||
import SRTDOM | ||
|
||
package final class Instance { | ||
final class ListenerBridge { | ||
init() {} | ||
|
||
var js: JSEventListener? | ||
var swift: EventListener? | ||
} | ||
|
||
init() { | ||
|
||
} | ||
|
||
weak var owner: VNode? | ||
var hooks: [any _AnyHookWrapper] = [] | ||
public var dom: JSNode? | ||
var attributes: Attributes = [:] | ||
var listeners: [String: ListenerBridge] = [:] | ||
var contextValueHolder: ContextValueHolder? | ||
var isDirty: Bool = false | ||
|
||
var contextHooks: [any _AnyContextHook] { | ||
hooks.compactMap { $0 as? any _AnyContextHook } | ||
} | ||
|
||
var stateHooks: [any _AnyStateHook] { | ||
hooks.compactMap { $0 as? any _AnyStateHook } | ||
} | ||
|
||
var effectHooks: [any _AnyEffectHook] { | ||
hooks.compactMap { $0 as? any _AnyEffectHook } | ||
} | ||
|
||
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.