diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-07 15:49:13 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-10 15:45:27 +0100 |
commit | 81ac4baec0746fd5df328e6af25d88c80c0fef4f (patch) | |
tree | d2d00069897accc977f14f90621182d8b5d00f4b | |
parent | 928b82a6a9d3e06dbdbc7a2ea713fb5555134731 (diff) |
Add Reactor type
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/src/lib.rs | 1 | ||||
-rw-r--r-- | lib/src/reactor/mod.rs | 46 |
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 0cccef9..b86077a 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -5,3 +5,4 @@ pub mod ipfs_client; pub mod profile; pub mod stream; pub mod types; +pub mod reactor; diff --git a/lib/src/reactor/mod.rs b/lib/src/reactor/mod.rs new file mode 100644 index 0000000..70088a3 --- /dev/null +++ b/lib/src/reactor/mod.rs @@ -0,0 +1,46 @@ +use std::sync::Arc; + +use anyhow::Result; +use tokio::sync::RwLock; + +use crate::profile::Profile; + +/// Reactor type, for running the application logic +/// +/// The Reactor runs the whole application logic, that is syncing with other devices, fetching and +/// keeping profile updates of other accounts, communication on the gossipsub topics... etc +#[derive(Debug)] +pub struct Reactor { + profile: Arc<RwLock<Profile>>, +} + +impl Reactor { + pub fn new(profile: Profile) -> Self { + Reactor { + profile: Arc::new(RwLock::new(profile)), + } + } + + pub async fn head(&self) -> Option<cid::Cid> { + self.profile.read().await.head().map(cid::Cid::clone) + } + + pub async fn connect(&self, peer: ipfs::MultiaddrWithPeerId) -> Result<()> { + self.profile.read().await.connect(peer).await + } + + pub fn profile(&self) -> Arc<RwLock<Profile>> { + self.profile.clone() + } + + pub async fn exit(self) -> Result<()> { + let mut inner = self.profile; + loop { + match Arc::try_unwrap(inner) { + Err(arc) => inner = arc, + Ok(inner) => return inner.into_inner().exit().await, + } + } + } + +} |