Add to playlist from video player, state fixes

This commit is contained in:
Arkadiusz Fal
2021-10-25 23:29:06 +02:00
parent 47ad6a4410
commit b50d915d8e
10 changed files with 129 additions and 27 deletions

40
Shared/Throttle.swift Normal file
View File

@@ -0,0 +1,40 @@
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
}
}
}