Skip to content

Commit

Permalink
Convert stuff to Send + Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ksedgwic committed Oct 17, 2024
1 parent 50c6dc2 commit 2b032cc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/notecache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::time::time_ago_since;
use crate::timecache::TimeCached;
use nostrdb::{Note, NoteKey, NoteReply, NoteReplyBuf};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

#[derive(Default)]
Expand Down Expand Up @@ -42,7 +43,7 @@ impl CachedNote {
let created_at = note.created_at();
let reltime = TimeCached::new(
Duration::from_secs(1),
Box::new(move || time_ago_since(created_at)) as Box<dyn Fn() -> String + Send>,
Arc::new(move || time_ago_since(created_at)) as Arc<dyn Fn() -> String + Send + Sync>,
);

let reply = NoteReply::new(note.tags()).to_owned();
Expand Down
6 changes: 3 additions & 3 deletions src/timecache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ pub struct TimeCached<T> {
last_update: Instant,
expires_in: Duration,
value: Option<T>,
refresh: Arc<dyn Fn() -> T + Send + 'static>,
refresh: Arc<dyn Fn() -> T + Send + Sync + 'static>, // Use Send + Sync
}

impl<T> TimeCached<T> {
pub fn new(expires_in: Duration, refresh: impl Fn() -> T + Send + 'static) -> Self {
pub fn new(expires_in: Duration, refresh: Arc<dyn Fn() -> T + Send + Sync>) -> Self {
TimeCached {
last_update: Instant::now(),
expires_in,
value: None,
refresh: Arc::new(refresh),
refresh,
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use std::sync::atomic::{AtomicU32, Ordering};

use egui_virtual_list::VirtualList;
use nostrdb::{Ndb, Note, Subscription, Transaction};
use std::cell::RefCell;
use std::hash::Hash;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

use tracing::{debug, error};

Expand Down Expand Up @@ -84,7 +83,7 @@ pub struct TimelineTab {
pub notes: Vec<NoteRef>,
pub selection: i32,
pub filter: ViewFilter,
pub list: Rc<RefCell<VirtualList>>,
pub list: Arc<Mutex<VirtualList>>,
}

impl TimelineTab {
Expand All @@ -97,7 +96,7 @@ impl TimelineTab {
let mut list = VirtualList::new();
list.hide_on_resize(None);
list.over_scan(1000.0);
let list = Rc::new(RefCell::new(list));
let list = Arc::new(Mutex::new(list));
let notes: Vec<NoteRef> = Vec::with_capacity(cap);

TimelineTab {
Expand All @@ -120,7 +119,7 @@ impl TimelineTab {

// TODO: technically items could have been added inbetween
if new_items > 0 {
let mut list = self.list.borrow_mut();
let mut list = self.list.lock().unwrap();

match merge_kind {
// TODO: update egui_virtual_list to support spliced inserts
Expand Down
3 changes: 2 additions & 1 deletion src/ui/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ impl<'a> TimelineTabView<'a> {
self.tab
.list
.clone()
.borrow_mut()
.lock()
.unwrap()
.ui_custom_layout(ui, len, |ui, start_index| {
ui.spacing_mut().item_spacing.y = 0.0;
ui.spacing_mut().item_spacing.x = 4.0;
Expand Down

0 comments on commit 2b032cc

Please sign in to comment.