1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use cyfs_base::BuckyResult;
use async_std::prelude::*;
use async_trait::async_trait;

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

#[async_trait]
pub trait EventListenerAsyncRoutine<P, R>: Send + Sync + 'static
where
    P: Send + Sync + 'static,
    R: 'static,
{
    async fn call(&self, param: &P) -> BuckyResult<R>;
}

#[async_trait]
impl<F, Fut, P, R> EventListenerAsyncRoutine<P, R> for F
where
    P: Send + Sync + 'static,
    R: 'static,
    F: Send + Sync + 'static + Fn(&P) -> Fut,
    Fut: Future<Output = BuckyResult<R>> + Send + 'static,
{
    async fn call(&self, param: &P) -> BuckyResult<R> {
        (self)(param).await
    }
}

#[async_trait]
pub trait EventListenerSyncRoutine<P, R>: Send + Sync + 'static
where
    P: Send + Sync + 'static,
    R: 'static,
{
    fn call(&self, param: &P) -> BuckyResult<R>;
}

#[async_trait]
impl<F, P, R> EventListenerSyncRoutine<P, R> for F
where
    P: Send + Sync + 'static,
    R: 'static,
    F: Send + Sync + 'static + Fn(&P) -> BuckyResult<R>,
{
    fn call(&self, param: &P) -> BuckyResult<R> {
        (self)(param)
    }
}

pub struct SyncEventManager<P, R>
where
    P: Send + Sync + 'static,
    R: 'static,
{
    next_cookie: u32,
    listeners: Vec<(u32, Box<dyn EventListenerSyncRoutine<P, R>>)>,
}

impl<P, R> SyncEventManager<P, R>
where
    P: Send + Sync + 'static,
    R: 'static,
{
    pub fn new() -> Self {
        Self {
            next_cookie: 1,
            listeners: Vec::new(),
        }
    }

    pub fn listener_count(&self) -> usize {
        self.listeners.len()
    }

    pub fn is_empty(&self) -> bool {
        self.listeners.is_empty()
    }

    pub fn on(&mut self, listener: Box<dyn EventListenerSyncRoutine<P, R>>) -> u32 {
        let cookie = self.next_cookie;
        self.next_cookie += 1;

        self.listeners.push((cookie, listener));

        cookie
    }

    pub fn off(&mut self, cookie: u32) -> bool {
        let ret = self.listeners.iter().enumerate().find(|v| v.1 .0 == cookie);

        match ret {
            Some((index, _)) => {
                self.listeners.remove(index);
                true
            }
            None => false,
        }
    }

    pub fn emit(&self, param: &P) -> BuckyResult<Option<R>> {
        let mut ret = None;
        for item in &self.listeners {
            ret = Some(item.1.call(param)?);
        }

        Ok(ret)
    }
}

#[derive(Clone)]
pub struct SyncEventManagerSync<P, R>(Arc<Mutex<SyncEventManager<P, R>>>)
where
    P: Send + Sync + 'static,
    R: 'static;

impl<P, R> SyncEventManagerSync<P, R>
where
    P: Send + Sync + 'static,
    R: 'static,
{
    pub fn new() -> Self {
        let inner = SyncEventManager::new();
        Self(Arc::new(Mutex::new(inner)))
    }

    pub fn listener_count(&self) -> usize {
        self.0.lock().unwrap().listeners.len()
    }

    pub fn is_empty(&self) -> bool {
        self.0.lock().unwrap().is_empty()
    }

    pub fn on(&self, listener: Box<dyn EventListenerSyncRoutine<P, R>>) -> u32 {
        self.0.lock().unwrap().on(listener)
    }

    pub fn off(&self, cookie: u32) -> bool {
        self.0.lock().unwrap().off(cookie)
    }

    pub fn emit(&self, param: &P) -> BuckyResult<Option<R>> {
        self.0.lock().unwrap().emit(param)
    }
}