#event #listener #emitter

event-listener-primitives

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

4 releases

0.2.2 Dec 22, 2020
0.2.1 Dec 22, 2020
0.2.0 Dec 22, 2020
0.1.1 Nov 15, 2020
0.1.0 Oct 24, 2020

#169 in Rust patterns

Download history 18/week @ 2020-12-24 8/week @ 2020-12-31 14/week @ 2021-01-07 15/week @ 2021-01-14 19/week @ 2021-01-21 5/week @ 2021-01-28 7/week @ 2021-02-04 39/week @ 2021-02-11 20/week @ 2021-02-18 26/week @ 2021-02-25 11/week @ 2021-03-04 12/week @ 2021-03-11 33/week @ 2021-03-18 76/week @ 2021-03-25 47/week @ 2021-04-01 32/week @ 2021-04-08

71 downloads per month
Used in mediasoup

0BSD license

13KB
170 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};

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

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

    bag.call_simple();
}

Close to real-world usage example:

use event_listener_primitives::{Bag, HandlerId};
fn main() {
    let bag = Bag::default();
    let handler_id = bag.add(Box::new(move || {
        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 {
    bar: Bag<Box<dyn Fn() + Send + Sync + 'static>>,
    closed: BagOnce<Box<dyn FnOnce() + Send + Sync + 'static>>,
}
struct Inner {
    handlers: Arc<Handlers>,
}
impl Drop for Inner {
    fn drop(&mut self) {
        self.handlers.closed.call_simple();
    }
}
#[derive(Clone)]
pub struct Foo {
    inner: Arc<Inner>,
}
impl Foo {
    pub fn new() -> Self {
        let handlers = Arc::<Handlers>::default();
        let inner = Arc::new(Inner { handlers });
        Self { inner }
    }
    pub fn do_bar(&self) {
        // Do things...
        self.inner.handlers.bar.call_simple();
    }
    pub fn do_other_bar(&self) {
        // Do things...
        self.inner.handlers.bar.call(|callback| {
            callback();
        });
    }
    pub fn on_bar<F: Fn() + Send + Sync + 'static>(&self, callback: F) -> HandlerId {
        self.inner.handlers.bar.add(Box::new(callback))
    }
    pub fn on_closed<F: FnOnce() + Send + Sync + 'static>(&self, callback: F) -> HandlerId {
        self.inner.handlers.closed.add(Box::new(callback))
    }
}
fn main() {
    let foo = Foo::new();
    let on_bar_handler_id = foo.on_bar(|| {
        println!("On bar");
    });
    foo
        .on_closed(|| {
            println!("On closed");
        })
        .detach();
    // This will trigger "bar" callback just fine since its handler ID is not dropped yet
    foo.do_bar();
    drop(on_bar_handler_id);
    // This will not trigger "bar" callback since its handler ID was already dropped
    foo.do_other_bar();
    // This will trigger "closed" callback though since we've detached handler ID
    drop(foo);
    println!("Done");
}

The output will be:

On bar
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

~800KB
~20K SLoC