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-propagator

v1.0.0

Published

event-propagator is an extension of the EventEmitter class that will forward specified events to another EventEmitter.

Downloads

8

Readme

event-propagator

This is a small module that provides functions and an extension to the EventEmitter class to provide mechanisms of forwarding events from one emitter to another.

Quickstart

import {propagate} from 'event-propagator';
import {EventEmitter} from 'events';
import {createServer} from 'http';

let server = createServer((req, res) => {
    res.end('Hello, World!');
}).listen(8080);

let myEmitter = new EventEmitter();

propagate(server, myEmitter, 'listening');

myEmitter.on('listening', function() {
    console.log('Listening event propated from server to myEmitter');
});

However, it also allows you to do single propagation that remove the listener automatically after the first event received, and the ability to cancel the propagation of an event by inserting a "middleware" kind of function into the propagation handler.

For example:

//Continued from above
propagate(server, myEmitter, 'connection', (socket) => {
    if(socket.remoteFamily === 'IPv4') {
        return false; //cancels propagation
    }
});

myEmitter.on('connection', function(socket) {
    console.log('New IPv6 connection');
    
    socket.setNoDelay();
});

Of course this is very trivial and not any more useful than a normal event listener. Where event propagation is most useful, however, is in deeply nested structures.

For example, I use this library in another library/program that manages loaded scripts and watches their files for changes. If a script's file changes, then a 'change' event must be propagated upwards to whatever scripts depend on it. It allows bubbling up of events.

API

isPropagating(source: EventEmitter, target: EventEmitter, event: string) -> bool

Returns true if the given event is currently being propagated from the source to the target. As in, it is listening on the source for the given event.

propagate(source: EventEmitter, target: EventEmitter, event: string, onEvent: fn() -> any|false, onlyOnce: bool, context: any)

Will set up a listener on the source emitter to propagate the given event to the target, but only if the onEvent function returns non-false.

if onlyOnce is true, after the first propagation, the listener will remove itself and will not be able to be called again.

context is what onEvent is called with. E.g., onEvent.call(context)

Only one propagation handler is allowed at a time, to avoid doubling up. If you wish to change any of these settings, the propagation handler will need to be removed using stopPropagating and then added back.

onEvent defaults to an empty function, onlyOnce defaults to false, and context defaults to undefined.

propagateOnce(source: EventEmitter, target: EventEmitter, event: string, onEvent: fn() -> any|false, context: any)

A variation of the above where you don't need to specify a new onEvent but will still set onlyOnce to true.

stopPropagating(source: EventEmitter, target: EventEmitter, event: string)

Stops propagating any/all propagators added to source to propagate events to target.

EventPropagator extends EventEmitter

A class that extends EventEmitter from the 'events' module to add the following functions:

  • isPropagatingTo(target: EventEmitter, event: string) -> bool
  • isPropagatingFrom(source; EventEmitter, event: string) -> bool
  • propagateTo(target: EventEmitter, event: string, onEvent: fn() -> any|false, onlyOnce: bool, context: any)
    • Context defaults to this
  • propagateFrom(source: EventEmitter, event: string, onEvent: fn() -> any|false, onlyOnce: bool, context: any)
    • Context defaults to this
  • stopPropagatingTo(target: EventEmitter, event: string)
  • stopPropagatingFrom(source: EventEmitter, event: string)

These are mostly just calls to the normal API given above, with this acting as the source or target depending on the function, but is provided for convenience.

LICENSE

The MIT License (MIT)

Copyright (c) 2015-2016 Aaron Trent

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.