Crates.io | mediator-sys |
lib.rs | mediator-sys |
version | 2.0.2 |
source | src |
created_at | 2022-09-29 00:44:00.122032 |
updated_at | 2022-11-04 17:42:26.721179 |
description | Strongly typed, extensible event mediator |
homepage | https://github.com/nyvs/mediator-sys |
repository | https://github.com/nyvs/mediator-sys |
max_upload_size | |
id | 676264 |
size | 45,328 |
Strongly typed, extensible event mediator. For more info and explanation, please see the docs.
This crate has been renamed and is soon phasing out. Please use mediatrix instead.
use mediator_sys::synchronous::basic::*;
struct UserMessageRequest {
msg: String,
priority: u8,
}
#[derive(Debug, Clone)]
enum NotifyEvent {
Ignore,
SendEmail(String),
SendTextMessage(String),
}
impl RequestHandler<UserMessageRequest, NotifyEvent> for BasicMediator<NotifyEvent> {
fn handle(&self, req: UserMessageRequest) {
match req.priority {
0 => self.publish(NotifyEvent::Ignore),
1..=5 => self.publish(NotifyEvent::SendEmail(req.msg)),
_ => self.publish(NotifyEvent::SendTextMessage(req.msg)),
};
}
}
let mediator = BasicMediator::<NotifyEvent>::builder()
.add_listener(move |ev| {
if let NotifyEvent::Ignore = ev {
println!("Ignored some Message")
}
})
.add_listener(move |ev| {
if let NotifyEvent::SendEmail(msg) = ev {
println!("Send Email with Message: {}", msg)
}
})
.add_listener(move |ev| {
if let NotifyEvent::SendTextMessage(msg) = ev {
println!("Send SMS with Message: {}", msg)
}
})
.build();
mediator.send(UserMessageRequest {
msg: String::from("Hello World"),
priority: 0,
});
mediator.send(UserMessageRequest {
msg: String::from("Is Rust Memory Safe?"),
priority: 2,
});
mediator.send(UserMessageRequest {
msg: String::from("New Rust Version"),
priority: 8,
});
// Prints: Ignored some Message
mediator.next().ok();
// Prints: Send Email with Message: Is Rust Memory Safe?
mediator.next().ok();
// Prints: Send SMS with Message: New Rust Version
mediator.next().ok();
use mediator_sys::asynchronous::basic::*;
use async_trait::async_trait;
struct UserMessageRequest {
msg: String,
priority: u8,
}
#[derive(Debug, Clone)]
enum NotifyEvent {
Ignore,
SendEmail(String),
SendTextMessage(String),
}
#[async_trait]
impl AsyncRequestHandler<UserMessageRequest, NotifyEvent> for BasicAsyncMediator<NotifyEvent> {
async fn handle(&self, req: UserMessageRequest) {
match req.priority {
0 => self.publish(NotifyEvent::Ignore).await,
1..=5 => self.publish(NotifyEvent::SendEmail(req.msg)).await,
_ => self.publish(NotifyEvent::SendTextMessage(req.msg)).await,
};
}
}
let async_mediator = BasicAsyncMediator::<NotifyEvent>::builder()
.add_listener(move |ev| {
if let NotifyEvent::Ignore = ev {
println!("Ignored some Message")
}
})
.add_listener(move |ev| {
if let NotifyEvent::SendEmail(msg) = ev {
println!("Send Email with Message: {}", msg)
}
})
.add_listener(move |ev| {
if let NotifyEvent::SendTextMessage(msg) = ev {
println!("Send SMS with Message: {}", msg)
}
})
.build();
async_std::task::block_on(async {
async_mediator.send(UserMessageRequest {
msg: String::from("Hello World"),
priority: 0,
}).await;
async_mediator.send(UserMessageRequest {
msg: String::from("Is Rust Memory Safe?"),
priority: 2,
}).await;
async_mediator.send(UserMessageRequest {
msg: String::from("New Rust Version"),
priority: 8,
}).await;
async_mediator.next().await.ok();
async_mediator.next().await.ok();
async_mediator.next().await.ok();
});
async
feature) mediatorsCxAwareMediator
(use async
feature, carries a dependency of your choice)Clone
bound on events Ev
for SyncMediatorInternalNext
.Feel free to open an issue/PR explaining possible improvements or changes.
Also, please do not hesitate and open an issue when needed. I am happy to help!