p2p-swift / Sources / IrdP2PSignaling / Clock.swift
101 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import Foundation

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

public struct SystemClock: P2PClock {
    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)
    }
}

extension Duration {
    var milliseconds: Double { Double(components.seconds) * 1000 + Double(components.attoseconds) / 1e15 }
    var totalSeconds: Double { milliseconds / 1000 }
}

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