#eda #event-driven #events #rollback

rs-eda

rust EDA event-driven framework

7 releases

Uses new Rust 2024

new 0.1.6 Mar 29, 2025
0.1.5 Mar 29, 2025

#6 in #event-driven

Download history

90 downloads per month

MIT license

21KB
319 lines

Framework introduction

EDA development framework, namely: interactive event, function componentization, logic strategy. In other words, a project is composed of multiple businesses, each business has multiple functions (components), multiple logic (policies), and multiple interaction events to complete

Example

use rs_eda::{engine::Engine, plugin::PluginOptions, strategy::StrategyOptions};
use serde_json::{Value, json};

fn main() {
    let mut engine = Engine::new();
    engine.install(APlugin::new()).unwrap();
    engine.install(BPlugin).unwrap();
    engine.exec(AStrategy).unwrap();

    engine.rollback("AStrategy");
    engine.uninstall("BPlugin");

    let a_func = |x| println!("--a--{:?}", x);
    let b_func = |x| println!("--b--{:?}", x);
    let c_func = |x| println!("--c--{:?}", x);
    let d_func = |x| println!("--d--{:?}", x);
    // Event listening, not manually moved, will always exist
    engine.event.on("a-event", a_func);
    // One-time monitor, not implemented
    engine.event.once("b-event", b_func);
    // Throttle monitoring, not implemented
    engine.event.on_throttle("c-event", c_func, 2000);
    // Anti-shake monitor, not implemented
    engine.event.on_anti_shake("d-event", d_func, 2000);


    // There is a data trigger event
    engine.event.emit("a-event", Some(json!("123")));
    // No data triggered event
    engine.event.emit("c-event", None);
    engine.event.emit("c-event", None);
    engine.event.emit("d-event", None);

    // Remove the b_func method from the a-event event
    engine.event.off("a-event", Some(b_func));
    // Remove the whole b-event event
    engine.event.off("b-event", None);
}

struct APlugin {
    title: String,
}
impl APlugin {
    fn new() -> Self {
        Self {
            title: "".to_string(),
        }
    }
}
impl PluginOptions for APlugin {
    fn name(&self) -> &str {
        "APlugin"
    }
    fn deps(&self) -> Vec<&str> {
        vec![]
    }
    fn get_data(&self) -> Option<Value> {
        Some(serde_json::json!({
            "title": self.title
        }))
    }
    fn install(&mut self, engine: &mut Engine) {
        self.title = "camera".to_string();
        println!("install APlugin: {}", self.title);
    }
    fn dispose(&mut self, engine: &mut Engine) {
        println!("dispose APlugin");
    }
}

struct BPlugin;
impl PluginOptions for BPlugin {
    fn name(&self) -> &str {
        "BPlugin"
    }
    fn deps(&self) -> Vec<&str> {
        vec!["APlugin"]
    }
    fn get_data(&self) -> Option<Value> {
        None
    }
    fn install(&mut self, engine: &mut Engine) {
        let data = engine.plugin_data("APlugin");
        println!("install BPlugin: {:?}", data);
    }
    fn dispose(&mut self, engine: &mut Engine) {
        println!("dispose BPlugin");
    }
}
struct AStrategy;
impl StrategyOptions for AStrategy {
    fn name(&self) -> &str {
        "AStrategy"
    }
    fn condition(&self) -> Vec<&str> {
        vec!["APlugin", "BPlugin"]
    }
    fn get_data(&self) -> Option<Value> {
        None
    }
    fn exec(&self, engine: &mut Engine) {
        let data = engine.plugin_data("APlugin");
        println!("exec AStrategy: {:?}", data);
    }
    fn rollback(&mut self, engine: &mut Engine) {
        println!("rollback AStrategy");
    }
}

Unrealized needs, hope everyone help

1, one-time trigger (once)
link: https://docs.rs/rs-eda/0.1.5/src/rs_eda/event/mod.rs.html#139

2, throttle trigger (on_throttle)
link: https://docs.rs/rs-eda/0.1.5/src/rs_eda/event/mod.rs.html#248

3, anti-shake trigger (on_anti_shake)
link: https://docs.rs/rs-eda/0.1.5/src/rs_eda/event/mod.rs.html#258

Dependencies

~1.6–2.9MB
~53K SLoC