npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

event-relay-emitter

v1.0.0

Published

A replacement of EventEmitter with additional event relay ability

Downloads

2

Readme

EventRelayEmitter


Build Status

Installation

$ npm install event-relay-emitter

Background

This module is inspired by Qt C++ GUI Framework. In Qt, you can do things like this: connect( &obj1, SIGNAL( signal1() ), &obj2, SIGNAL( signal2() ) ). Once the signal1 from obj1 is emitted, the signal2 from obj2 will be emitted as well.

With JavaScript and Node.js, a simple event relay can be done as below:

var EventEmitter = require('events'),
    source = new EventEmitter(),
    target = new EventEmitter();

source.on('sourceEvent', target.emit.bind(target, 'targetEvent');
source.emit('event', param1, param2, ...);

While emitting the 'sourceEvent' from the source, any event handler on the target attached to the 'targetEvent' will also be invoked.

If that's all you need, you can skip the rest of this, but...

  • if you want a whole different parameters to be passed into the relayed event handler
  • if you want an event to be relayed only once

That's why EventRelayEmitter comes into play. I know this module name looks weird, I was thinking something like event-relay, but unfortunately it's been taken, so be it.

Functions

EventRelayEmitter.relay(sourceEvent, target [, options])
EventRelayEmitter.relayOnce(sourceEvent, target [, options])
  • sourceEvent (mandatory): <String> | <Symbol> The source event name.
  • target (mandatory): <EventRelayEmitter> | <EventEmitter> The target that you want the source event to be relayed to.
  • options (optional): <Object>
    • targetEvent (optional): <String> | <Symbol> A new event name that you want the target to emit when the source event is invoked, if this value is omitted, it will use the source event name instead.
    • parameters (optional): <Array> The parameters that will be passed into target event handler, if this is omitted, it will use the same one that the source event is invoked with.

There is no big difference between EventRelayEmitter.relay and EventRelayEmitter.relayOnce, but, with EventRelayEmitter.relayOnce, once a event has been relayed, the corresponding event handler will be removed. The idea comes from here.

Usage

var EventRelayEmitter = require('event-relay-emitter'),
    allen = new EventRelayEmitter(),
    richard = new EventRelayEmitter();

allen.on('greetings', console.log.bind(null, 'allen:'));
allen.relay('greetings', richard);
richard.on('greetings', console.log.bind(null, 'richard:'));
allen.emit('greetings', 'hey');
allen.emit('greetings', 'hello', 'world');

// [console]
// allen: hey
// richard: hey
// allen: hello world
// richard: hello world

As you can see, the 'greetings' event invoked from allen is also relayed to richard. Let's see how EventRelayEmitter.relayOnce works:

var EventRelayEmitter = require('event-relay-emitter'),
    allen = new EventRelayEmitter(),
    richard = new EventRelayEmitter();

allen.relayOnce('greetings', richard);
allen.on('greetings', console.log.bind(null, 'allen:'));
richard.on('greetings', console.log.bind(null, 'richard:'));
allen.emit('greetings', 'hey');
allen.emit('greetings', 'hello', 'world');

// [console]
// richard: hey
// allen: hey
// allen: hello world

There are 2 difference between the first example and the second one

  • after swapping the order of allen.relayOnce(...) and allen.on(...), the output sequence on the console has changed. (richard shown before allen)
  • the 'greetings' event handler on richard is not invoked on the second call to allen.emit(...)

Now you know the rule, let's see how options work:

var EventRelayEmitter = require('event-relay-emitter'),
    allen = new EventRelayEmitter(),
    richard = new EventRelayEmitter(),
    curtis = new EventRelayEmitter();

allen.relayOnce('greetings', richard, { parameters: ['hello', 'world'] });
allen.on('greetings', console.log.bind(null, 'allen:'));
allen.relayOnce('greetings', curtis, { targetEvent: 'cheers', parameters: ['yes', 'baby'] });
richard.on('greetings', console.log.bind(null, 'richard:'));
curtis.on('cheers', console.log.bind(null, 'curtis:'));
allen.emit('greetings', 'hey');

// [console]
// richard: hello world
// allen: hey
// curtis: yes baby

You see, the 'greetings' event came from allen has been redirected to curtis's 'cheers' event handler, also, as richard's 'greetings' event handler been triggered, the corresponding parameters was switched to 'hello' and 'world'.

That's pretty much it, have fun!