Code Monkey home page Code Monkey logo

simple-event-bus's Introduction

Simple Event Bus

Minimal event bus in rust. It provides a basic event bus system that works with (probably) any type.

Example Usage:

use simple_event_bus::{Event, EventBus, Subscriber};

struct ExampleSubscriber {
    pub name: String,
}

impl ExampleSubscriber {
    pub fn new(name: String) -> ExampleSubscriber {
        ExampleSubscriber { name }
    }
}

impl Subscriber for ExampleSubscriber {
    type Input = String;

    fn on_event(&mut self, event: &Event<Self::Input>) {
        println!("{} received message: {}", self.name, event.get_data());
    }
}

fn main() {
    let mut event_bus = EventBus::new();

    // We have to manually create and add each subscriber to the event bus.
    event_bus.subscribe_listener(ExampleSubscriber::new("Subscriber 1".to_string()));
    event_bus.subscribe_listener(ExampleSubscriber::new("Subscriber 2".to_string()));

    // We can manually define an event and publish it to the event bus.
    event_bus.publish(Event::new("hello".to_string()));
    event_bus.publish(Event::new("world".to_string()));

    // Alternatively, we can use the From trait to define an event and publish it to the event bus.
    // This is as simple as using ".into()" on the data (so long as it matches the event bus's type parameter).
    event_bus.publish("!".to_string().into());

    // Here we show another example, this time directly from a String instead of using &str.
    let test_string = String::from("Hello, World!");
    event_bus.publish(test_string.into());

    // Runs through each event, and calls each listener's on_event method.
    event_bus.run();
}

Feel free to fork this implementation to add your own features!

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.