15 unstable releases (6 breaking)
0.6.1 | Aug 24, 2023 |
---|---|
0.5.0 | Apr 23, 2023 |
0.4.1 | Mar 8, 2023 |
0.3.3 | Nov 1, 2021 |
0.1.2 | Dec 13, 2020 |
#1444 in Asynchronous
17KB
353 lines
wd_event: A async event crate for rust
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
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.
Dependencies
~2.4–8.5MB
~69K SLoC