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

co-emitter

v0.2.3

Published

Emitter that works with generator methods using co

Downloads

41

Readme

co-emitter

co-powered emitter mixin for objects Build Status

Installation

npm install co-emitter

API

Emitter(obj)

The Emitter may be used as a mixin or a standalone object.

Standalone:

var Emitter = require('co-emitter');
var emitter = new Emitter();

emitter.on('say', function *(message) {
  yield delay(200);
  console.log("I say: " + message);
  return true;
});

co(function *() {
  var result = yield emitter.emit('say', 'hello')
});
// result == true, and console.logs "I say: hello""

Mixin:

var user = { name: 'Tobi' };
Emitter(user);
yield user.emit('some event');

Prototype mixin:

var User = function() {
  this.name = 'Tobi';
};

Emitter(User.prototype);

var user = new User();
yield user.emit('some event');

Emitter#on(name, fn*, [...moreFns*])

Registers generator as a listener for name.

fn* can be:

  • a generator function
  • an array of multiple generator functions (will be run in the order passed in)
var emitter = new Emitter();
var listenerA = function *() {},
    listenerB = function *() {},
    listenerC = function *() {};

// All of these would be equal

emitter.on('test', listenerA);
emitter.on('test', listenerB);
emitter.on('test', listenerC);

// or

emitter.on('test', [listenerA, listenerB, listenerC]);

// or

emitter.on('test', listenerA, listenerB, listenerC);

Emitter#once(name, fn*, [...moreFns*])

Registers generator as a listener for name. This generator runs once and is then removed.

var emitter = new Emitter();
emitter.once('test', function*() { console.log("Yay!"); });
yield emitter.emit('test');

Emitter#emit(event, ...args)

Runs the registered listeners for event. Passes in optional args to the listeners.

args can be an array, or multiple arguments.

Returns the result of the last run listener. If results are omitted, returns original arguments.

var result = yield emitter.emit('add', 2, 2);
console.log(result) // 4
Async and Sync Sync Events

If you want an event to be synchronous, and not have to yield to emitter.emit, you can do so by using normal functions for your listeners.

var callCount = 0;
emitter.on('call', function() {
  callCount++;
});

emitter.emit('call');
emitter.emit('call');

If you wan't to wait for async operations to have finished, you may do so by using yield within generators.

emitter.on('saving', function *() {
  userStatistics.saveCount += 1;
  yield userStatistics.save();
});

// Elsewhere...
yield emitter.emit('saving'); // wait for `saving`'s async to have finished.

You may not use both generators and normal functions as listeners for a single event. If you need async and sync for a single event, use a non-yielding generator for the sync.

emitter.on('saving', function*() {
  userStatistics.saveCount += 1;
});

emitter.on('saving', function *() {
  yield userStatistics.save();
});

// Elsewhere...
yield emitter.emit('saving'); // wait for `saving`'s async to have finished.

Because we are using one async function (or a generator), we must use yield when calling emit.

Emitter chaining

Listeners run with a middleware pattern, results from one listener can be piped into the next. For example:

var emitter = new Emitter();

var uppercase = function *(str) {
  return str.toUpperCase();
};

var firstChar = function *(str) {
  return str.charAt(0)
};

emitter.on('upperFirst', uppercase, firstChar);

var result = yield emitter.emit('upperFirst', 'hello world');
// result == 'H'

If you do not return a value, it will use the initial arguments passed in/ the value of the last returning listener.

Emitter#off([event], [listener])

Removes a given listener for a specific listener.

  • If no event is provided, all listeners will be removed.
  • If no listener is provided, all listeners for given event will be removed.
  • If event and listener provided, removes listener from event

Emitter#removeAllListeners

Convenience method to call off with no event or listener, thereby removing all listeners.

Emitter#hasListeners(event)

Returns whether or not any listeners exist for event.

Emitter#listeners(event)

Returns an array of all listeners for a given event. If no listeners exist, returns an empty array.