Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ package struct ViewGraphGeometryObservers<Measurer> where Measurer: ViewGraphGeo
return result
}

// [?]
mutating func notify() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If notify() is intended to be part of the same package-level API surface as needsUpdate(graph:) / notifySizes(), consider marking it package (it’s currently internal, so other modules in the package can’t call it). If it’s only meant for tests/internal use, this may be fine.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

let keys = store.keys
for proposal in keys {
guard var observer = store[proposal] else { continue }
guard case let .pending(oldSize, pending: newSize) = observer.storage else {
continue
}
if oldSize != newSize {
observer.callback(oldSize, newSize)
}
observer.storage = .value(newSize)
store[proposal] = observer
}
}

/// Adds an observer for a specific layout proposal.
///
/// - Parameters:
Expand Down Expand Up @@ -202,7 +218,7 @@ package struct ViewGraphGeometryObservers<Measurer> where Measurer: ViewGraphGeo
return true
case let .pending(value, _):
guard size != value else {
self = .value(size)
self = .value(size) // ?
return false
}
self = .pending(value, pending: size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ private struct TestMeasurer: ViewGraphGeometryMeasurer {
typealias Proposal = CGSize
typealias Size = CGFloat

static var mockValue: CGFloat?

static func measure(given proposal: CGSize, in graph: ViewGraph) -> CGFloat {
max(proposal.width, proposal.height)
mockValue ?? max(proposal.width, proposal.height)
}

static func measure(proposal: CGSize, layoutComputer: LayoutComputer, insets: EdgeInsets) -> CGFloat {
max(proposal.width, proposal.height)
mockValue ?? max(proposal.width, proposal.height)
}

static var invalidValue: CGFloat = .nan
Expand All @@ -28,14 +30,19 @@ struct ViewGraphGeometryObserversTests {
@MainActor
@Test
func observeCallback() async throws {
// TODO: when the callback got called.
await confirmation(expectedCount: 0) { confirm in
await confirmation(expectedCount: 1) { confirm in
var observers = Observers()
observers.addObserver(for: CGSize(width: 10, height: 20)) { _, _ in
observers.addObserver(for: CGSize(width: 10, height: 20)) { oldSize, newSize in
confirm()
#expect(oldSize.isApproximatelyEqual(to: 20.0))
#expect(newSize.isApproximatelyEqual(to: 30.0))
}
let emptyViewGraph = ViewGraph(rootViewType: EmptyView.self)
_ = observers.needsUpdate(graph: emptyViewGraph)
TestMeasurer.mockValue = 30.0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since TestMeasurer.mockValue is static, consider resetting it (e.g. via defer { TestMeasurer.mockValue = nil }) to avoid state leaking between tests and creating order-dependent behavior.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

defer { TestMeasurer.mockValue = nil }
_ = observers.needsUpdate(graph: emptyViewGraph)
observers.notify()
}
}
#endif
Expand Down
Loading