39 lines
960 B
Swift
39 lines
960 B
Swift
//
|
|
// SwiftDataRefreshThrottler.swift
|
|
// Bedrock
|
|
//
|
|
// Generic minimum-interval gate for refresh operations.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public final class SwiftDataRefreshThrottler {
|
|
/// Timestamp of the last allowed refresh.
|
|
public private(set) var lastRefreshDate: Date?
|
|
|
|
public init(lastRefreshDate: Date? = nil) {
|
|
self.lastRefreshDate = lastRefreshDate
|
|
}
|
|
|
|
/// Returns true when a refresh should run now.
|
|
/// If true, records `now` as the latest refresh date.
|
|
@discardableResult
|
|
public func shouldRefresh(
|
|
now: Date = Date(),
|
|
minimumInterval: TimeInterval
|
|
) -> Bool {
|
|
if let lastRefreshDate,
|
|
now.timeIntervalSince(lastRefreshDate) < minimumInterval {
|
|
return false
|
|
}
|
|
|
|
lastRefreshDate = now
|
|
return true
|
|
}
|
|
|
|
/// Clears refresh history so the next check can pass immediately.
|
|
public func reset() {
|
|
lastRefreshDate = nil
|
|
}
|
|
}
|