Documentation

wd_event: A async event crate for rust

wd_event GitHub Actions wd_event on crates.io wd_event on docs.rs License

This is a Rust async event Crate. Compared to the previous version, this is a brand new version, understandably a brand new crate

  • Support for circular calls
  • Support for call timeouts
  • Support for arbitrary type passing
  • Support for single and global middleware
  • Event registration supports the trait, lambda and function

Getting Started

wd_event is available on crates.io. It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.

At the point of the last update of this README, the latest published version could be used like this:

Add the following dependency to your Cargo manifest...

[dependencies]
wd_event = "0.3"

Example

use wd_event::{Error,EventHandle,EventManage,Context};
use std::pin::Pin;
use std::future::Future;

#[tokio::main]
async fn main(){
    let mut em = EventManage::new()
        .set_max_cycle(99)
        .set_timeout(std::time::Duration::from_secs(100));
    //impl EventHandle
    em.register_event("say",|ctx:Context|->Pin<Box<dyn Future<Output=Context>+Send>>{
        Box::pin(async move{
            let value = ctx.copy::<_,String>("value").await.unwrap();
            println!("say:{}",value);
            return ctx;
        })
    });

    let ctx = Context::new();
    ctx.set("value",format!("hello world")).await;
    let ctx = em.dispatch("say",ctx).await;
    if let Some(s) = ctx.error().await{
        println!("error:{}",s);
    }
}

attention:

  • ctx.next needs to be executed in the middleware, otherwise the execution of the event will be terminated
  • ctx.abort can terminate downward execution in any middleware or event
  • Settings on context take precedence over global Settings

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.