use std::sync::Arc;
use anyhow::{anyhow, Result};
use tokio::sync::mpsc;
use crate::{core::identity::Identity, crypto::signature::Signer};
use super::{client, processor, App};
pub struct Environment<A: App> {
app: Arc<A>,
client: client::Client<A>,
signer: Arc<dyn Signer>,
identity: Arc<Identity>,
cmdq: mpsc::WeakSender<processor::Command>,
}
impl<A> Environment<A>
where
A: App,
{
pub(super) fn new(
state: Arc<processor::State<A>>,
cmdq: mpsc::WeakSender<processor::Command>,
) -> Self {
Self {
app: state.app.clone(),
signer: state.signer.clone(),
identity: state.identity.clone(),
client: client::Client::new(state, cmdq.clone()),
cmdq,
}
}
pub fn app(&self) -> Arc<A> {
self.app.clone()
}
pub fn client(&self) -> &client::Client<A> {
&self.client
}
pub fn signer(&self) -> Arc<dyn Signer> {
self.signer.clone()
}
pub fn identity(&self) -> Arc<Identity> {
self.identity.clone()
}
pub(super) async fn send_command(&self, cmd: processor::Command) -> Result<()> {
let cmdq = self
.cmdq
.upgrade()
.ok_or(anyhow!("processor has shut down"))?;
cmdq.send(cmd).await?;
Ok(())
}
}
impl<A> Clone for Environment<A>
where
A: App,
{
fn clone(&self) -> Self {
Self {
app: self.app.clone(),
signer: self.signer.clone(),
identity: self.identity.clone(),
client: self.client.clone(),
cmdq: self.cmdq.clone(),
}
}
}