nodevent
A Node.js-style event bus in Rust with synchronous and asynchronous support (via Tokio).
Installation
# Cargo.toml
[]
= { = "../path_to_your_crate" }
= { = "1", = ["full"] }
1. Synchronous Event Handling
use ;
on(event, callback): Registers a persistent listener.once(event, callback): Registers a listener that will be removed after first call.off(event, id): Removes a specific listener by its ID.off_all(event): Removes all listeners for an event.
2. Thread-Safe / Multi-Threaded Event Handling
use ;
use ;
use Runtime;
- Use
ts_args!to pass arguments in a thread-safe way (Arc<Vec<Box<dyn Any + Send + Sync>>>). - Multi-threaded emitters require a
tokio::runtime::Handleto run async tasks.
3. Asynchronous Event Handling
use ;
use Runtime;
async
on_async/once_asyncrequire closures returningPin<Box<dyn Future<Output = ()> + Send + Sync>>.- Use
Box::pin(async move { ... })inside the closure.
4. Macros
use ;
// Synchronous args (single-threaded)
let sync_args = args!;
// Thread-safe args (multi-threaded)
let safe_args = ts_args!;
args!: Wraps values intoBox<dyn Any>insideRc<Vec<_>>.ts_args!: Wraps values intoBox<dyn Any + Send + Sync>insideArc<Vec<_>>.
5. Summary
| Feature | Macro / Method | Thread Safety |
|---|---|---|
| Synchronous listener | on / once |
Single-thread |
| Asynchronous listener | on_async / once_async |
Multi-thread |
| Emit event | emit |
Single/Multi-thread |
| Arguments | args! / ts_args! |
Single / Multi-thread |
This crate provides a flexible Node.js-style event bus in Rust, with full async support via Tokio and optional multi-threaded safety.