Skip to content

Commit

Permalink
notecache: add initial in-memory notecache
Browse files Browse the repository at this point in the history
This is useful for things like relative time strings and other
transient note cache state

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 15, 2024
1 parent c246b9d commit 2ce2d4c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +98,7 @@ pub struct Damus {
compose: String,

Check failure on line 98 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

field `compose` is never read

Check warning on line 98 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

field `compose` is never read
initial_filter: Vec<enostr::Filter>,

note_cache: HashMap<NoteKey, NoteCache>,
pool: RelayPool,

timelines: Vec<Timeline>,
Expand Down Expand Up @@ -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()],
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod filter;
mod ui;
mod timecache;
mod time;
mod notecache;
mod frame_history;
mod timeline;

Expand Down
17 changes: 17 additions & 0 deletions src/notecache.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
}

0 comments on commit 2ce2d4c

Please sign in to comment.