macos: debounce search requests with length less than 3

pull/9709/head
Mitchell Hashimoto 2025-11-26 06:59:16 -08:00
parent 339abf97f7
commit f7b14a0142
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
1 changed files with 22 additions and 6 deletions

View File

@ -69,9 +69,25 @@ extension Ghostty {
@Published var searchState: SearchState? = nil {
didSet {
if let searchState {
searchNeedleCancellable = searchState.$needle.removeDuplicates().sink { [weak self] needle in
// I'm not a Combine expert so if there is a better way to do this I'm
// all ears. What we're doing here is grabbing the latest needle. If the
// needle is less than 3 chars, we debounce it for a few hundred ms to
// avoid kicking off expensive searches.
searchNeedleCancellable = searchState.$needle
.removeDuplicates()
.filter { $0.count > 0 }
.map { needle -> AnyPublisher<String, Never> in
if needle.count >= 3 {
return Just(needle).eraseToAnyPublisher()
} else {
return Just(needle)
.delay(for: .milliseconds(300), scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
}
}
.switchToLatest()
.sink { [weak self] needle in
guard let surface = self?.surface else { return }
guard needle.count > 0 else { return }
let action = "search:\(needle)"
ghostty_surface_binding_action(surface, action, UInt(action.count))
}