damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit f6b59b3f5dc9d9fcbdb857f92a6198f4855527bd
parent e40d5b3e83cb5dd410fe01d3720c32bd651fc064
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  4 Dec 2023 12:26:20 -0800

search: debounce when searching

so we don't spawn tons of searching tasks for no reason

Diffstat:
Mdamus/Views/Search/PullDownSearch.swift | 58+++++++++++++++++++++++++++++++++-------------------------
1 file changed, 33 insertions(+), 25 deletions(-)

diff --git a/damus/Views/Search/PullDownSearch.swift b/damus/Views/Search/PullDownSearch.swift @@ -13,39 +13,47 @@ struct PullDownSearchView: View { @State private var search_text = "" @State private var results: [NostrEvent] = [] @State private var is_active: Bool = false + let debouncer: Debouncer = Debouncer(interval: 0.25) let state: DamusState let on_cancel: () -> Void + + func do_search(query: String) { + let note_keys = state.ndb.text_search(query: query, limit: 16) + var res = [NostrEvent]() + // TODO: fix duplicate results from search + var keyset = Set<NoteKey>() + + do { + let txn = NdbTxn(ndb: state.ndb) + for note_key in note_keys { + guard let note = state.ndb.lookup_note_by_key_with_txn(note_key, txn: txn) else { + continue + } + + if !keyset.contains(note_key) { + let owned_note = note.to_owned() + res.append(owned_note) + keyset.insert(note_key) + } + } + } + + let res_ = res + + Task { @MainActor [res_] in + results = res_ + } + } var body: some View { VStack(alignment: .leading) { HStack { TextField("Search", text: $search_text) .textFieldStyle(RoundedBorderTextFieldStyle()) - .onChange(of: search_text) { newValue in - Task.detached { - let note_keys = state.ndb.text_search(query: newValue, limit: 16) - var res = [NostrEvent]() - // TODO: fix duplicate results from search - var keyset = Set<NoteKey>() - do { - let txn = NdbTxn(ndb: state.ndb) - for note_key in note_keys { - guard let note = state.ndb.lookup_note_by_key_with_txn(note_key, txn: txn) else { - continue - } - - if !keyset.contains(note_key) { - let owned_note = note.to_owned() - res.append(owned_note) - keyset.insert(note_key) - } - } - } - - let res_ = res - - Task { @MainActor [res_] in - results = res_ + .onChange(of: search_text) { query in + debouncer.debounce { + Task.detached { + do_search(query: query) } } }