Thread Caching

Changelog-Added: Threads now load instantly and are cached
This commit is contained in:
William Casarin
2023-03-04 14:33:01 -05:00
parent b58baca227
commit d0a6c2e2e4
20 changed files with 433 additions and 539 deletions

View File

@@ -81,7 +81,7 @@ struct ContentView: View {
@State var event: NostrEvent? = nil
@State var active_profile: String? = nil
@State var active_search: NostrFilter? = nil
@State var active_event_id: String? = nil
@State var active_event: NostrEvent = test_event
@State var profile_open: Bool = false
@State var thread_open: Bool = false
@State var search_open: Bool = false
@@ -176,7 +176,8 @@ struct ContentView: View {
NavigationLink(destination: MaybeProfileView, isActive: $profile_open) {
EmptyView()
}
NavigationLink(destination: MaybeThreadView, isActive: $thread_open) {
let thread = ThreadModel(event: active_event, damus_state: damus_state!)
NavigationLink(destination: ThreadView(state: damus_state!, thread: thread), isActive: $thread_open) {
EmptyView()
}
NavigationLink(destination: MaybeSearchView, isActive: $search_open) {
@@ -223,16 +224,6 @@ struct ContentView: View {
}
}
var MaybeThreadView: some View {
Group {
if let evid = self.active_event_id {
BuildThreadV2View(damus: damus_state!, event_id: evid)
} else {
EmptyView()
}
}
}
var MaybeProfileView: some View {
Group {
if let pk = self.active_profile {
@@ -352,7 +343,11 @@ struct ContentView: View {
active_profile = ref.ref_id
profile_open = true
} else if ref.key == "e" {
active_event_id = ref.ref_id
find_event(state: damus_state!, evid: ref.ref_id, find_from: nil) { ev in
if let ev {
active_event = ev
}
}
thread_open = true
}
case .filter(let filt):
@@ -766,3 +761,45 @@ func setup_notifications() {
}
}
func find_event(state: DamusState, evid: String, find_from: [String]?, callback: @escaping (NostrEvent?) -> ()) {
if let ev = state.events.lookup(evid) {
callback(ev)
return
}
let subid = UUID().description
var has_event = false
var filter = NostrFilter.filter_ids([ evid ])
filter.limit = 1
var attempts = 0
state.pool.subscribe_to(sub_id: subid, filters: [filter], to: find_from) { relay_id, res in
guard case .nostr_event(let ev) = res else {
return
}
guard ev.subid == subid else {
return
}
switch ev {
case .event(_, let ev):
has_event = true
callback(ev)
state.pool.unsubscribe(sub_id: subid)
case .eose:
if !has_event {
attempts += 1
if attempts == state.pool.descriptors.count / 2 {
callback(nil)
}
state.pool.unsubscribe(sub_id: subid, to: [relay_id])
}
case .notice(_):
break
}
}
}