#events #mediator #event-manager #eventmediator

mediator-sys

强类型、可扩展的事件中介

1 个稳定版本

2.0.2 2022年11月4日
2.0.0 2022年10月28日
1.0.0 2022年10月6日
0.1.0 2022年9月29日

#5#mediator

MIT/Apache

46KB
660

mediator-sys

Latest Release Documentation

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

警告

该包已被重命名,并即将淘汰。请使用mediatrix代替。

用法

同步

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功能)
  • CxAwareMediator(使用async功能,携带您选择的依赖项)
  • 编译器烘焙类型
  • 可扩展架构

待办事项

  • 移除对事件EvClone约束,以供SyncMediatorInternalNext使用。
  • 内部,以“累加”方式使构建器功能。

贡献

请随时提交一个问题/PR,解释可能的改进或更改。

帮助

此外,当需要时,请不要犹豫,提交一个问题。我很乐意提供帮助!

依赖项

~0–9MB
~78K SLoC