mycorrh/
event.rs

1use std::{
2    result::Result,
3    sync::Arc,
4};
5
6use tokio::{
7    sync::RwLock,
8    task::JoinHandle,
9};
10
11pub trait ScEventEmitter: EventLoop {
12    type Event: Send + Sync + std::fmt::Debug;
13
14    fn emit(&self, event: Self::Event) -> Result<(), Self::Err>;
15}
16
17pub trait McEventEmitter<T>: EventLoop {
18    fn emit(&self, data: T) -> Result<(), Self::Err>;
19
20    fn on(&self, callback: dyn FnOnce(T) -> Result<(), Self::Err>) -> Result<(), Self::Err>;
21}
22
23#[async_trait::async_trait]
24pub trait EventLoop {
25    type Close: Send + Sync + std::fmt::Debug + std::fmt::Display;
26    type Err: Send + Sync + std::error::Error;
27
28    fn event_loop_join_handle(&self) -> &Arc<RwLock<JoinHandle<Result<Self::Close, Self::Err>>>>;
29
30    fn is_closed(&self) -> bool;
31
32    /// If Err is returned, the event loop is already closed.
33    async fn close(&self, reason: Self::Close) -> Result<(), Self::Err>;
34}