macos: debounce search requests with length less than 3

This commit is contained in:
Mitchell Hashimoto
2025-11-26 06:59:16 -08:00
parent 339abf97f7
commit f7b14a0142

View File

@@ -69,12 +69,28 @@ extension Ghostty {
@Published var searchState: SearchState? = nil {
didSet {
if let searchState {
searchNeedleCancellable = searchState.$needle.removeDuplicates().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))
}
// 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 }
let action = "search:\(needle)"
ghostty_surface_binding_action(surface, action, UInt(action.count))
}
} else if oldValue != nil {
searchNeedleCancellable = nil
guard let surface = self.surface else { return }