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

onemit

v2.2.0

Published

Event emitter

Downloads

16

Readme

OnEmit

Event emitter that returns the event on emit [ .on() / .emit() / .emitAsync().then() ]

Build Status Dependencies devDependencies

Take control of your events!

This is a vanilla JS event system.

As oposed to "emit-and-forget" systems, this library allows you to do stuff after all event listeners have been executed.

Usage

Include the library - it is an UMD module, works in Browser and Node.js.

var OnEmit = require('emitter');

OnEmit instance:

var emitter = new OnEmit;

// Add event listeners
emitter.on('anything', function (event, obj, str, num) {
   // event instanceof OnEmit.Event -> true
   // ...
   return value;
});

// Emit events

// full version
var event = emitter.emit(new OnEmit.Event('anything'), {"with": "custom arguments"}, ...);

// object event
emitter.emit({type: 'anything', data: ["any type"]}, ...);
// short version
emitter.emit('anything', ...);

console.log(event); // -> { type: "anything", timeStamp: 1453059701092, result: [return1, return2, ...] }

// Remove event listeners
emitter.off('anything');

As a mixin:

The OnEmit may also be used as a mixin. For example a "plain" object may become an emitter,

var user = { name: 'dima' };
OnEmit(user);
user.on('im a user', function (event){ /*...*/ })
var event = user.emit('im a user');

As a prototype mixin:

You may extend an existing prototype.

OnEmit(User.prototype);
var emitter = new User;
var event = emitter.emit('im a user as prototype method');

Bind emitter to an object

var emitter = new OnEmit;
var user = { name: 'dima' };
emitter.bind(user);
user.on(...);
var event = user.emit('im a user');

Register an event handler fn.

emitter.on(event, fn);
  • event should be an event name (String), or "*" to catch all events.
  • fn is an event handler of form function fn(emittedEvent, ...) { /*...*/ return value; }.
    • emittedEvent is instance of OnEmit.Event.
    • The returned value goes into emittedEvent.result[idx]. value could be a promise (used with .emitAsync()).

Register an event handler fn only once.

emitter.only(event, fn);

Register a single-shot event handler fn

removed immediately after it is invoked the first time.

emitter.once(event, fn);

Wait for event with a Promise.

emitter.when(event).then((event => {
    let { args } = event;
    // ...
}));

// reject after 10 seconds
emitter.when(event, 10e3).catch((error) => {
    console.log(error.type, error.message);
});

Remove event listener(s)

emitter.off(event, fn);
  • Pass event and fn to remove a listener.
  • Pass event to remove all listeners on that event.
  • Pass nothing to remove all listeners on all events.

Emit an event with variable option args.

var event = emitter.emit(event, ...);

event argument can be either an event name (String), event properties (Object) including at least .type property, or an OnEmit.Event instance.

.emit() returns an OnEmit.Event instance, which contains an array .result of whatever event listeners have returned.

Event handlers can manipulate event properties before return.

Emit an event asynchronously.

emitter.emitAsync(event, ...).then(function (event){
    // do something after all events have fired
    console.log(event.result);
});

emitAsync uses OnEmit.setImmediate or setImmediate, if available, or setTimeout otherwise.

Emit an event after delay milliseconds.

emitter.emitAfter(delay, event, ...).then(function (event){
    // do something after all events have fired
    console.log(event.result);
});

emitAfter uses OnEmit.setTimeout or setTimeout.

Get the array of callbacks

emitter.listeners(event);

Check if this emitter has event handlers.

emitter.hasListeners(event);

Check if this OnEmit has a specific event handler fn.

emitter.hasListener(event, fn);

The special event * listens on all events.

emitter.on('*', fn);
emitter.emit('foo'); // -> fn('foo');
emitter.emit('bar'); // -> fn('bar');
emitter.off('*', fn);

License

MIT