Skip to content

Commit 4c855cf

Browse files
authored
Update ViewGraph.NextUpdate (#697)
1 parent 2b3441a commit 4c855cf

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

Sources/OpenSwiftUICore/View/Graph/ViewGraph.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ package final class ViewGraph: GraphHost {
9090

9191
private var mainUpdates: Int = 0
9292

93-
// MARK: - ViewGraph + NextUpdate
94-
93+
// MARK: - ViewGraph + NextUpdate [6.5.4]
94+
9595
package struct NextUpdate {
9696
package private(set) var time: Time = .infinity
9797

@@ -101,6 +101,8 @@ package final class ViewGraph: GraphHost {
101101
_interval.isFinite ? _interval : .zero
102102
}
103103

104+
private var _defaultIntervalWasRequested: Bool = false
105+
104106
package private(set) var reasons: Set<UInt32> = []
105107

106108
package mutating func at(_ next: Time) {
@@ -113,18 +115,23 @@ package final class ViewGraph: GraphHost {
113115
}
114116
let interval = velocity < 320 ? 1 / 80.0 : 1 / 120.0
115117
let highFrameRateReason: UInt32 = _HighFrameRateReasonMake(0)
116-
_interval = min(interval, _interval)
118+
var newInterval = min(interval, _interval)
119+
if _defaultIntervalWasRequested && newInterval > 1 / 60.0 {
120+
newInterval = .infinity
121+
}
122+
_interval = newInterval
117123
reasons.insert(highFrameRateReason)
118124
}
119125

120126
package mutating func interval(_ interval: Double, reason: UInt32? = nil) {
121127
if interval == .zero {
122-
if _interval > 1 / 60 {
123-
_interval = .infinity
124-
}
128+
_defaultIntervalWasRequested = true
125129
} else {
126130
_interval = min(interval, _interval)
127131
}
132+
if _defaultIntervalWasRequested && _interval > 1 / 60 {
133+
_interval = .infinity
134+
}
128135
if let reason {
129136
reasons.insert(reason)
130137
}

Tests/OpenSwiftUICoreTests/View/Graph/ViewGraphTests.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,32 @@ struct ViewGraphTests {
1010
struct NextUpdateTests {
1111
typealias Update = ViewGraph.NextUpdate
1212

13-
@Test
14-
func interval() {
15-
let update = Update()
16-
#expect(update.interval == .zero)
13+
@Test(arguments: [
14+
(0.0, Double.zero, Set<UInt32>()),
15+
(159.0, Double.zero, Set<UInt32>()),
16+
(160.0, 1 / 80.0, [0x27_0000]),
17+
(319.0, 1 / 80.0, [0x27_0000]),
18+
(320.0, 1 / 120.0, [0x27_0000]),
19+
(1000.0, 1 / 120.0, [0x27_0000]),
20+
] as [(Double, Double, Set<UInt32>)])
21+
func maxVelocity(velocity: Double, expectedInterval: Double, expectedReasons: Set<UInt32>) {
22+
var update = Update()
23+
update.maxVelocity(velocity)
24+
#expect(update.interval == expectedInterval)
25+
#expect(update.reasons == expectedReasons)
26+
}
27+
28+
@Test(arguments: [
29+
(0.05, nil, 0.05, Set<UInt32>()),
30+
(0.025, nil, 0.025, Set<UInt32>()),
31+
(0.025, 1 as UInt32?, 0.025, [1] as Set<UInt32>),
32+
(0.0, nil, Double.zero, Set<UInt32>()),
33+
] as [(Double, UInt32?, Double, Set<UInt32>)])
34+
func intervalWithReason(interval: Double, reason: UInt32?, expectedInterval: Double, expectedReasons: Set<UInt32>) {
35+
var update = Update()
36+
update.interval(interval, reason: reason)
37+
#expect(update.interval == expectedInterval)
38+
#expect(update.reasons == expectedReasons)
1739
}
1840
}
1941
}

0 commit comments

Comments
 (0)