Repository Public License
1import Foundation23/// Transfer speed and remaining time from a sliding window of progress samples. Time is always passed in.4public struct DownloadSpeedMeter: Sendable, Equatable {5 /// Length of the window the speed is averaged over.6 public var window: TimeInterval7 /// Shortest span of samples that yields a speed: below it one burst would give an absurd number.8 public var minimumSpan: TimeInterval9 private var samples: [(time: Date, bytes: Int64)] = []1011 public init(window: TimeInterval = 5, minimumSpan: TimeInterval = 0.5) {12 self.window = max(window, 0.1)13 self.minimumSpan = min(max(minimumSpan, 0.01), self.window)14 }1516 public static func == (lhs: Self, rhs: Self) -> Bool {17 lhs.window == rhs.window && lhs.minimumSpan == rhs.minimumSpan && lhs.samples.count == rhs.samples.count18 && zip(lhs.samples, rhs.samples).allSatisfy { $0.time == $1.time && $0.bytes == $1.bytes }19 }2021 /// Records the total number of bytes received so far. A total going backwards (the transfer restarted) or22 /// a clock going backwards starts a new measurement.23 public mutating func record(bytes: Int64, at time: Date) {24 if let last = samples.last, bytes < last.bytes || time < last.time { samples.removeAll() }25 if let last = samples.last, last.time == time {26 samples[samples.count - 1].bytes = bytes27 } else {28 samples.append((time, bytes))29 }30 // Keep one sample at or before the start of the window as the baseline.31 let start = time.addingTimeInterval(-window)32 while samples.count > 2, samples[1].time <= start { samples.removeFirst() }33 }3435 public mutating func reset() { samples.removeAll() }3637 /// Average speed over the window, in bytes per second. `nil` until enough time has been observed; `0`38 /// when nothing arrived for a whole window (stalled).39 public func bytesPerSecond(at now: Date) -> Double? {40 guard let first = samples.first, let last = samples.last else { return nil }41 if now.timeIntervalSince(last.time) >= window { return 0 }42 let span = last.time.timeIntervalSince(first.time)43 guard span >= minimumSpan else { return nil }44 return Double(last.bytes - first.bytes) / span45 }4647 /// Seconds left at the current speed. `nil` when the total is unknown, the speed is unknown or zero.48 public func estimatedSecondsRemaining(totalBytes: Int64?, at now: Date) -> TimeInterval? {49 guard let totalBytes, let last = samples.last, let speed = bytesPerSecond(at: now), speed > 0 else { return nil }50 let remaining = totalBytes - last.bytes51 guard remaining > 0 else { return 0 }52 return Double(remaining) / speed53 }54}5556/// Lets an update through at most once per interval.57public struct ProgressThrottle: Sendable, Equatable {58 public var interval: TimeInterval59 private var last: Date?6061 public init(interval: TimeInterval) { self.interval = max(interval, 0) }6263 /// `true` when an update may be emitted now; the call then counts as the emission. `force` always passes64 /// (state changes are never throttled). A clock that went backwards also passes.65 public mutating func shouldEmit(at now: Date, force: Bool = false) -> Bool {66 if !force, let last, now >= last, now.timeIntervalSince(last) < interval { return false }67 last = now68 return true69 }70}71