Created
January 28, 2023 16:33
-
-
Save lgastler/bed6b18ef8b3a80e498bf59c9d0149c6 to your computer and use it in GitHub Desktop.
Debouncer
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
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