mirror of
git://jb55.com/damus
synced 2024-09-18 19:23:49 +00:00
search: debounce when searching
so we don't spawn tons of searching tasks for no reason
This commit is contained in:
parent
e40d5b3e83
commit
f6b59b3f5d
@ -13,39 +13,47 @@ struct PullDownSearchView: View {
|
|||||||
@State private var search_text = ""
|
@State private var search_text = ""
|
||||||
@State private var results: [NostrEvent] = []
|
@State private var results: [NostrEvent] = []
|
||||||
@State private var is_active: Bool = false
|
@State private var is_active: Bool = false
|
||||||
|
let debouncer: Debouncer = Debouncer(interval: 0.25)
|
||||||
let state: DamusState
|
let state: DamusState
|
||||||
let on_cancel: () -> Void
|
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 {
|
var body: some View {
|
||||||
VStack(alignment: .leading) {
|
VStack(alignment: .leading) {
|
||||||
HStack {
|
HStack {
|
||||||
TextField("Search", text: $search_text)
|
TextField("Search", text: $search_text)
|
||||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||||
.onChange(of: search_text) { newValue in
|
.onChange(of: search_text) { query in
|
||||||
Task.detached {
|
debouncer.debounce {
|
||||||
let note_keys = state.ndb.text_search(query: newValue, limit: 16)
|
Task.detached {
|
||||||
var res = [NostrEvent]()
|
do_search(query: query)
|
||||||
// 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_
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user