Skip to content

Commit 310b510

Browse files
authored
Add UIBinding._printChanges() (pointfreeco#214)
Useful for debugging how a binding is written to.
1 parent 4d04eb0 commit 310b510

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

Sources/SwiftNavigation/UIBinding.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,25 @@ public struct UIBinding<Value>: Sendable {
440440
binding.transaction = transaction
441441
return binding
442442
}
443+
444+
public func _printChanges(
445+
_ prefix: String = "",
446+
fileID: StaticString = #fileID,
447+
line: UInt = #line
448+
) -> Self {
449+
func open(_ location: some _UIBinding<Value>) -> Self {
450+
Self(
451+
location: _UIBindingPrintChanges(
452+
base: location,
453+
prefix: prefix,
454+
fileID: fileID,
455+
line: line
456+
),
457+
transaction: transaction
458+
)
459+
}
460+
return open(location)
461+
}
443462
}
444463

445464
extension UIBinding: Identifiable where Value: Identifiable {
@@ -793,3 +812,38 @@ private final class _UIBindingOptionalEnumToCase<
793812
hasher.combine(keyPath)
794813
}
795814
}
815+
816+
private final class _UIBindingPrintChanges<Base: _UIBinding>: _UIBinding, @unchecked Sendable {
817+
let base: Base
818+
let prefix: String
819+
let fileID: StaticString
820+
let line: UInt
821+
init(base: Base, prefix: String, fileID: StaticString, line: UInt) {
822+
self.base = base
823+
self.prefix = prefix
824+
self.fileID = fileID
825+
self.line = line
826+
}
827+
var wrappedValue: Base.Value {
828+
get { base.wrappedValue }
829+
set {
830+
var oldDescription = ""
831+
debugPrint(base.wrappedValue, terminator: "", to: &oldDescription)
832+
var newDescription = ""
833+
debugPrint(newValue, terminator: "", to: &newDescription)
834+
print(
835+
"\(prefix.isEmpty ? "UIBinding<\(Value.self)>@\(fileID):\(line)" : prefix):",
836+
oldDescription,
837+
"",
838+
newDescription
839+
)
840+
base.wrappedValue = newValue
841+
}
842+
}
843+
static func == (lhs: _UIBindingPrintChanges, rhs: _UIBindingPrintChanges) -> Bool {
844+
lhs.base == rhs.base
845+
}
846+
func hash(into hasher: inout Hasher) {
847+
hasher.combine(base)
848+
}
849+
}

Sources/SwiftUINavigation/Binding.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,24 @@
8787
}
8888

8989
extension Binding where Value: Sendable {
90-
public func _printChanges(_ prefix: String = "") -> Self {
90+
public func _printChanges(
91+
_ prefix: String = "",
92+
fileID: StaticString = #fileID,
93+
line: UInt = #line
94+
) -> Self {
9195
Self(
9296
get: { self.wrappedValue },
9397
set: { newValue, transaction in
9498
var oldDescription = ""
9599
debugPrint(self.wrappedValue, terminator: "", to: &oldDescription)
96100
var newDescription = ""
97101
debugPrint(newValue, terminator: "", to: &newDescription)
98-
print("\(prefix.isEmpty ? "\(Self.self)" : prefix):", oldDescription, "=", newDescription)
102+
print(
103+
"\(prefix.isEmpty ? "\(Self.self)@\(fileID):\(line)" : prefix):",
104+
oldDescription,
105+
"",
106+
newDescription
107+
)
99108
self.transaction(transaction).wrappedValue = newValue
100109
}
101110
)

0 commit comments

Comments
 (0)