Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Binding DynamicProperty implementation #31

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update Binding DynamicProperty implementation
  • Loading branch information
Kyle-Ye committed Jan 31, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
Kyle-Ye Kyle
commit 0214a7360a32cf323504d6578de249dec3b3ae2a
55 changes: 51 additions & 4 deletions Sources/OpenSwiftUI/DataAndStorage/ModelData/Binding/Binding.swift
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@
//
// Created by Kyle on 2023/11/5.
// Lastest Version: iOS 15.5
// Status: Blocked by DynamicProperty
// Status: Complete
// ID: 5436F2B399369BE3B016147A5F8FE9F2

/// A property wrapper type that can read and write a value owned by a source of
/// truth.
@@ -297,9 +298,55 @@ extension Binding {
}

extension Binding: DynamicProperty {
// TODO:
// public static func _makeProperty<V>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<V>, fieldOffset: Int, inputs: inout _GraphInputs) {
// }
private struct ScopedLocation: Location {
var base: AnyLocation<Value>
var wasRead: Bool

init(base: AnyLocation<Value>) {
self.base = base
self.wasRead = base.wasRead
}

func get() -> Value {
base.get()
}

func set(_ value: Value, transaction: Transaction) {
base.set(value, transaction: transaction)
}

func update() -> (Value, Bool) {
base.update()
}
}

private struct Box: DynamicPropertyBox {
var location: LocationBox<ScopedLocation>?

typealias Property = Binding
func destroy() {}
func reset() {}
mutating func update(property: inout Property, phase: _GraphInputs.Phase) -> Bool {
if let location {
if location.location.base !== property.location {
self.location = LocationBox(location: ScopedLocation(base: property.location))
if location.wasRead {
self.location!.wasRead = true
}
}
} else {
location = LocationBox(location: ScopedLocation(base: property.location))
}
let (value, isUpdated) = location!.update()
property.location = location!
property._value = value
return isUpdated ? location!.wasRead : false
}
}

public static func _makeProperty<V>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<V>, fieldOffset: Int, inputs: inout _GraphInputs) {
buffer.append(Box(), fieldOffset: fieldOffset)
}
}

// MARK: - Binding Internal API
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ protocol DynamicPropertyBox<Property>: DynamicProperty {
associatedtype Property: DynamicProperty
func destroy()
func reset()
func update(property: inout Property, phase: _GraphInputs.Phase) -> Bool
mutating func update(property: inout Property, phase: _GraphInputs.Phase) -> Bool
func getState<Value>(type: Value.Type) -> Binding<Value>?
}