mirror of
https://github.com/yattee/yattee.git
synced 2024-11-14 18:18:22 +00:00
41 lines
844 B
Swift
41 lines
844 B
Swift
|
import Foundation
|
||
|
|
||
|
final class Throttle {
|
||
|
let interval: TimeInterval
|
||
|
private(set) var lastExecutedAt: Date?
|
||
|
|
||
|
private let syncQueue = DispatchQueue(label: "net.yatee.app.throttle")
|
||
|
|
||
|
init(interval: TimeInterval) {
|
||
|
self.interval = interval
|
||
|
}
|
||
|
|
||
|
@discardableResult func execute(_ action: () -> Void) -> Bool {
|
||
|
let executed = syncQueue.sync { () -> Bool in
|
||
|
let now = Date()
|
||
|
|
||
|
let timeInterval = now.timeIntervalSince(lastExecutedAt ?? .distantPast)
|
||
|
|
||
|
if timeInterval > interval {
|
||
|
lastExecutedAt = now
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if executed {
|
||
|
action()
|
||
|
}
|
||
|
|
||
|
return executed
|
||
|
}
|
||
|
|
||
|
func reset() {
|
||
|
syncQueue.sync {
|
||
|
lastExecutedAt = nil
|
||
|
}
|
||
|
}
|
||
|
}
|