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

@dreamworld/event-emitter

v1.1.0

Published

It's library by using we can bind event on any class instance

Downloads

373

Readme

EventEmitter

Use cases

import EventEmitter from  "@dreamworld/event-emitter/event-emitter.js";

//This code simply creates an instance of EventEmitter to be used.
var ee = new EventEmitter();

Adding listeners

A listener is a function that is executed when an event is emitted. You can add them in a multitude of ways, the simplest of which is with the addListener method.

function listener() {
    console.log('The foo event has been emitted.');
}

ee.addListener('foo', listener);

You can also add in bulk using the addListeners method (notice the "s" on the end). You can interact with addListeners in two ways, the first is to pass it an event name and array of listeners to add.

function listener1() {
    console.log('ONE');
}

function listener2() {
    console.log('TWO');
}

ee.addListeners('foo', [listener1, listener2]);

The second way of calling addListeners involves passing an object of event names and listeners. You can either pass a single listener for each event or an array, just as you can see above.

function listener1() {
    console.log('ONE');
}

function listener2() {
    console.log('TWO');
}

function listener3() {
    console.log('THREE');
}

ee.addListeners({
    foo: [listener1, listener2],
    bar: listener3
});

Removing listeners

This works in the exact same way as adding listeners. The only difference is that you replace the add in the method names with remove. Like this:

function listener() {
    console.log('The foo event has been emitted.');
}

ee.addListener('foo', listener);
ee.removeListener('foo', listener);

If you want a listener to remove itself after it has been called or after a condition has been met then all you need to do is return true.

function listener1() {
    // If a condition is met then remove the listener
    if(completed) {
        return true;
    }
}

function listener2() {
    // Always remove after use
    return true;
}

ee.addListeners('foo', [listener1, listener2]);
ee.emitEvent('foo');

If you do not want to, or can't for some reason, return true, you can set the return value using setOnceReturnValue.

function listener() {
    // Always remove after use
    return 'REMOVE-ME';
}

ee.addListener('foo', listener);
ee.setOnceReturnValue('REMOVE-ME');
ee.emitEvent('foo');

Alternatively you can use the addOnceListener method, or it's alias, once.

function listener() {
    // Do stuff
}

ee.addOnceListener('foo', listener);
ee.emitEvent('foo');
// The listener will be removed now...
ee.emitEvent('foo');

// The listener will only be executed once.

You can also remove whole events and all of their attached listeners with the removeEvent method. If you pass an event name to the method then it will remove that event and it's listeners.

function listener1() {
    console.log('ONE');
}

function listener2() {
    console.log('TWO');
}

ee.addListeners('foo', [listener1, listener2]);
ee.removeEvent('foo');

However, if you leave it blank and do not pass an event name, then all events will be removed. It will wipe everything.

Fetching the listeners

If you really need to then you can get an array of all listeners attached to an event with the getListeners method.

function listener1() {
    console.log('ONE');
}

function listener2() {
    console.log('TWO');
}

ee.addListeners('foo', [listener1, listener2]);
ee.getListeners('foo');

Emitting events

So once you have added your listeners and you are ready to start executing them, you can start to use the emitEvent method. At it's most basic level it will just execute all listeners attached to an event.

function listener1() {
    console.log('ONE');
}

function listener2() {
    console.log('TWO');
}

ee.addListeners('foo', [listener1, listener2]);
ee.emitEvent('foo');

For more control, you can pass an arguments array as the second argument. This array will be applied to every listener as individual arguments.

function adder(a, b) {
    console.log(a + b);
}

ee.addListener('addStuff', adder);
ee.emitEvent('addStuff', [10, 20]);

Method aliases

Hebo, from GitHub, contributed three aliases to add, remove and emit. The aliases can be found in the API documentation but here is the mapping.

  • on - addListener
  • off - removeListener
  • trigger - emitEvent

I've also added one since then.

  • once - addOnceListener

Using regular expressions

You can pass a regular expression to pretty much every function in EventEmitter in place of an event name string. So if you have two events, say, bar and baz, you can manipulate both of them with /ba[rz]/.

This applies to adding, removing and emitting events as well as a few others. There is one thing you have to remember when using this though, you must have the event defined before you use it in a regex.

By defined I mean, it has to have some other listener added to it, or you have to explicitly define it with the defineEvent / defineEvents functions. Here is an example of defining some events and adding to both with a regular expression.

ee.defineEvents(['bar', 'baz']);
ee.addListener(/ba[rz]/, function () {
    console.log('Now you are thinking with regular expressions.');
});
ee.emitEvent(/ba[rz]/);

Extending with the EventEmitter class

You probably won't want to use EventEmitter as a raw class. You will probably want to write a Player class or something like that and implement EventEmitter's methods into it. To do this you will need to clone and merge EventEmitter's prototype object into your classes prototype object.

class Player(){}

Object.assign(Player.prototype, EventEmitter.prototype);