|
4 | 4 | //
|
5 | 5 | // Created by Kyle on 2023/11/5.
|
6 | 6 | // Lastest Version: iOS 15.5
|
7 |
| -// Status: Blocked by DynamicProperty |
| 7 | +// Status: Complete |
| 8 | +// ID: 5436F2B399369BE3B016147A5F8FE9F2 |
8 | 9 |
|
9 | 10 | /// A property wrapper type that can read and write a value owned by a source of
|
10 | 11 | /// truth.
|
@@ -297,9 +298,55 @@ extension Binding {
|
297 | 298 | }
|
298 | 299 |
|
299 | 300 | extension Binding: DynamicProperty {
|
300 |
| - // TODO: |
301 |
| - // public static func _makeProperty<V>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<V>, fieldOffset: Int, inputs: inout _GraphInputs) { |
302 |
| - // } |
| 301 | + private struct ScopedLocation: Location { |
| 302 | + var base: AnyLocation<Value> |
| 303 | + var wasRead: Bool |
| 304 | + |
| 305 | + init(base: AnyLocation<Value>) { |
| 306 | + self.base = base |
| 307 | + self.wasRead = base.wasRead |
| 308 | + } |
| 309 | + |
| 310 | + func get() -> Value { |
| 311 | + base.get() |
| 312 | + } |
| 313 | + |
| 314 | + func set(_ value: Value, transaction: Transaction) { |
| 315 | + base.set(value, transaction: transaction) |
| 316 | + } |
| 317 | + |
| 318 | + func update() -> (Value, Bool) { |
| 319 | + base.update() |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + private struct Box: DynamicPropertyBox { |
| 324 | + var location: LocationBox<ScopedLocation>? |
| 325 | + |
| 326 | + typealias Property = Binding |
| 327 | + func destroy() {} |
| 328 | + func reset() {} |
| 329 | + mutating func update(property: inout Property, phase: _GraphInputs.Phase) -> Bool { |
| 330 | + if let location { |
| 331 | + if location.location.base !== property.location { |
| 332 | + self.location = LocationBox(location: ScopedLocation(base: property.location)) |
| 333 | + if location.wasRead { |
| 334 | + self.location!.wasRead = true |
| 335 | + } |
| 336 | + } |
| 337 | + } else { |
| 338 | + location = LocationBox(location: ScopedLocation(base: property.location)) |
| 339 | + } |
| 340 | + let (value, isUpdated) = location!.update() |
| 341 | + property.location = location! |
| 342 | + property._value = value |
| 343 | + return isUpdated ? location!.wasRead : false |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + public static func _makeProperty<V>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<V>, fieldOffset: Int, inputs: inout _GraphInputs) { |
| 348 | + buffer.append(Box(), fieldOffset: fieldOffset) |
| 349 | + } |
303 | 350 | }
|
304 | 351 |
|
305 | 352 | // MARK: - Binding Internal API
|
|
0 commit comments