1 个稳定版本

1.0.0 2022年11月4日

#1448异步

MIT/Apache

46KB
660

mediatrix

Latest Release Documentation Dependencies

强类型、可扩展的事件中介。更多信息和说明,请参阅文档

用法

同步

use mediatrix::synchronous::basic::*;

struct UserMessageRequest {
    msg: String,
    priority: u8,
}

#[derive(Debug)]
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: &NotifyEvent| {
        if let NotifyEvent::Ignore = ev {
            println!("Ignored some Message")
        }
    })
    .add_listener(move |ev: &NotifyEvent| {
        if let NotifyEvent::SendEmail(msg) = ev {
            println!("Send Email with Message: {}", msg)
        }
    })
    .add_listener(move |ev: &NotifyEvent| {
        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 mediatrix::asynchronous::basic::*;
use async_trait::async_trait;

struct UserMessageRequest {
    msg: String,
    priority: u8,
}

#[derive(Debug)]
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: &NotifyEvent| {
        if let NotifyEvent::Ignore = ev {
            println!("Ignored some Message")
        }
    })
    .add_listener(move |ev: &NotifyEvent| {
        if let NotifyEvent::SendEmail(msg) = ev {
            println!("Send Email with Message: {}", msg)
        }
    })
    .add_listener(move |ev: &NotifyEvent| {
        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 功能)中介
  • CxAwareMediator(使用 async 功能,携带您选择的任何结构体)
  • 编译器内建类型
  • 可扩展架构

待办事项

  • 内部,使构建器重用目标为该构建器组成部分的其它构建器。

贡献

请随意提出一个说明可能的改进或更改的问题/PR。

帮助

此外,如有需要,请随时提出一个问题。我很乐意提供帮助!

依赖

~0–13MB
~107K SLoC