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>
This commit is contained in:
William Casarin
2024-02-15 13:03:46 -08:00
parent c246b9d92f
commit 2ce2d4cc70
3 changed files with 27 additions and 0 deletions

View File

@@ -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) {

View File

@@ -11,6 +11,7 @@ mod filter;
mod ui;
mod timecache;
mod time;
mod notecache;
mod frame_history;
mod timeline;

17
src/notecache.rs Normal file
View File

@@ -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 }
}
}