search: debounce when searching
so we don't spawn tons of searching tasks for no reason
This commit is contained in:
@@ -13,20 +13,16 @@ 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
|
||||||
|
|
||||||
var body: some View {
|
func do_search(query: String) {
|
||||||
VStack(alignment: .leading) {
|
let note_keys = state.ndb.text_search(query: query, limit: 16)
|
||||||
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]()
|
var res = [NostrEvent]()
|
||||||
// TODO: fix duplicate results from search
|
// TODO: fix duplicate results from search
|
||||||
var keyset = Set<NoteKey>()
|
var keyset = Set<NoteKey>()
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let txn = NdbTxn(ndb: state.ndb)
|
let txn = NdbTxn(ndb: state.ndb)
|
||||||
for note_key in note_keys {
|
for note_key in note_keys {
|
||||||
@@ -48,6 +44,18 @@ struct PullDownSearchView: View {
|
|||||||
results = res_
|
results = res_
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack(alignment: .leading) {
|
||||||
|
HStack {
|
||||||
|
TextField("Search", text: $search_text)
|
||||||
|
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||||
|
.onChange(of: search_text) { query in
|
||||||
|
debouncer.debounce {
|
||||||
|
Task.detached {
|
||||||
|
do_search(query: query)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
is_active = true
|
is_active = true
|
||||||
|
|||||||
Reference in New Issue
Block a user