eventbuzz 0.1.0

A safe, fast, concurrent event publish/subscribe system based on tokio(async), inspired by Spring events.
Documentation

eventbuzz

A safe, fast, event publish/subscribe system, where asynchronous events are implemented based on tokio, and inspired by Spring events.

1.Usage

Add this to your Cargo.toml:

[dependencies]

eventbuzz = "0.1"



# 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::asynchronous::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;