Created
February 9, 2020 20:07
-
-
Save soxjke/b3b73d00e85e3f5a8abee580d26435bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Debouncer<T: Equatable> { | |
private(set) var value: T? | |
private var valueTimestamp: Date = Date() | |
private var interval: TimeInterval | |
private var queue: DispatchQueue | |
private var callbacks: [(T) -> ()] = [] | |
private var debounceWorkItem: DispatchWorkItem = DispatchWorkItem {} | |
public init(_ interval: TimeInterval, | |
on queue: DispatchQueue = .main) { | |
self.interval = interval | |
self.queue = queue | |
} | |
public func receive(_ value: T) { | |
self.value = value | |
dispatchDebounce() | |
} | |
public func on(throttled: @escaping (T) -> ()) { | |
self.callbacks.append(throttled) | |
} | |
private func dispatchDebounce() { | |
self.valueTimestamp = Date() | |
self.debounceWorkItem.cancel() | |
self.debounceWorkItem = DispatchWorkItem { [weak self] in | |
self?.onDebounce() | |
} | |
queue.asyncAfter(deadline: .now() + interval, execute: debounceWorkItem) | |
} | |
private func onDebounce() { | |
if (Date().timeIntervalSince(self.valueTimestamp) > interval) { | |
sendValue() | |
} | |
} | |
private func sendValue() { | |
if let value = self.value { callbacks.forEach { $0(value) } } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment