notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 2ce2d4cc703c16824e89381e19577cca367e7d87
parent c246b9d92f20b8042b1394b9aee488b4c79c3624
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 15 Feb 2024 13:03:46 -0800

notecache: add initial in-memory notecache

This is useful for things like relative time strings and other
transient note cache state

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Msrc/app.rs | 9+++++++++
Msrc/lib.rs | 1+
Asrc/notecache.rs | 17+++++++++++++++++
3 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/src/app.rs b/src/app.rs @@ -3,6 +3,7 @@ use crate::error::Error; use crate::fonts::setup_gossip_fonts; use crate::frame_history::FrameHistory; use crate::images::fetch_img; +use crate::notecache::NoteCache; use crate::timeline; use crate::ui::padding; use crate::Result; @@ -97,6 +98,7 @@ pub struct Damus { compose: String, initial_filter: Vec<enostr::Filter>, + note_cache: HashMap<NoteKey, NoteCache>, pool: RelayPool, timelines: Vec<Timeline>, @@ -449,6 +451,7 @@ impl Damus { state: DamusState::Initializing, pool: RelayPool::new(), img_cache: HashMap::new(), + note_cache: HashMap::new(), initial_filter, n_panels: 1, timelines: vec![Timeline::new()], @@ -457,6 +460,12 @@ impl Damus { frame_history: FrameHistory::default(), } } + + pub fn get_note_cache_mut(&mut self, note_ref: &NoteRef) -> &mut NoteCache { + self.note_cache + .entry(note_ref.key) + .or_insert_with(|| NoteCache::new(note_ref.created_at)) + } } fn render_pfp(ui: &mut egui::Ui, img_cache: &mut ImageCache, url: &str) { diff --git a/src/lib.rs b/src/lib.rs @@ -11,6 +11,7 @@ mod filter; mod ui; mod timecache; mod time; +mod notecache; mod frame_history; mod timeline; diff --git a/src/notecache.rs b/src/notecache.rs @@ -0,0 +1,17 @@ +use crate::time::time_ago_since; +use crate::timecache::TimeCached; +use std::time::Duration; + +pub struct NoteCache { + reltime: TimeCached<String>, +} + +impl NoteCache { + pub fn new(created_at: u64) -> Self { + let reltime = TimeCached::new( + Duration::from_secs(1), + Box::new(move || time_ago_since(created_at)), + ); + NoteCache { reltime } + } +}