api-swift / Sources / IrdAPI / Core / Clock.swift
77 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import Foundation

/// Monotonic time and sleeping, injectable so poll loops are testable.
public protocol IrdClock: Sendable {
    /// Monotonic elapsed time since an arbitrary origin.
    var now: Duration { get }
    func sleep(for duration: Duration) async throws
}

public struct SystemClock: IrdClock {
    private let origin = ContinuousClock.now
    public init() {}
    public var now: Duration { ContinuousClock.now - origin }
    public func sleep(for duration: Duration) async throws {
        try await Task.sleep(for: duration, clock: .continuous)
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/api-swift.git