{"id":17623729,"url":"https://github.com/wiseaidev/emitter-rs","last_synced_at":"2025-03-23T05:25:56.793Z","repository":{"id":242241837,"uuid":"808936748","full_name":"wiseaidev/emitter-rs","owner":"wiseaidev","description":"📢 A lightweight Event Emitter implementation for Rust and Wasm.","archived":false,"fork":false,"pushed_at":"2025-02-10T10:15:20.000Z","size":31,"stargazers_count":0,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-02T03:12:32.217Z","etag":null,"topics":["emitter","event-emitter","pubsub","rust","wasm"],"latest_commit_sha":null,"homepage":"https://docs.rs/emitter-rs","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wiseaidev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-01T07:57:29.000Z","updated_at":"2024-06-01T15:06:32.000Z","dependencies_parsed_at":"2024-06-01T17:17:09.408Z","dependency_job_id":"2195b376-9ea8-4898-840c-e9e87149686c","html_url":"https://github.com/wiseaidev/emitter-rs","commit_stats":null,"previous_names":["wiseaidev/emitter-rs"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiseaidev%2Femitter-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiseaidev%2Femitter-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiseaidev%2Femitter-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiseaidev%2Femitter-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wiseaidev","download_url":"https://codeload.github.com/wiseaidev/emitter-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245060850,"owners_count":20554549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["emitter","event-emitter","pubsub","rust","wasm"],"created_at":"2024-10-22T21:42:36.147Z","updated_at":"2025-03-23T05:25:56.773Z","avatar_url":"https://github.com/wiseaidev.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📢 Emitter RS\n\n[![Crates.io](https://img.shields.io/crates/v/emitter-rs.svg)](https://crates.io/crates/emitter-rs)\n[![License](https://img.shields.io/crates/l/emitter-rs.svg)](https://opensource.org/licenses/MIT)\n\n\u003e 📢 Emitter RS is a simple EventEmitter implementation for Rust and Wasm, providing easy event subscription and firing capabilities.\n\n## 🚀 Getting Started\n\nTo start using `emitter-rs`, add it to your `Cargo.toml`:\n\n```toml\n[dependencies]\nemitter-rs = \"0.0.4\"\n```\n\nThen, you can use it in your code:\n\n```rust\nuse emitter_rs::EventEmitter;\n\nfn main() {\n    let mut event_emitter = EventEmitter::new();\n\n    event_emitter.on(\"Say Hello\", |value: String| {\n        println!(\"{}\", value);\n    });\n\n    event_emitter.emit(\"Say Hello\", \"Hello world!\".to_string());\n}\n```\n\n## 💡 Basic Usage\n\nYou 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.\n\n```rust\nuse emitter_rs::EventEmitter;\n\nfn main() {\n    let mut event_emitter = EventEmitter::new();\n\n    event_emitter.on(\"Add three\", |number: f32| println!(\"{}\", number + 3.0));\n\n    event_emitter.emit(\"Add three\", 5.0 as f32);\n    event_emitter.emit(\"Add three\", 4.0 as f32);\n}\n// \u003e\u003e \"8\"\n// \u003e\u003e \"7\"\n```\n\nUsing a more advanced value type such as a struct by implementing the `serde` traits:\n\n```rust\nuse emitter_rs::EventEmitter;\nuse serde::{Deserialize, Serialize};\n\n#[derive(Serialize, Deserialize)]\nstruct Date {\n    month: String,\n    day: String,\n}\n\nfn main() {\n    let mut event_emitter = EventEmitter::new();\n\n    event_emitter.on(\"LOG_DATE\", |date: Date| {\n        println!(\"Month: {} - Day: {}\", date.month, date.day)\n    });\n\n    event_emitter.emit(\"LOG_DATE\", Date {\n        month: \"January\".to_string(),\n        day: \"Tuesday\".to_string()\n    });\n}\n// \u003e\u003e \"Month: January - Day: Tuesday\"\n```\n\nRemoving listeners is also easy:\n\n```rust\nuse emitter_rs::EventEmitter;\n\nfn main() {\n    let mut event_emitter = EventEmitter::new();\n\n    let listener_id = event_emitter.on(\"Hello\", |_: ()| println!(\"Hello World\"));\n    match event_emitter.remove_listener(\u0026listener_id) {\n        Some(_listener_id) =\u003e println!(\"Removed event listener!\"),\n        None =\u003e println!(\"No event listener of that id exists\")\n    }\n}\n\n// \u003e\u003e \"Removed event listener!\"\n```\n\n## 🌐 Creating a Global EventEmitter\n\nYou can create a global `EventEmitter` instance that can be shared across files:\n\n```rust\n// global_event_emitter.rs\nuse std::sync::Mutex;\nuse emitter_rs::EventEmitter;\nuse lazy_static::lazy_static;\n\n// Use lazy_static! because the size of EventEmitter is not known at compile time\nlazy_static! {\n    // Export the emitter with `pub` keyword\n    pub static ref EVENT_EMITTER: Mutex\u003cEventEmitter\u003e = Mutex::new(EventEmitter::new());\n}\n```\n\nThen import this instance into multiple files:\n\n```ignore\n// main.rs\nmod global_event_emitter;\nuse global_event_emitter::EVENT_EMITTER;\n\nfn main() {\n    // We need to maintain a lock through the mutex to avoid data races\n    EVENT_EMITTER.lock().unwrap().on(\"Hello\", |_: ()| println!(\"hello there!\"));\n    EVENT_EMITTER.lock().unwrap().emit(\"Hello\", ());\n}\n```\n\nAnd in another file, you can listen to the `\"Hello\"` event in `main.rs` by adding a listener to the global event emitter:\n\n```rust\n// some_random_file.rs\nuse emitter_rs::event_emitter_file::EVENT_EMITTER;\n\nfn random_function() {\n    // When the \"Hello\" event is emitted in `main.rs`, print \"Random stuff!\"\n    EVENT_EMITTER.lock().unwrap().on(\"Hello\", |_: ()| println!(\"Random stuff!\"));\n}\n```\n\n## 🌟 Usage in WASM\n\n`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:\n\n```ignore\nuse emitter_rs::EventEmitter;\nuse std::sync::{Arc, Mutex};\nuse wasm_bindgen_futures::spawn_local;\n\nfn run() {\n    let mut event_emitter = EventEmitter::new();\n    let result = Arc::new(Mutex::new(String::new()));\n\n    let cloned_result = Arc::clone(\u0026result);\n\n    spawn_local(async move {\n        event_emitter.on(\"some_event\", move |value: String| {\n            let mut result = cloned_result.lock().unwrap();\n            result.push_str(\u0026value);\n        });\n\n        event_emitter.emit(\"some_event\", \"Hello, world!\".to_string());\n\n        assert_eq!(*result.lock().unwrap(), \"Hello, world!\");\n    });\n}\n```\n\n\u003e [!NOTE]\nEmitter RS is a maintained fork of [`event-emitter-rs`](https://crates.io/crates/event-emitter-rs) crate.\n\n## 📄 License\n\nThis project is licensed under the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwiseaidev%2Femitter-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwiseaidev%2Femitter-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwiseaidev%2Femitter-rs/lists"}