-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9b44f19
Showing
8 changed files
with
773 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[bumpversion] | ||
current_version = 0.0.3 | ||
|
||
[bumpversion:file:Cargo.toml] | ||
search = version = "{current_version}" | ||
replace = version = "{new_version}" | ||
|
||
[bumpversion:file:README.md] | ||
search = emitter-rs = "{current_version}" | ||
replace = emitter-rs = "{new_version}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[package] | ||
edition = "2021" | ||
name = "emitter-rs" | ||
version = "0.0.3" | ||
authors = ["Mahmoud Harmouch <[email protected]>"] | ||
description = "📢 Emitter RS is a lightweight EventEmitter implementation for Rust and Wasm." | ||
documentation = "https://docs.rs/emitter-rs" | ||
repository = "https://github.com/wiseaidev/emitter-rs" | ||
homepage = "https://github.com/wiseaidev/emitter-rs" | ||
keywords = ["event-emitter", "emitter", "EventEmitter", "event"] | ||
categories = ["asynchronous", "wasm"] | ||
license = "MIT" | ||
|
||
[dependencies] | ||
getrandom = { version = "0.2.15", features = ["js"] } | ||
lazy_static = "1.4.0" | ||
serde = { version = "1.0.203", features = ["derive"] } | ||
serde_json = "1.0.117" | ||
uuid = { version = "1.8.0", features = ["v4"] } | ||
|
||
[target.'cfg(target_arch = "wasm32")'.dependencies] | ||
wasm-bindgen-futures = "0.4.42" | ||
futures = "0.3.30" | ||
|
||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies] | ||
wasm-bindgen-test = "0.3.42" | ||
|
||
[badges] | ||
maintenance = { status = "passively-maintained" } | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
|
||
[profile.release] | ||
codegen-units = 1 | ||
opt-level = "z" | ||
lto = "thin" | ||
strip = "symbols" | ||
|
||
[dev-dependencies] | ||
bump2version = "0.1.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# 📢 Emitter RS | ||
|
||
[](https://crates.io/crates/emitter-rs) | ||
[](https://opensource.org/licenses/MIT) | ||
|
||
> 📢 Emitter RS is a simple EventEmitter implementation for Rust and Wasm, providing easy event subscription and firing capabilities. | ||
## 🚀 Getting Started | ||
|
||
To start using `emitter-rs`, add it to your `Cargo.toml`: | ||
|
||
```toml | ||
[dependencies] | ||
emitter-rs = "0.0.3" | ||
``` | ||
|
||
Then, you can use it in your code: | ||
|
||
```rust | ||
use emitter_rs::EventEmitter; | ||
|
||
fn main() { | ||
let mut event_emitter = EventEmitter::new(); | ||
|
||
event_emitter.on("Say Hello", |value: String| { | ||
println!("{}", value); | ||
}); | ||
|
||
event_emitter.emit("Say Hello", "Hello world!".to_string()); | ||
} | ||
``` | ||
|
||
## 💡 Basic Usage | ||
|
||
You can emit and listen to values of any type as long as they implement the `serde` `Serialize` and `Deserialize` traits. A single `EventEmitter` instance can have listeners to values of multiple types. | ||
|
||
```rust | ||
use emitter_rs::EventEmitter; | ||
|
||
fn main() { | ||
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" | ||
// >> "7" | ||
``` | ||
|
||
Using a more advanced value type such as a struct by implementing the `serde` traits: | ||
|
||
```rust | ||
use emitter_rs::EventEmitter; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct Date { | ||
month: String, | ||
day: String, | ||
} | ||
|
||
fn main() { | ||
let mut event_emitter = EventEmitter::new(); | ||
|
||
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: | ||
|
||
```rust | ||
use emitter_rs::EventEmitter; | ||
|
||
fn main() { | ||
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) => println!("Removed event listener!"), | ||
None => println!("No event listener of that id exists") | ||
} | ||
} | ||
|
||
// >> "Removed event listener!" | ||
``` | ||
|
||
## 🌐 Creating a Global EventEmitter | ||
|
||
You can create a global `EventEmitter` instance that can be shared across files: | ||
|
||
```rust | ||
// global_event_emitter.rs | ||
use std::sync::Mutex; | ||
use emitter_rs::EventEmitter; | ||
use lazy_static::lazy_static; | ||
|
||
// 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 import this instance into multiple files: | ||
|
||
```ignore | ||
// main.rs | ||
mod global_event_emitter; | ||
use global_event_emitter::EVENT_EMITTER; | ||
fn main() { | ||
// We need to maintain a lock through the mutex to avoid data races | ||
EVENT_EMITTER.lock().unwrap().on("Hello", |_: ()| println!("hello there!")); | ||
EVENT_EMITTER.lock().unwrap().emit("Hello", ()); | ||
} | ||
``` | ||
|
||
And in another file, you can listen to the `"Hello"` event in `main.rs` by adding a listener to the global event emitter: | ||
|
||
```rust | ||
// some_random_file.rs | ||
use emitter_rs::event_emitter_file::EVENT_EMITTER; | ||
|
||
fn random_function() { | ||
// When the "Hello" event is emitted in `main.rs`, print "Random stuff!" | ||
EVENT_EMITTER.lock().unwrap().on("Hello", |_: ()| println!("Random stuff!")); | ||
} | ||
``` | ||
|
||
## 🌟 Usage in WASM | ||
|
||
`Emitter RS` can be seamlessly integrated into WebAssembly (WASM) projects, allowing you to create event-driven applications in the browser. Consider the following as an example: | ||
|
||
```ignore | ||
use emitter_rs::EventEmitter; | ||
use std::sync::{Arc, Mutex}; | ||
use wasm_bindgen_futures::spawn_local; | ||
fn run() { | ||
let mut event_emitter = EventEmitter::new(); | ||
let result = Arc::new(Mutex::new(String::new())); | ||
let cloned_result = Arc::clone(&result); | ||
spawn_local(async move { | ||
event_emitter.on("some_event", move |value: String| { | ||
let mut result = cloned_result.lock().unwrap(); | ||
result.push_str(&value); | ||
}); | ||
event_emitter.emit("some_event", "Hello, world!".to_string()); | ||
assert_eq!(*result.lock().unwrap(), "Hello, world!"); | ||
}); | ||
} | ||
``` | ||
|
||
> [!NOTE] | ||
Emitter RS is a maintained fork of [`event-emitter-rs`](https://crates.io/crates/event-emitter-rs) crate. | ||
|
||
## 📄 License | ||
|
||
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT). |
Oops, something went wrong.