Skip to content

Commit

Permalink
Wrap Damus in DamusApp for synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
ksedgwic committed Oct 17, 2024
1 parent 2b032cc commit 2a59106
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 23 deletions.
39 changes: 20 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ strum_macros = "0.26"
bitflags = "2.5.0"
uuid = { version = "1.10.0", features = ["v4"] }
indexmap = "2.6.0"

futures = "0.3.31"

[target.'cfg(target_os = "macos")'.dependencies]
security-framework = "2.11.0"
Expand Down
58 changes: 58 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub enum DamusState {

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
pub struct Damus {
reference: Option<Weak<Mutex<Damus>>>,
state: DamusState,
pub note_cache: NoteCache,
pub pool: RelayPool,
Expand Down Expand Up @@ -720,6 +721,7 @@ impl Damus {
}

Self {
reference: None,
pool,
debug,
unknown_ids: UnknownIds::default(),
Expand All @@ -740,6 +742,19 @@ impl Damus {
}
}

pub fn set_reference(&mut self, reference: Weak<Mutex<Damus>>) {
self.reference = Some(reference);
}

pub fn reference(&self) -> DamusRef {
self.reference
.as_ref()
.expect("weak damus reference")
.upgrade()
.expect("strong damus reference")
.clone()
}

pub fn pool_mut(&mut self) -> &mut RelayPool {
&mut self.pool
}
Expand Down Expand Up @@ -803,6 +818,7 @@ impl Damus {
let mut config = Config::new();
config.set_ingester_threads(2);
Self {
reference: None,
debug,
unknown_ids: UnknownIds::default(),
subscriptions: Subscriptions::default(),
Expand Down Expand Up @@ -1056,3 +1072,45 @@ impl eframe::App for Damus {
render_damus(self, ctx);
}
}

use std::sync::{Arc, Mutex, Weak};

pub type DamusRef = Arc<Mutex<Damus>>;

pub fn with_mut_damus<F: Sized, T>(damusref: &DamusRef, mut f: F) -> T
where
F: FnMut(&mut Damus) -> T,
{
let mut damus = damusref.as_ref().lock().unwrap();
f(&mut damus)
}

/// A wrapper so access to Damus can be synchronized
pub struct DamusApp {
damus: DamusRef,
}

impl DamusApp {
pub fn new(damus: DamusRef) -> Self {
let weak_damus = Arc::downgrade(&damus);
damus.lock().unwrap().set_reference(weak_damus);
Self { damus }
}

pub fn with_mut_damus<F: Sized, T>(&mut self, f: F) -> T
where
F: FnMut(&mut Damus) -> T,
{
with_mut_damus(&self.damus, f)
}
}

impl eframe::App for DamusApp {
fn save(&mut self, storage: &mut dyn eframe::Storage) {
self.with_mut_damus(|damus| damus.save(storage))
}

fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
self.with_mut_damus(|damus| damus.update(ctx, frame));
}
}
12 changes: 10 additions & 2 deletions src/bin/notedeck.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![warn(clippy::all, rust_2018_idioms)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use notedeck::app_creation::generate_native_options;
use notedeck::Damus;
use notedeck::{Damus, DamusApp};

use std::sync::{Arc, Mutex};

// Entry point for wasm
//#[cfg(target_arch = "wasm32")]
Expand All @@ -17,7 +19,13 @@ async fn main() {
let _res = eframe::run_native(
"Damus NoteDeck",
generate_native_options(),
Box::new(|cc| Ok(Box::new(Damus::new(cc, ".", std::env::args().collect())))),
Box::new(|cc| {
Ok(Box::new(DamusApp::new(Arc::new(Mutex::new(Damus::new(
cc,
".",
std::env::args().collect(),
))))))
}),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod view_state;
mod test_utils;
mod linux_key_storage;

pub use app::Damus;
pub use app::{with_mut_damus, Damus, DamusApp, DamusRef};
pub use error::Error;
pub use profile::DisplayName;

Expand Down

0 comments on commit 2a59106

Please sign in to comment.