Skip to content

Commit bd95c92

Browse files
authored
Update Time implementation (#127)
1 parent d727c77 commit bd95c92

File tree

8 files changed

+111
-95
lines changed

8 files changed

+111
-95
lines changed

Sources/OpenSwiftUI/Core/Graph/GraphHost.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
internal import COpenSwiftUICore
1010
internal import OpenGraphShims
11+
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
1112

1213
private let waitingForPreviewThunks = EnvironmentHelper.bool(for: "XCODE_RUNNING_FOR_PREVIEWS")
1314
private var blockedGraphHosts: [Unmanaged<GraphHost>] = []

Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
2+
13
internal import OpenGraphShims
24

35
public struct _GraphInputs {

Sources/OpenSwiftUI/Core/Util/Time.swift

-93
This file was deleted.

Sources/OpenSwiftUI/Core/View/ViewGraph.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
internal import OpenGraphShims
1010
import Foundation
11+
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
1112

1213
final class ViewGraph: GraphHost {
1314
@inline(__always)

Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Status: WIP
77

88
internal import OpenGraphShims
9+
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
910

1011
protocol ViewRendererHost: ViewGraphDelegate {
1112
var viewGraph: ViewGraph { get }
@@ -61,7 +62,7 @@ extension ViewRendererHost {
6162
return
6263
}
6364
let update = { [self] in
64-
currentTimestamp.advancing(by: interval)
65+
currentTimestamp += interval
6566
let time = currentTimestamp
6667
viewGraph.flushTransactions()
6768
// Signpost.renderUpdate

Sources/OpenSwiftUI/Data/Environment/CachedEnvironment.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import Foundation
1010
internal import OpenGraphShims
11+
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
1112

1213
struct CachedEnvironment {
1314
var environment: Attribute<EnvironmentValues>
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//
2+
// Time.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for RELEASE_2024
6+
// Status: Complete
7+
8+
#if canImport(QuartzCore)
9+
import QuartzCore
10+
#endif
11+
12+
@_spi(ForOpenSwiftUIOnly)
13+
public struct Time: Equatable, Hashable, Comparable {
14+
public var seconds: Double
15+
public init(seconds: Double) {
16+
self.seconds = seconds
17+
}
18+
public init() {
19+
self.seconds = .zero
20+
}
21+
public static let zero: Time = Time(seconds: .zero)
22+
public static let infinity: Time = Time(seconds: .infinity)
23+
24+
#if canImport(QuartzCore)
25+
public static var systemUptime: Time {
26+
Time(seconds: CACurrentMediaTime())
27+
}
28+
#endif
29+
30+
@inlinable
31+
prefix public static func - (lhs: Time) -> Time {
32+
Time(seconds: -lhs.seconds)
33+
}
34+
35+
@inlinable
36+
public static func + (lhs: Time, rhs: Double) -> Time {
37+
Time(seconds: lhs.seconds + rhs)
38+
}
39+
40+
@inlinable
41+
public static func + (lhs: Double, rhs: Time) -> Time {
42+
rhs + lhs
43+
}
44+
45+
@inlinable
46+
public static func - (lhs: Time, rhs: Double) -> Time {
47+
Time(seconds: lhs.seconds - rhs)
48+
}
49+
50+
@inlinable
51+
public static func - (lhs: Time, rhs: Time) -> Double {
52+
lhs.seconds - rhs.seconds
53+
}
54+
55+
@inlinable
56+
public static func * (lhs: Time, rhs: Double) -> Time {
57+
Time(seconds: lhs.seconds * rhs)
58+
}
59+
60+
@inlinable
61+
public static func / (lhs: Time, rhs: Double) -> Time {
62+
return Time(seconds: lhs.seconds / rhs)
63+
}
64+
65+
@inlinable
66+
public static func += (lhs: inout Time, rhs: Double) {
67+
lhs = lhs + rhs
68+
}
69+
70+
@inlinable
71+
public static func -= (lhs: inout Time, rhs: Double) {
72+
lhs = lhs - rhs
73+
}
74+
75+
@inlinable
76+
public static func *= (lhs: inout Time, rhs: Double) {
77+
lhs = lhs * rhs
78+
}
79+
80+
@inlinable
81+
public static func /= (lhs: inout Time, rhs: Double) {
82+
lhs = lhs / rhs
83+
}
84+
85+
@inlinable
86+
public static func < (lhs: Time, rhs: Time) -> Bool {
87+
lhs.seconds < rhs.seconds
88+
}
89+
90+
@inlinable
91+
public static func == (a: Time, b: Time) -> Bool {
92+
a.seconds == b.seconds
93+
}
94+
95+
@inlinable
96+
public func hash(into hasher: inout Hasher) {
97+
hasher.combine(seconds)
98+
}
99+
}
100+
101+
@_spi(ForOpenSwiftUIOnly)
102+
extension Time: Sendable {}

Tests/OpenSwiftUITests/Core/Graph/GraphHostTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
@testable import OpenSwiftUI
66
import Testing
7+
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
78

89
struct GraphHostTests {
910
@Test
@@ -18,7 +19,7 @@ struct GraphHostTests {
1819
graphHost.setTime(Time.infinity)
1920
#expect(graphHost.data.time.seconds == Time.infinity.seconds)
2021

21-
let timeNow = Time.now
22+
let timeNow = Time.systemUptime
2223
graphHost.setTime(timeNow)
2324
#expect(graphHost.data.time.seconds == timeNow.seconds)
2425
#endif

0 commit comments

Comments
 (0)