ferris_emitter

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.

Basic Usage

```rust use eventemitter::EventEmitter; let mut eventemitter = EventEmitter::new();

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

Advanced Usage

We can also emit values of any type so long as they implement the serde Serialize and Deserialize traits

```rust use eventemitter::EventEmitter; let mut eventemitter = EventEmitter::new();

eventemitter.on("Add three", |number: f32| println!("{}", number + 3.0)); eventemitter.emit("Add three", 5.0 as f32); // >> "8.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, }

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

Removing listeners is also easy

```rust use eventemitter::EventEmitter; let mut eventemitter = EventEmitter::new();

let listenerid = eventemitter.on("Hello", |: ()| println!("Hello World")); match eventemitter.removelistener(listenerid) { Some(listener_id) => print!("Removed event listener!"), None => print!("No event listener of that id exists") } ```