4 个版本

0.3.7 2024年5月14日
0.3.6 2024年2月7日
0.3.5 2024年1月30日
0.3.3 2023年12月21日
0.2.0 2023年7月7日

#752 in 异步

Download history 132/week @ 2024-05-14 14/week @ 2024-05-21 4/week @ 2024-05-28 1/week @ 2024-06-04 32/week @ 2024-07-02 109/week @ 2024-07-30

每月 109 次下载

MIT 许可证

30KB
513

Orsomafo

Orsomafo 是一个用于 Rust 应用的事件调度器

示例(长方法)

use orsomafo::{Dispatchable, DispatchedEvent, EventDispatcherBuilder};
use tokio::time::{sleep, Duration};

// Event must be
// - serializable
// - deserializable
// - cloneable
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] // Event must be cloneable
struct MyEvent;

impl orsomafo::Dispatchable for MyEvent {} // MyEvent is now dispatchable

// create an event handler
// event handler must implement Default
#[derive(Default)]
struct MyEventHandler;

#[orsomafo::async_trait]
impl orsomafo::EventHandler for MyEventHandler {
    // called when event from "MyEvent" is dispatched
    async fn handle(&self, dispatched: &DispatchedEvent) {
        let event: MyEvent = dispatched.the_event().unwrap(); // Get the instance of "MyEvent"
        println!(">>>>> handled my event: {:#?}", event);
    }
}

#[tokio::main]
async fn main() {
    _ = EventDispatcherBuilder::new()
        .listen::<MyEvent, MyEventHandler>()
        .build()
        .await;

    let event = MyEvent;
    event.dispatch_event();

    // The following line is use to pause the application for
    // few milliseconds. This will allow us to handle all dispatched events.
    // In a full application, this line wil not be require.
    sleep(Duration::from_millis(100)).await;
}

示例(短方法)

use orsomafo::{Dispatchable, DispatchedEvent};
use tokio::time::{sleep, Duration};

// Event must be
// - serializable
// - deserializable
// - cloneable
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
struct MyEvent;

impl orsomafo::Dispatchable for MyEvent {} // MyEvent is now dispatchable

// create an event handler
// event handler must implement Default
#[derive(Default)]
struct MyEventHandler;

#[orsomafo::async_trait]
impl orsomafo::EventHandler for MyEventHandler {
    // called when event from "MyEvent" is dispatched
    async fn handle(&self, dispatched: &DispatchedEvent) {
        let event: MyEvent = dispatched.the_event().unwrap(); // Get the instance of "MyEvent"
        println!(">>> handled my event: {:#?}", event);
    }
}

#[tokio::main]
async fn main() {
    MyEvent::subscribe::<MyEventHandler>().await;

    let event = MyEvent;
    event.dispatch_event();

    // The following line is use to pause the application for
    // few milliseconds. This will allow us to handle all dispatched events.
    // In a full application, this line wil not be require.
    sleep(Duration::from_millis(100)).await;
}

示例

示例文件夹包含简单和完整的示例。如果这些示例都不适用,请提供您的用例,我将尝试提供一个。

反馈

如果您觉得这个包很有用,请为仓库评分。提交您的问题和建议。

许可证

MIT 许可证(MIT)

任何人免费获得本软件及其相关文档副本(“软件”),可以不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供者提供软件的人员这样做,前提是以下条件

上述版权声明和本许可声明应包含在软件的任何副本或实质性部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的还是隐含的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,源自、因之产生或与此软件或软件的使用或其他操作有关。

依赖关系

~4.5–7.5MB
~125K SLoC