#event-listener #events #listener #emitter

event-listener-primitives

Low-level primitive for building Node.js-like event listeners

7 releases (3 stable)

2.0.1 Sep 2, 2021
2.0.0 Aug 30, 2021
1.0.0 Jun 13, 2021
0.2.2 Dec 22, 2020
0.1.0 Oct 24, 2020

#746 in Rust patterns

Download history 551/week @ 2023-12-14 512/week @ 2023-12-21 198/week @ 2023-12-28 417/week @ 2024-01-04 194/week @ 2024-01-11 379/week @ 2024-01-18 440/week @ 2024-01-25 437/week @ 2024-02-01 288/week @ 2024-02-08 508/week @ 2024-02-15 527/week @ 2024-02-22 749/week @ 2024-02-29 649/week @ 2024-03-07 732/week @ 2024-03-14 385/week @ 2024-03-21 268/week @ 2024-03-28

2,099 downloads per month
Used in 3 crates (2 directly)

0BSD license

18KB
424 lines

Event listener primitives

Build Status Crates.io Docs License

This crate provides a low-level primitive for building Node.js-like event listeners.

The 3 primitives are Bag that is a container for Fn() event handlers, BagOnce the same for FnOnce() event handlers and HandlerId that will remove event handler from the bag on drop.

Trivial example:

use event_listener_primitives::{Bag, HandlerId};
use std::sync::Arc;

fn main() {
    let bag = Bag::default();

    let handler_id = bag.add(Arc::new(|| {
        println!("Hello");
    }));

    bag.call_simple();
}

Close to real-world usage example:

use event_listener_primitives::{Bag, BagOnce, HandlerId};
use std::sync::Arc;

#[derive(Default)]
struct Handlers {
    action: Bag<Arc<dyn Fn() + Send + Sync + 'static>>,
    closed: BagOnce<Box<dyn FnOnce() + Send + 'static>>,
}

pub struct Container {
    handlers: Handlers,
}

impl Drop for Container {
    fn drop(&mut self) {
        self.handlers.closed.call_simple();
    }
}

impl Container {
    pub fn new() -> Self {
        let handlers = Handlers::default();

        Self { handlers }
    }

    pub fn do_action(&self) {
        // Do things...

        self.handlers.action.call_simple();
    }

    pub fn do_other_action(&self) {
        // Do things...

        self.handlers.action.call(|callback| {
            callback();
        });
    }

    pub fn on_action<F: Fn() + Send + Sync + 'static>(&self, callback: F) -> HandlerId {
        self.handlers.action.add(Arc::new(callback))
    }

    pub fn on_closed<F: FnOnce() + Send + 'static>(&self, callback: F) -> HandlerId {
        self.handlers.closed.add(Box::new(callback))
    }
}

fn main() {
    let container = Container::new();
    let on_action_handler_id = container.on_action(|| {
        println!("On action");
    });
    container
        .on_closed(|| {
            println!("On container closed");
        })
        .detach();
    // This will trigger "action" callback just fine since its handler ID is not dropped yet
    container.do_action();
    drop(on_action_handler_id);
    // This will not trigger "action" callback since its handler ID was already dropped
    container.do_other_action();
    // This will trigger "closed" callback though since we've detached handler ID
    drop(container);

    println!("Done");
}

The output will be:

On action
On closed
Done

Contribution

Feel free to create issues and send pull requests, they are highly appreciated!

License

Zero-Clause BSD

https://opensource.org/licenses/0BSD

https://tldrlegal.com/license/bsd-0-clause-license

Dependencies

~0.5–0.8MB
~13K SLoC