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,12 +69,28 @@ extension Ghostty {
@Published var searchState: SearchState? = nil { @Published var searchState: SearchState? = nil {
didSet { didSet {
if let searchState { 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
guard let surface = self?.surface else { return } // all ears. What we're doing here is grabbing the latest needle. If the
guard needle.count > 0 else { return } // needle is less than 3 chars, we debounce it for a few hundred ms to
let action = "search:\(needle)" // avoid kicking off expensive searches.
ghostty_surface_binding_action(surface, action, UInt(action.count)) 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 { } else if oldValue != nil {
searchNeedleCancellable = nil searchNeedleCancellable = nil
guard let surface = self.surface else { return } guard let surface = self.surface else { return }