Skip to content

Instantly share code, notes, and snippets.

@bradfol
Last active September 28, 2020 14:52
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bradfol/541c010a6540404eca0f4a5da009c761 to your computer and use it in GitHub Desktop.
Save bradfol/541c010a6540404eca0f4a5da009c761 to your computer and use it in GitHub Desktop.
import Foundation
class Debouncer {
/**
Create a new Debouncer instance with the provided time interval.
- parameter timeInterval: The time interval of the debounce window.
*/
init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
}
typealias Handler = () -> Void
/// Closure to be debounced.
/// Perform the work you would like to be debounced in this handler.
var handler: Handler?
/// Time interval of the debounce window.
private let timeInterval: TimeInterval
private var timer: Timer?
/// Indicate that the handler should be invoked.
/// Begins the debounce window with the duration of the time interval parameter.
func renewInterval() {
// Invalidate existing timer if there is one
timer?.invalidate()
// Begin a new timer from now
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false, block: { [weak self] timer in
self?.handleTimer(timer)
})
}
private func handleTimer(_ timer: Timer) {
guard timer.isValid else {
return
}
handler?()
handler = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment