polariton_server 0.6.0

Server functionality for the Photon packet system
Documentation
#[derive(Clone)]
pub struct EventEmitter<C: Send + 'static = ()> {
    sender: tokio::sync::mpsc::UnboundedSender<crate::ToSend<C>>,
}

impl <C: Send + 'static> EventEmitter<C> {
    pub fn new(sender: tokio::sync::mpsc::UnboundedSender<crate::ToSend<C>>) -> Self {
        Self { sender }
    }

    pub fn emit<E: super::IntoEvent<C>>(&self, event: E) -> bool {
        self.sender.send(event.into_sendable()).is_ok()
    }

    pub fn downgrade(self) -> WeakEventEmitter<C> {
        WeakEventEmitter {
            sender: self.sender.downgrade(),
        }
    }
}

pub struct WeakEventEmitter<C: Send + 'static = ()> {
    sender: tokio::sync::mpsc::WeakUnboundedSender<crate::ToSend<C>>,
}

impl <C: Send + 'static> WeakEventEmitter<C> {
    pub fn new(sender: tokio::sync::mpsc::WeakUnboundedSender<crate::ToSend<C>>) -> Self {
        Self { sender }
    }

    pub fn emit<E: super::IntoEvent<C>>(&self, event: E) -> bool {
        if let Some(strong_sender) = self.sender.upgrade() {
            strong_sender.send(event.into_sendable()).is_ok()
        } else {
            false
        }

    }
}