Crates.io | eventbuzz |
lib.rs | eventbuzz |
version | 0.2.2 |
source | src |
created_at | 2024-07-24 18:48:09.322898 |
updated_at | 2024-07-25 13:54:17.656312 |
description | A safe, fast, concurrent event publish/subscribe system based on tokio(async), inspired by Spring events. |
homepage | https://github.com/photowey/eventbuzz |
repository | https://github.com/photowey/eventbuzz |
max_upload_size | |
id | 1314232 |
size | 18,788 |
eventbuzz
A safe, fast, event publish/subscribe system, where asynchronous events are implemented based on tokio
, and inspired
by Spring
events.
Usage
Add this to your Cargo.toml
:
[dependencies]
eventbuzz = "0.2"
# And
# If necessary
tokio = "${version}"
async-trait = "${version}"
APIs
Sync
use eventbuzz::sync::prelude::*;
Event
use eventbuzz::sync::prelude::*;
struct HelloEvent {
message: String,
}
// ...
// ----------------------------------------------------------------
impl ApplicationEvent for HelloEvent {
fn topic() -> String {
// default: io.github.eventbuzz.global.default.topic
// Unused now.
String::from("io.github.eventbuzz.global.hello.topic")
}
}
Listener
struct HelloEventListener;
// ----------------------------------------------------------------
// HelloEvent -> This target event of Listener.
impl ApplicationEventListener<HelloEvent> for HelloEventListener {
fn on_application_event(&self, event: &HelloEvent) {
// Handle event.
}
}
Publish
// 1.Build an instance of Eventbus
// -> Maybe -> Eventbus::new() | unsupported now.
let mut eventbus: Eventbus = Eventbus::builder()
/* config or init | Unsupported now */
.build();
// 2.Register
// -> Auto register unsupported now.
eventbus.register_listener(HelloEventListener);
eventbus.register_listener(GreetingEventListener);
// 3.Publish event.
eventbus.publish_event(HelloEvent {
message: String::from("Hello, HelloEvent!"),
});
eventbus.publish_event(GreetingEvent {
message: String::from("Hello, GreetingEvent!"),
});
Async
use eventbuzz::asynchronous::prelude::*;
Event
use eventbuzz::asynchronous::prelude::*;
struct HelloEvent {
message: String,
}
// ...
// ----------------------------------------------------------------
impl ApplicationEvent for HelloEvent {
fn topic() -> String {
// default: io.github.eventbuzz.global.default.topic
// Unused now.
String::from("io.github.eventbuzz.global.hello.topic")
}
}
Listener
use
#[async_trait]
struct HelloEventListener;
// ----------------------------------------------------------------
// Notes: #[async_trait]
// HelloEvent -> This target event of Listener.
#[async_trait]
impl AsyncApplicationEventListener<HelloEvent> for HelloEventListener {
async fn on_application_event(&self, event: &HelloEvent) {
// Handle event.
}
}
2.2.3. Publish
// #[tokio::test(flavor = "multi_thread")]
// 1.Build an instance of Eventbus
// -> Maybe -> Eventbus::new() | unsupported now.
let mut eventbus: AsyncEventbus = AsyncEventbus::builder()
/* config or init | Unsupported now */
.build();
// 2.Register
// -> Auto register unsupported now.
eventbus.register_listener(HelloEventListener).await;
eventbus.register_listener(GreetingEventListener).await;
// 3.Publish event.
eventbus.publish_event(HelloEvent {
message: String::from("Hello, HelloEvent!"),
}).await;
eventbus.publish_event(GreetingEvent {
message: String::from("Hello, GreetingEvent!"),
}).await;
tokio::spawn
let mut eventbus: AsyncEventbus = AsyncEventbus::builder()
/* config or init | Unsupported now */
.build();
eventbus.register_listener(HelloEventListener).await;
eventbus.register_listener(GreetingEventListener).await;
// Spawn
tokio::spawn( async move {
eventbus.publish_event(HelloEvent {
message: String::from("Hello, tokio.HelloEvent!"),
}).await;
}).await.unwrap();
// Arc<AsyncEventbus>
let mut eventbus: AsyncEventbus = AsyncEventbus::builder()
/* config or init | Unsupported now */
.build();
eventbus.register_listener(HelloEventListener).await;
eventbus.register_listener(GreetingEventListener).await;
let eventbus_arc = Arc::new(eventbus);
let eventbus_wrapped_1 = Arc::clone( & eventbus_arc);
tokio::spawn( async move {
eventbus_wrapped_1.publish_event(HelloEvent {
message: String::from("Hello, multi.tokio.arc.1.HelloEvent!"),
}).await;
}).await.unwrap();
let eventbus_wrapped_2 = Arc::clone( & eventbus_arc);
tokio::spawn( async move {
eventbus_wrapped_2.publish_event(HelloEvent {
message: String::from("Hello, multi.tokio.arc.2.HelloEvent!"),
}).await;
}).await.unwrap();