wd_event

Crates.iowd_event
lib.rswd_event
version0.6.1
sourcesrc
created_at2020-12-06 12:17:42.546617
updated_at2023-08-24 02:51:50.768682
descriptionevent system
homepage
repository
max_upload_size
id320108
size17,929
teshin (woshihaoren4)

documentation

README

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. Not compatible with previous versions

  • you can customize any type of message event
  • Event handler, which can be a function or trait
  • According to the registration order, the events are executed sequentially and propagated through the online text
  • The event entity is abstract, and you can assemble it as you wish
  • This is an early release and the api may change in the future

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.4"

Example

struct Message<V>{
    id : u64,
    value: V,
}
impl<V> Message<V> {
    fn new(id:u64,value:V)->Message<V>{
        Self{id,value}
    }
}
fn generate_event_entity()->Event<EventSchedule>{
    EventSchedule::default()
        .register(|ctx:Context, mut msg:Message<String>|{
            println!("this is first Message<String> handler ID[{}]--> {}",msg.id,msg.value);
            msg.value.push_str("->one");
            ctx.next(msg)  // no need async
        })
        .register(|ctx:Context, mut msg:Message<String>|async{
            println!("this is second Message<String> handler ID[{}]--> {}",msg.id,msg.value);
            msg.value.push_str("->two");
            ctx.next(msg).await //all is async
        })
        .register(|ctx:Context, mut msg:Message<i64>|{
            println!("this is third Message<i64> handle  ID[{}]--> {}",msg.id,msg.value);
            msg.value = 3;
            async move{
                ctx.next(msg).await //part is async
            }
        })
        .build()
}

#[tokio::test]
async fn test_message(){
    let event = generate_event_entity();
    let res = event.launch(Context::default(),Message::new(1,"hello world".to_string())).await.expect("first test handle failed");
    assert_eq!(res.value.as_str(),"hello world->one->two","Message<String> result assert failed");
    let res = event.launch(Context::default(),Message::new(1,1i64)).await.expect("second test failed");
    assert_eq!(res.value,3i64,"Message<i64> result assert failed")
}

attention:

  • the event handler executes in the order of registration, ctx.next representing the next handler to execute the event
  • Context can deliver messages in context
  • 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.

Commit count: 0

cargo fmt