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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*! 
[![](http://meritbadge.herokuapp.com/event-emitter-rs)](https://crates.io/crates/event-emitter-rs)

A simple EventEmitter implementation.

Allows you to subscribe to events with callbacks and also fire those events.
Events are in the form of (strings, value) and callbacks are in the form of closures that take in a value parameter.

# Getting Started

```
use event_emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();

// This will print <"Hello world!"> whenever the <"Say Hello"> event is emitted
event_emitter.on("Say Hello", |value: ()| println!("Hello world!"));
event_emitter.emit("Say Hello", ()); 
// >> "Hello world!"
```

# Basic Usage

We can emit and listen to values of any type so long as they implement the serde Serialize and Deserialize traits.
A single EventEmitter instance can have listeners to values of multiple types.

```
use event_emitter_rs::EventEmitter;
use serde::{Deserialize, Serialize};
let mut event_emitter = EventEmitter::new();

event_emitter.on("Add three", |number: f32| println!("{}", number + 3.0));
event_emitter.emit("Add three", 5.0 as f32); 
event_emitter.emit("Add three", 4.0 as f32);
// >> "8.0"
// >> "7.0"

// Using a more advanced value type such as a struct by implementing the serde traits
#[derive(Serialize, Deserialize)]
struct Date {
    month: String,
    day: String,   
}

event_emitter.on("LOG_DATE", |date: Date| {
    println!("Month: {} - Day: {}", date.month, date.day)
});
event_emitter.emit("LOG_DATE", Date { 
    month: "January".to_string(), 
    day: "Tuesday".to_string() 
}); 
// >> "Month: January - Day: Tuesday"
```

Removing listeners is also easy

```
use event_emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();

let listener_id = event_emitter.on("Hello", |_: ()| println!("Hello World"));
match event_emitter.remove_listener(&listener_id) {
    Some(listener_id) => print!("Removed event listener!"),
    None => print!("No event listener of that id exists")
}
```
# Creating a Global EventEmitter

It's likely that you'll want to have a single EventEmitter instance that can be shared accross files;

After all, one of the main points of using an EventEmitter is to avoid passing down a value through several nested functions/types and having a global subscription service.

```
// global_event_emitter.rs
use std::sync::Mutex;
use crate::EventEmitter;

// Use lazy_static! because the size of EventEmitter is not known at compile time
lazy_static! {
    // Export the emitter with `pub` keyword
    pub static ref EVENT_EMITTER: Mutex<EventEmitter> = Mutex::new(EventEmitter::new());
}
```

Then we can import this instance into multiple files.

```
// main.rs
#[macro_use]
extern crate lazy_static;

mod global_event_emitter;
use global_event_emitter::EVENT_EMITTER;

fn main() {
    // We need to maintain a lock through the mutex so we can avoid data races
    EVENT_EMITTER.lock().unwrap().on("Hello", |_: ()| println!("hello there!"));
    EVENT_EMITTER.lock().unwrap().emit("Hello", ());
}
```

And in another file we can now listen to the <"Hello"> event in main.rs by adding a listener to the global event emitter.

```
// some_random_file.rs
use crate::global_event_emitter::EVENT_EMITTER;

fn random_function() {
    // When the <"Hello"> event is emitted in main.rs then print <"Random stuff!">
    EVENT_EMITTER.lock().unwrap().on("Hello", |_: ()| println!("Random stuff!"));
}
```
*/

//#[cfg(test)]
mod tests;

use std::collections::HashMap;
use std::thread;
use std::sync::Arc;
use serde::{Deserialize, Serialize};

#[macro_use]
extern crate lazy_static;

use bincode;

use uuid::Uuid;

pub struct Listener {
    callback: Arc<dyn Fn(Vec<u8>) + Sync + Send + 'static>,
    limit: Option<u64>,
    id: String,
}

#[derive(Default)]
pub struct EventEmitter {
    pub listeners: HashMap<String, Vec<Listener>>
}

impl EventEmitter {
    // Potentially may want to add features here in the future so keep it like this
    pub fn new() -> Self {
        Self {
            ..Self::default()
        }
    }

    /// Adds an event listener with a callback that will get called whenever the given event is emitted.
    /// Returns the id of the newly added listener.
    ///
    /// # Example
    ///
    /// ```
    /// use event_emitter_rs::EventEmitter;
    /// let mut event_emitter = EventEmitter::new();
    ///
    /// // This will print <"Hello world!"> whenever the <"Some event"> event is emitted
    /// // The type of the `value` parameter for the closure MUST be specified and, if you plan to use the `value`, the `value` type 
    /// // MUST also match the type that is being emitted (here we just use a throwaway `()` type since we don't care about using the `value`)
    /// event_emitter.on("Some event", |value: ()| println!("Hello world!"));
    /// ```
    pub fn on<F, T>(&mut self, event: &str, callback: F) -> String
        where 
            for<'de> T: Deserialize<'de>,
            F: Fn(T) + 'static + Sync + Send 
    {
        let id = self.on_limited(event, None, callback);
        return id;
    }

    /// Emits an event of the given parameters and executes each callback that is listening to that event asynchronously by spawning a new thread for each callback.
    ///
    /// # Example
    ///
    /// ```
    /// use event_emitter_rs::EventEmitter;
    /// let mut event_emitter = EventEmitter::new();
    ///
    /// // Emits the <"Some event"> event and a value <"Hello programmer">
    /// // The value can be of any type as long as it implements the serde Serialize trait
    /// event_emitter.emit("Some event", "Hello programmer!");
    /// ```
    pub fn emit<T>(&mut self, event: &str, value: T) -> Vec<thread::JoinHandle<()>>
        where T: Serialize
    {
        let mut callback_handlers: Vec<thread::JoinHandle<()>> = Vec::new();

        if let Some(listeners) = self.listeners.get_mut(event) {
            let bytes: Vec<u8> = bincode::serialize(&value).unwrap();
            
            let mut listeners_to_remove: Vec<usize> = Vec::new();
            for (index, listener) in listeners.iter_mut().enumerate() {
                let cloned_bytes = bytes.clone();
                let callback = Arc::clone(&listener.callback);

                match listener.limit {
                    None => { 
                        callback_handlers.push(thread::spawn(move || { 
                            callback(cloned_bytes);
                        })); 
                    },
                    Some(limit) => {
                        if limit != 0 {
                            callback_handlers.push(thread::spawn(move || {
                                callback(cloned_bytes);
                            }));
                            listener.limit = Some(limit - 1);
                        } else {
                            listeners_to_remove.push(index);
                        }
                    }
                }
            }

            // Reverse here so we don't mess up the ordering of the vector
            for index in listeners_to_remove.into_iter().rev() {
                listeners.remove(index);
            }
        }

        return callback_handlers;
    }

    /// Removes an event listener with the given id
    ///
    /// # Example
    ///
    /// ```
    /// use event_emitter_rs::EventEmitter;
    /// let mut event_emitter = EventEmitter::new();
    /// let listener_id = event_emitter.on("Some event", |value: ()| println!("Hello world!"));
    ///
    /// // Removes the listener that we just added
    /// event_emitter.remove_listener(&listener_id);
    /// ```
    pub fn remove_listener(&mut self, id_to_delete: &str) -> Option<String> {
        for (_, event_listeners) in self.listeners.iter_mut() {
            if let Some(index) = event_listeners.iter().position(|listener| listener.id == id_to_delete) {
                event_listeners.remove(index);
                return Some(id_to_delete.to_string());
            } 
        }

        return None;
    }

    /// Adds an event listener that will only execute the listener x amount of times - Then the listener will be deleted.
    /// Returns the id of the newly added listener.
    ///
    /// # Example
    ///
    /// ```
    /// use event_emitter_rs::EventEmitter;
    /// let mut event_emitter = EventEmitter::new();
    ///
    /// // Listener will be executed 3 times. After the third time, the listener will be deleted.
    /// event_emitter.on_limited("Some event", Some(3), |value: ()| println!("Hello world!"));
    /// event_emitter.emit("Some event", ()); // 1 >> "Hello world!"
    /// event_emitter.emit("Some event", ()); // 2 >> "Hello world!"
    /// event_emitter.emit("Some event", ()); // 3 >> "Hello world!"
    /// event_emitter.emit("Some event", ()); // 4 >> <Nothing happens here because listener was deleted after the 3rd call>
    /// ```
    pub fn on_limited<F, T>(&mut self, event: &str, limit: Option<u64>, callback: F) -> String
        where 
            for<'de> T: Deserialize<'de>,
            F: Fn(T) + 'static + Sync + Send 
    {
        let id = Uuid::new_v4().to_string();
        let parsed_callback = move |bytes: Vec<u8>| {
            let value: T = bincode::deserialize(&bytes).unwrap();
            callback(value);
        };

        let listener = Listener {
            id: id.clone(),
            limit,
            callback: Arc::new(parsed_callback),
        };

        match self.listeners.get_mut(event) {
            Some(callbacks) => { callbacks.push(listener); },
            None => { self.listeners.insert(event.to_string(), vec![listener]); }
        }

        return id;
    }

    /// Adds an event listener that will only execute the callback once - Then the listener will be deleted.
    /// Returns the id of the newly added listener.
    ///
    /// # Example
    ///
    /// ```
    /// use event_emitter_rs::EventEmitter;
    /// let mut event_emitter = EventEmitter::new();
    ///
    /// event_emitter.once("Some event", |value: ()| println!("Hello world!"));
    /// event_emitter.emit("Some event", ()); // First event is emitted and the listener's callback is called once
    /// // >> "Hello world!"
    ///
    /// event_emitter.emit("Some event", ());
    /// // >> <Nothing happens here since listener was deleted>
    /// ```
    pub fn once<F, T>(&mut self, event: &str, callback: F) -> String
        where 
            for<'de> T: Deserialize<'de>,
            F: Fn(T) + 'static + Sync + Send 
    {
        let id = self.on_limited(event, Some(1), callback);
        return id;
    }

    /// NOT IMPLEMENTED!
    /// Emits an event of the given parameters in a synchronous fashion.
    /// Instead of executing each callback in a newly spawned thread, it will execute each callback in the order that they were inserted.
    ///
    /// # Example
    ///
    /// ```
    /// use event_emitter_rs::EventEmitter;
    /// let mut event_emitter = EventEmitter::new();
    ///
    /// event_emitter.on("Some event", |value: ()| println!("1")); // Guaranteed to be executed first
    /// event_emitter.on("Some event", |value: ()| println!("2")); // Will not execute this until the first callback has finished executing
    /// event_emitter.on("Some event", |value: ()| println!("3")); // Will not execute this until the second callback has finished executing
    ///
    /// // Emits the <"Some event"> event and a value <"Hello programmer">
    /// // The value can be of any type
    /// event_emitter.sync_emit("Some event", "Hello programmer!");
    /// ```
    pub fn sync_emit<T>(&self, event: &str, value: T) 
        where T: Serialize
    {
    }
}