Skip to content

Instantly share code, notes, and snippets.

@lgastler
Created January 28, 2023 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgastler/bed6b18ef8b3a80e498bf59c9d0149c6 to your computer and use it in GitHub Desktop.
Save lgastler/bed6b18ef8b3a80e498bf59c9d0149c6 to your computer and use it in GitHub Desktop.
Debouncer
class Debouncer<T>: ObservableObject {
@Published var input: T
@Published var output: T
private var debounce: AnyCancellable?
init(initialValue: T, delay: Double = 1) {
self.input = initialValue
self.output = initialValue
debounce = $input.debounce(for: .seconds(delay), scheduler: DispatchQueue.main)
.sink { [weak self] in
self?.output = $0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment