|
| 1 | +protocol DOMElementModifier: AnyObject { |
| 2 | + associatedtype Value |
| 3 | + |
| 4 | + static var key: DOMElementModifiers.Key<Self> { get } |
| 5 | + |
| 6 | + init(value: consuming Value, upstream: Self?, _ context: inout _RenderContext) |
| 7 | + func updateValue(_ value: consuming Value, _ context: inout _RenderContext) |
| 8 | + |
| 9 | + func mount(_ node: DOM.Node, _ context: inout _CommitContext) -> AnyUnmountable |
| 10 | +} |
| 11 | + |
| 12 | +extension DOMElementModifier { |
| 13 | + static var key: DOMElementModifiers.Key<Self> { |
| 14 | + DOMElementModifiers.Key(Self.self) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +protocol Unmountable: AnyObject { |
| 19 | + func unmount(_ context: inout _CommitContext) |
| 20 | +} |
| 21 | + |
| 22 | +struct DOMElementModifiers { |
| 23 | + struct Key<Directive: DOMElementModifier> { |
| 24 | + let typeID: ObjectIdentifier |
| 25 | + |
| 26 | + init(_: Directive.Type) { |
| 27 | + typeID = ObjectIdentifier(Directive.self) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private var storage: [ObjectIdentifier: any DOMElementModifier] = [:] |
| 32 | + |
| 33 | + subscript<Directive: DOMElementModifier>(_ key: Key<Directive>) -> Directive? { |
| 34 | + get { |
| 35 | + storage[key.typeID] as? Directive |
| 36 | + } |
| 37 | + set { |
| 38 | + if let newValue = newValue { |
| 39 | + storage[key.typeID] = newValue |
| 40 | + } else { |
| 41 | + storage.removeValue(forKey: key.typeID) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + consuming func takeModifiers() -> [any DOMElementModifier] { |
| 47 | + let directives = Array(storage.values) |
| 48 | + storage.removeAll() |
| 49 | + return directives |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +struct AnyUnmountable { |
| 54 | + private let _unmount: (inout _CommitContext) -> Void |
| 55 | + |
| 56 | + init(_ unmountable: some Unmountable) { |
| 57 | + self._unmount = unmountable.unmount(_:) |
| 58 | + } |
| 59 | + |
| 60 | + func unmount(_ context: inout _CommitContext) { |
| 61 | + _unmount(&context) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +final class BindingModifier<Configuration>: DOMElementModifier, Unmountable where Configuration: BindingConfiguration { |
| 66 | + typealias Value = Binding<Configuration.Value> |
| 67 | + |
| 68 | + private var lastValue: Configuration.Value |
| 69 | + var binding: Value |
| 70 | + |
| 71 | + var mountedNode: DOM.Node? |
| 72 | + var sink: DOM.EventSink? |
| 73 | + var accessor: DOM.PropertyAccessor? |
| 74 | + var isDirty: Bool = false |
| 75 | + |
| 76 | + init(value: consuming Value, upstream: BindingModifier?, _ context: inout _RenderContext) { |
| 77 | + self.lastValue = value.wrappedValue |
| 78 | + self.binding = value |
| 79 | + } |
| 80 | + |
| 81 | + func updateValue(_ value: consuming Value, _ context: inout _RenderContext) { |
| 82 | + self.binding = value |
| 83 | + |
| 84 | + if !Configuration.equals(binding.wrappedValue, lastValue) { |
| 85 | + self.lastValue = binding.wrappedValue |
| 86 | + markDirty(&context) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private func markDirty(_ context: inout _RenderContext) { |
| 91 | + guard !isDirty else { return } |
| 92 | + isDirty = true |
| 93 | + |
| 94 | + context.commitPlan.addNodeAction( |
| 95 | + CommitAction(run: updateDOMNode) |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + private func updateDOMNode(_ context: inout _CommitContext) { |
| 100 | + guard let accessor = self.accessor else { return } |
| 101 | + guard let value = Configuration.writeValue(lastValue) else { |
| 102 | + logWarning("Cannot set value \(lastValue) to the DOM") |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + logTrace("setting value \(value) to accessor") |
| 107 | + accessor.set(value) |
| 108 | + isDirty = false |
| 109 | + } |
| 110 | + |
| 111 | + func mount(_ node: DOM.Node, _ context: inout _CommitContext) -> AnyUnmountable { |
| 112 | + if mountedNode != nil { |
| 113 | + assertionFailure("Binding effect can only be mounted on a single element") |
| 114 | + self.unmount(&context) |
| 115 | + } |
| 116 | + |
| 117 | + self.mountedNode = node |
| 118 | + self.accessor = context.dom.makePropertyAccessor(node, name: Configuration.propertyName) |
| 119 | + |
| 120 | + let sink = context.dom.makeEventSink { [self] name, event in |
| 121 | + guard let value = self.accessor?.get() else { |
| 122 | + logWarning("Unexpected property value read from accessor") |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + guard let value = Configuration.readValue(value) else { |
| 127 | + logWarning("Unexpected property value read from accessor") |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + self.lastValue = value |
| 132 | + self.binding.wrappedValue = value |
| 133 | + } |
| 134 | + |
| 135 | + context.dom.addEventListener(node, event: Configuration.eventName, sink: sink) |
| 136 | + return AnyUnmountable(self) |
| 137 | + } |
| 138 | + |
| 139 | + func unmount(_ context: inout _CommitContext) { |
| 140 | + guard let sink = self.sink, let node = self.mountedNode else { |
| 141 | + assertionFailure("Binding effect can only be unmounted on a mounted element") |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + context.dom.removeEventListener(node, event: "input", sink: sink) |
| 146 | + self.mountedNode = nil |
| 147 | + self.sink = nil |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +protocol BindingConfiguration { |
| 152 | + associatedtype Value |
| 153 | + static var propertyName: String { get } |
| 154 | + static var eventName: String { get } |
| 155 | + static func readValue(_ jsValue: DOM.PropertyValue) -> Value? |
| 156 | + static func writeValue(_ value: Value) -> DOM.PropertyValue? |
| 157 | + static func equals(_ lhs: Value, _ rhs: Value) -> Bool |
| 158 | +} |
| 159 | + |
| 160 | +extension BindingConfiguration where Value == String { |
| 161 | + static func equals(_ lhs: Value, _ rhs: Value) -> Bool { |
| 162 | + lhs.utf8Equals(rhs) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +extension BindingConfiguration where Value: Equatable { |
| 167 | + static func equals(_ lhs: Value, _ rhs: Value) -> Bool { |
| 168 | + lhs == rhs |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +extension BindingConfiguration where Value == Double { |
| 173 | + static func equals(_ lhs: Value, _ rhs: Value) -> Bool { |
| 174 | + // a bit hacky, but this is to avoid unnecessary updates when the value is NaN |
| 175 | + guard !(lhs.isNaN && rhs.isNaN) else { return true } |
| 176 | + return lhs == rhs |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +struct TextBindingConfiguration: BindingConfiguration { |
| 181 | + typealias Value = String |
| 182 | + static var propertyName: String { "value" } |
| 183 | + static var eventName: String { "input" } |
| 184 | + static func readValue(_ jsValue: DOM.PropertyValue) -> Value? { |
| 185 | + switch jsValue { |
| 186 | + case let .string(value): |
| 187 | + return value |
| 188 | + default: |
| 189 | + return nil |
| 190 | + } |
| 191 | + } |
| 192 | + static func writeValue(_ value: Value) -> DOM.PropertyValue? { |
| 193 | + .string(value) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +struct NumberBindingConfiguration: BindingConfiguration { |
| 198 | + typealias Value = Double? |
| 199 | + static var propertyName: String { "valueAsNumber" } |
| 200 | + static var eventName: String { "input" } |
| 201 | + static func readValue(_ jsValue: DOM.PropertyValue) -> Value? { |
| 202 | + switch jsValue { |
| 203 | + case let .number(value): |
| 204 | + value.isNaN ? nil : value |
| 205 | + default: |
| 206 | + nil |
| 207 | + } |
| 208 | + } |
| 209 | + static func writeValue(_ value: Value) -> DOM.PropertyValue? { |
| 210 | + guard let value, !value.isNaN else { |
| 211 | + return .undefined |
| 212 | + } |
| 213 | + |
| 214 | + return .number(value) |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +struct CheckboxBindingConfiguration: BindingConfiguration { |
| 219 | + typealias Value = Bool |
| 220 | + static var propertyName: String { "checked" } |
| 221 | + static var eventName: String { "change" } |
| 222 | + static func readValue(_ jsValue: DOM.PropertyValue) -> Value? { |
| 223 | + switch jsValue { |
| 224 | + case let .boolean(value): |
| 225 | + return value |
| 226 | + default: |
| 227 | + return nil |
| 228 | + } |
| 229 | + } |
| 230 | + static func writeValue(_ value: Value) -> DOM.PropertyValue? { |
| 231 | + .boolean(value) |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +struct DependencyList: ~Copyable { |
| 236 | + private var downstreams: [() -> Void] = [] |
| 237 | + |
| 238 | + mutating func add(_ invalidate: @escaping () -> Void) { |
| 239 | + downstreams.append(invalidate) |
| 240 | + } |
| 241 | + |
| 242 | + func invalidateAll() { |
| 243 | + for downstream in downstreams { |
| 244 | + downstream() |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments