Skip to content

Commit ea9e1b8

Browse files
committed
Wrap Damus in DamusApp for synchronization
1 parent 007ea6f commit ea9e1b8

File tree

5 files changed

+90
-23
lines changed

5 files changed

+90
-23
lines changed

Cargo.lock

+20-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ strum_macros = "0.26"
4545
bitflags = "2.5.0"
4646
uuid = { version = "1.10.0", features = ["v4"] }
4747
indexmap = "2.6.0"
48-
48+
futures = "0.3.31"
4949

5050
[target.'cfg(target_os = "macos")'.dependencies]
5151
security-framework = "2.11.0"

src/app.rs

+58
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub enum DamusState {
4646

4747
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
4848
pub struct Damus {
49+
reference: Option<Weak<Mutex<Damus>>>,
4950
state: DamusState,
5051
pub note_cache: NoteCache,
5152
pub pool: RelayPool,
@@ -720,6 +721,7 @@ impl Damus {
720721
}
721722

722723
Self {
724+
reference: None,
723725
pool,
724726
debug,
725727
unknown_ids: UnknownIds::default(),
@@ -740,6 +742,19 @@ impl Damus {
740742
}
741743
}
742744

745+
pub fn set_reference(&mut self, reference: Weak<Mutex<Damus>>) {
746+
self.reference = Some(reference);
747+
}
748+
749+
pub fn reference(&self) -> DamusRef {
750+
self.reference
751+
.as_ref()
752+
.expect("weak damus reference")
753+
.upgrade()
754+
.expect("strong damus reference")
755+
.clone()
756+
}
757+
743758
pub fn pool_mut(&mut self) -> &mut RelayPool {
744759
&mut self.pool
745760
}
@@ -803,6 +818,7 @@ impl Damus {
803818
let mut config = Config::new();
804819
config.set_ingester_threads(2);
805820
Self {
821+
reference: None,
806822
debug,
807823
unknown_ids: UnknownIds::default(),
808824
subscriptions: Subscriptions::default(),
@@ -1056,3 +1072,45 @@ impl eframe::App for Damus {
10561072
render_damus(self, ctx);
10571073
}
10581074
}
1075+
1076+
use std::sync::{Arc, Mutex, Weak};
1077+
1078+
pub type DamusRef = Arc<Mutex<Damus>>;
1079+
1080+
pub fn with_mut_damus<F, T>(damusref: &DamusRef, mut f: F) -> T
1081+
where
1082+
F: FnMut(&mut Damus) -> T,
1083+
{
1084+
let mut damus = damusref.as_ref().lock().unwrap();
1085+
f(&mut damus)
1086+
}
1087+
1088+
/// A wrapper so access to Damus can be synchronized
1089+
pub struct DamusApp {
1090+
damus: DamusRef,
1091+
}
1092+
1093+
impl DamusApp {
1094+
pub fn new(damus: DamusRef) -> Self {
1095+
let weak_damus = Arc::downgrade(&damus);
1096+
damus.lock().unwrap().set_reference(weak_damus);
1097+
Self { damus }
1098+
}
1099+
1100+
pub fn with_mut_damus<F, T>(&mut self, f: F) -> T
1101+
where
1102+
F: FnMut(&mut Damus) -> T,
1103+
{
1104+
with_mut_damus(&self.damus, f)
1105+
}
1106+
}
1107+
1108+
impl eframe::App for DamusApp {
1109+
fn save(&mut self, storage: &mut dyn eframe::Storage) {
1110+
self.with_mut_damus(|damus| damus.save(storage))
1111+
}
1112+
1113+
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
1114+
self.with_mut_damus(|damus| damus.update(ctx, frame));
1115+
}
1116+
}

src/bin/notedeck.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
33
use notedeck::app_creation::generate_native_options;
4-
use notedeck::Damus;
4+
use notedeck::{Damus, DamusApp};
5+
6+
use std::sync::{Arc, Mutex};
57

68
// Entry point for wasm
79
//#[cfg(target_arch = "wasm32")]
@@ -17,7 +19,13 @@ async fn main() {
1719
let _res = eframe::run_native(
1820
"Damus NoteDeck",
1921
generate_native_options(),
20-
Box::new(|cc| Ok(Box::new(Damus::new(cc, ".", std::env::args().collect())))),
22+
Box::new(|cc| {
23+
Ok(Box::new(DamusApp::new(Arc::new(Mutex::new(Damus::new(
24+
cc,
25+
".",
26+
std::env::args().collect(),
27+
))))))
28+
}),
2129
);
2230
}
2331

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod view_state;
4747
mod test_utils;
4848
mod linux_key_storage;
4949

50-
pub use app::Damus;
50+
pub use app::{with_mut_damus, Damus, DamusApp, DamusRef};
5151
pub use error::Error;
5252
pub use profile::DisplayName;
5353

0 commit comments

Comments
 (0)