4个版本
0.2.2 | 2024年7月25日 |
---|---|
0.2.1 | 2024年7月24日 |
0.2.0 | 2024年7月24日 |
0.1.0 | 2024年7月24日 |
222 在 异步
每月321次下载
38KB
481 行
eventbuzz
一个安全、快速的事件发布/订阅系统,其中异步事件基于 tokio
实现,并受 Spring
事件启发。
1. 使用
将此添加到您的 Cargo.toml
[dependencies]
eventbuzz = "0.2"
# And
# If necessary
tokio = "${version}"
async-trait = "${version}"
2. APIs
2.1. Sync
use eventbuzz::sync::prelude::*;
2.1.1. 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")
}
}
2.1.2. Listener
struct HelloEventListener;
// ----------------------------------------------------------------
// HelloEvent -> This target event of Listener.
impl ApplicationEventListener<HelloEvent> for HelloEventListener {
fn on_application_event(&self, event: &HelloEvent) {
// Handle event.
}
}
2.1.3. 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!"),
});
2.2. Async
use eventbuzz::异步::prelude::*;
2.2.1. 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")
}
}
2.2.2. 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;
2.3.4. 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();
依赖
~3.5–10MB
~86K SLoC