Skip to content

Update Time implementation #127

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

Merged
merged 1 commit into from
Sep 30, 2024
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
1 change: 1 addition & 0 deletions Sources/OpenSwiftUI/Core/Graph/GraphHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

internal import COpenSwiftUICore
internal import OpenGraphShims
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore

private let waitingForPreviewThunks = EnvironmentHelper.bool(for: "XCODE_RUNNING_FOR_PREVIEWS")
private var blockedGraphHosts: [Unmanaged<GraphHost>] = []
Expand Down
2 changes: 2 additions & 0 deletions Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore

internal import OpenGraphShims

public struct _GraphInputs {
Expand Down
93 changes: 0 additions & 93 deletions Sources/OpenSwiftUI/Core/Util/Time.swift

This file was deleted.

1 change: 1 addition & 0 deletions Sources/OpenSwiftUI/Core/View/ViewGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

internal import OpenGraphShims
import Foundation
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore

final class ViewGraph: GraphHost {
@inline(__always)
Expand Down
3 changes: 2 additions & 1 deletion Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Status: WIP

internal import OpenGraphShims
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore

protocol ViewRendererHost: ViewGraphDelegate {
var viewGraph: ViewGraph { get }
Expand Down Expand Up @@ -61,7 +62,7 @@ extension ViewRendererHost {
return
}
let update = { [self] in
currentTimestamp.advancing(by: interval)
currentTimestamp += interval
let time = currentTimestamp
viewGraph.flushTransactions()
// Signpost.renderUpdate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
internal import OpenGraphShims
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore

struct CachedEnvironment {
var environment: Attribute<EnvironmentValues>
Expand Down
102 changes: 102 additions & 0 deletions Sources/OpenSwiftUICore/Data/Time.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// Time.swift
// OpenSwiftUICore
//
// Audited for RELEASE_2024
// Status: Complete

#if canImport(QuartzCore)
import QuartzCore
#endif

@_spi(ForOpenSwiftUIOnly)
public struct Time: Equatable, Hashable, Comparable {
public var seconds: Double
public init(seconds: Double) {
self.seconds = seconds
}
public init() {
self.seconds = .zero
}
public static let zero: Time = Time(seconds: .zero)
public static let infinity: Time = Time(seconds: .infinity)

#if canImport(QuartzCore)
public static var systemUptime: Time {
Time(seconds: CACurrentMediaTime())
}
#endif

@inlinable
prefix public static func - (lhs: Time) -> Time {
Time(seconds: -lhs.seconds)
}

@inlinable
public static func + (lhs: Time, rhs: Double) -> Time {
Time(seconds: lhs.seconds + rhs)
}

@inlinable
public static func + (lhs: Double, rhs: Time) -> Time {
rhs + lhs
}

@inlinable
public static func - (lhs: Time, rhs: Double) -> Time {
Time(seconds: lhs.seconds - rhs)
}

@inlinable
public static func - (lhs: Time, rhs: Time) -> Double {
lhs.seconds - rhs.seconds
}

@inlinable
public static func * (lhs: Time, rhs: Double) -> Time {
Time(seconds: lhs.seconds * rhs)
}

@inlinable
public static func / (lhs: Time, rhs: Double) -> Time {
return Time(seconds: lhs.seconds / rhs)
}

@inlinable
public static func += (lhs: inout Time, rhs: Double) {
lhs = lhs + rhs
}

@inlinable
public static func -= (lhs: inout Time, rhs: Double) {
lhs = lhs - rhs
}

@inlinable
public static func *= (lhs: inout Time, rhs: Double) {
lhs = lhs * rhs
}

@inlinable
public static func /= (lhs: inout Time, rhs: Double) {
lhs = lhs / rhs
}

@inlinable
public static func < (lhs: Time, rhs: Time) -> Bool {
lhs.seconds < rhs.seconds
}

@inlinable
public static func == (a: Time, b: Time) -> Bool {
a.seconds == b.seconds
}

@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(seconds)
}
}

@_spi(ForOpenSwiftUIOnly)
extension Time: Sendable {}
3 changes: 2 additions & 1 deletion Tests/OpenSwiftUITests/Core/Graph/GraphHostTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

@testable import OpenSwiftUI
import Testing
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore

struct GraphHostTests {
@Test
Expand All @@ -18,7 +19,7 @@ struct GraphHostTests {
graphHost.setTime(Time.infinity)
#expect(graphHost.data.time.seconds == Time.infinity.seconds)

let timeNow = Time.now
let timeNow = Time.systemUptime
graphHost.setTime(timeNow)
#expect(graphHost.data.time.seconds == timeNow.seconds)
#endif
Expand Down
Loading