Giter Site home page Giter Site logo

rs-event-emitter's Introduction

Event System

A simple rs-event-emitter implementation in Rust.

The rs-event-emitter allows you to register event handlers for specific events and emit events with associated data. It provides a way to decouple components and enable communication through events.

Currently, the parameters only support types that have implemented cloning. Improvements will be made in the future.

Usage

To use the event system, follow these steps:

  1. Add the following to your Cargo.toml file:
[dependencies]
rs-event-emitter = "0.0.5"
  1. Import the crate in your main.rs or lib.rs file:
extern crate rs_event_emitter::*;
  1. Create an event handler function:
// Define the event handlers
let handler = EventHandler::new(Box::new(move |val: i32| {
    println!("Handler called with value: {}", val);
    let mut data = data_clone.lock().unwrap();
    data.called = true;
    data.value = val;
}));

// if you have more parameters, you can use a tuple
let handler = EventHandler::new(Box::new(move |(a, b, c): (i32, &str, f64)| {
    println!("event1: {}, {}, {}", a, b, c);
    assert_eq!(a, 42);
    assert_eq!(b, "hello");
    assert_eq!(c, 3.14);
}));
  1. Register the event handler for a specific event:
let emitter = EventEmitter::new();

// Register the event handlers
emitter.on("test_event", &handler);
  1. Emit an event:
emitter.emit("test_event", Box::new(24));
  1. Unregister event handlers using the EventEmitter::off method:
emitter.off("test_event", &handler);

API

EventEmitter

  • fn new() -> Self: Creates a new EventEmitter instance.

  • fn on(&self, event: &'static str, handler: Arc<dyn Handle>): listen an event handler for a specific event.

  • fn off(&self, event: &str, handler: Arc<dyn Handle>): unListen an event handler for a specific event.

  • fn emit(&self, event: &str, data: Box<dyn Any>): Emits an event with associated data, triggering the registered event handlers for that event.

EventHandler

  • fn new(handler: fn(T)) -> Self: Creates a new event handler with the provided closure.

Examples

use rs_event_emitter::*;

fn main() {
    let data = Arc::new(Mutex::new(TestData {
        called: false,
        value: 0,
    }));
    let data_clone = data.clone();

    let handler = EventHandler::new(Box::new(move |val: i32| {
        println!("Handler called with value: {}", val);
        let mut data = data_clone.lock().unwrap();
        data.called = true;
        data.value = val;
    }));

    let emitter = EventEmitter::new();

    // Register the event handler
    emitter.on("test_event", &handler);

    let clone_emitter = emitter.clone();

    let t_handler = thread::spawn(move || {
        thread::sleep(std::time::Duration::from_millis(100));
        clone_emitter.emit("test_event", Box::new(42));
    });

    t_handler.join().unwrap();

    {
        let data = data.lock().unwrap();

        println!("{:?}", data.called);
        // Check if the handler was called and the value was set correctly
        assert!(data.called);
        assert_eq!(data.value, 42);
    }

    // Remove the event handler
    emitter.off("test_event", &handler);

    // Reset the test data
    {
        let mut data = data.lock().unwrap();
        data.called = false;
        data.value = 0;
    }

    // Emit the event again
    emitter.emit("test_event", Box::new(24));

    // Verify the handler was not called after being removed
    let data = data.lock().unwrap();
    assert!(!data.called);
    assert_eq!(data.value, 0);
}

When you run the above code, you should see the event handlers being called for the emitted events.

Contributing

If you would like to contribute to this project, please open an issue or submit a pull request.

License

This project is licensed under the MIT License

rs-event-emitter's People

Contributors

hui200102 avatar shcarp avatar

Stargazers

 avatar Roland Poulter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.