mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-29 16:31:18 +00:00
Stop recomputing person feed every frame (when looking at person feed)
This commit is contained in:
parent
2b79ce9a35
commit
cf530a8897
58
src/feed.rs
58
src/feed.rs
@ -1,7 +1,7 @@
|
||||
use crate::comms::{ToMinionMessage, ToMinionPayload, ToOverlordMessage};
|
||||
use crate::error::Error;
|
||||
use crate::globals::GLOBALS;
|
||||
use nostr_types::{Event, EventKind, Id, PublicKeyHex, RelayUrl, Unixtime};
|
||||
use nostr_types::{EventKind, Id, PublicKeyHex, RelayUrl, Unixtime};
|
||||
use parking_lot::RwLock;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
@ -31,6 +31,7 @@ pub struct Feed {
|
||||
|
||||
followed_feed: RwLock<Vec<Id>>,
|
||||
inbox_feed: RwLock<Vec<Id>>,
|
||||
person_feed: RwLock<Vec<Id>>,
|
||||
|
||||
// We only recompute the feed at specified intervals (or when they switch)
|
||||
interval_ms: RwLock<u32>,
|
||||
@ -47,6 +48,7 @@ impl Feed {
|
||||
current_feed_kind: RwLock::new(FeedKind::Followed(false)),
|
||||
followed_feed: RwLock::new(Vec::new()),
|
||||
inbox_feed: RwLock::new(Vec::new()),
|
||||
person_feed: RwLock::new(Vec::new()),
|
||||
interval_ms: RwLock::new(1000), // Every second, until we load from settings
|
||||
last_computed: RwLock::new(None),
|
||||
thread_parent: RwLock::new(None),
|
||||
@ -99,6 +101,9 @@ impl Feed {
|
||||
// Overlord will climb it, and recompute will climb it
|
||||
*self.thread_parent.write() = Some(id);
|
||||
|
||||
// Recompute as they switch
|
||||
self.sync_recompute();
|
||||
|
||||
let _ = GLOBALS.to_minions.send(ToMinionMessage {
|
||||
target: "all".to_string(),
|
||||
payload: ToMinionPayload::UnsubscribePersonFeed,
|
||||
@ -110,16 +115,20 @@ impl Feed {
|
||||
}
|
||||
|
||||
pub fn set_feed_to_person(&self, pubkey: PublicKeyHex) {
|
||||
*self.current_feed_kind.write() = FeedKind::Person(pubkey.clone());
|
||||
*self.thread_parent.write() = None;
|
||||
|
||||
// Recompute as they switch
|
||||
self.sync_recompute();
|
||||
|
||||
let _ = GLOBALS.to_minions.send(ToMinionMessage {
|
||||
target: "all".to_string(),
|
||||
payload: ToMinionPayload::UnsubscribeThreadFeed,
|
||||
});
|
||||
let _ = GLOBALS.to_minions.send(ToMinionMessage {
|
||||
target: "all".to_string(),
|
||||
payload: ToMinionPayload::SubscribePersonFeed(pubkey.clone()),
|
||||
payload: ToMinionPayload::SubscribePersonFeed(pubkey),
|
||||
});
|
||||
*self.current_feed_kind.write() = FeedKind::Person(pubkey);
|
||||
*self.thread_parent.write() = None;
|
||||
}
|
||||
|
||||
pub fn get_feed_kind(&self) -> FeedKind {
|
||||
@ -136,26 +145,9 @@ impl Feed {
|
||||
self.inbox_feed.read().clone()
|
||||
}
|
||||
|
||||
pub fn get_person_feed(&self, person: PublicKeyHex) -> Vec<Id> {
|
||||
let enable_reposts = GLOBALS.settings.read().reposts;
|
||||
|
||||
pub fn get_person_feed(&self) -> Vec<Id> {
|
||||
self.sync_maybe_periodic_recompute();
|
||||
let mut events: Vec<Event> = GLOBALS
|
||||
.events
|
||||
.iter()
|
||||
.map(|r| r.value().to_owned())
|
||||
.filter(|e| {
|
||||
e.kind == EventKind::TextNote
|
||||
|| e.kind == EventKind::EncryptedDirectMessage
|
||||
|| (enable_reposts && (e.kind == EventKind::Repost))
|
||||
})
|
||||
.filter(|e| e.pubkey.as_hex_string() == person.as_str())
|
||||
.filter(|e| !GLOBALS.dismissed.blocking_read().contains(&e.id))
|
||||
.collect();
|
||||
|
||||
events.sort_unstable_by(|a, b| b.created_at.cmp(&a.created_at));
|
||||
|
||||
events.iter().map(|e| e.id).collect()
|
||||
self.person_feed.read().clone()
|
||||
}
|
||||
|
||||
pub fn get_thread_parent(&self) -> Option<Id> {
|
||||
@ -332,7 +324,25 @@ impl Feed {
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
FeedKind::Person(person_pubkey) => {
|
||||
let mut events: Vec<(Unixtime, Id)> = GLOBALS
|
||||
.events
|
||||
.iter()
|
||||
.filter(|e| {
|
||||
e.value().kind == EventKind::TextNote
|
||||
|| e.value().kind == EventKind::EncryptedDirectMessage
|
||||
|| (reposts && (e.value().kind == EventKind::Repost))
|
||||
})
|
||||
.filter(|e| e.value().pubkey.as_hex_string() == person_pubkey.as_str())
|
||||
.filter(|e| !dismissed.contains(&e.value().id)) // not dismissed
|
||||
.map(|e| (e.value().created_at, e.value().id))
|
||||
.collect();
|
||||
|
||||
// Sort
|
||||
events.sort_unstable_by(|a, b| b.0.cmp(&a.0));
|
||||
|
||||
*self.person_feed.write() = events.iter().map(|e| e.1).collect();
|
||||
}
|
||||
}
|
||||
|
||||
self.recompute_lock.store(false, Ordering::Relaxed);
|
||||
|
@ -137,7 +137,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
|
||||
}
|
||||
}
|
||||
FeedKind::Person(pubkeyhex) => {
|
||||
let feed = GLOBALS.feed.get_person_feed(pubkeyhex.clone());
|
||||
let feed = GLOBALS.feed.get_person_feed();
|
||||
render_a_feed(app, ctx, frame, ui, feed, false, pubkeyhex.as_str());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user