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

onall

v2.0.0

Published

A helper for the event emitter that allows to only be fired when all events of a given type have been fired

Downloads

14

Readme

onall

npm version Build Status Coverage Status Dependency Status

This is a super simple set of helper functions that allow you to react on more complex events from an event emitter. Events can be bundled and processed when either all of them or any of them fire. There are different modes for how you want to handle repeat events.

This library is written in ECMA6 for node.js and currently not compatible with previous versions or browsers.

Installation

npm install --save onall

API

Here are some simple examples on how to use the API. For more details take a look at the test cases.

On.all()

On.all({string[]} events, {function} callback, {boolean, optional} useFirst)

React as soon as all registered events have been fired at least once (and then reset). If an event is fired before all other events are fired, the arguments are overwritten, unless useFirst is set to true in which case the first argument is used and subsequent arguments are discarded.

var On = require('onall');
var emitter = new events.EventEmitter();
var on = new On(emitter);
on.all(['event1', 'event2'], args => {
    console.log(args);
});
emitter.emit('event1', 'arg1');
emitter.emit('event1', 'arg3');
emitter.emit('event2', 'arg2');
 // => { event1: ['arg3'], event2: ['arg2'] }

On.allCached()

On.allCached({string[]} events, {function} callback, {number, optional} cacheLimit, {boolean, optional} lifo)

React as soon as all registered events have been fired at least once (and then reset). Other than the standard On.all() method this one will queue up events and not discard duplicate events. An optional limit to the size of the cache can be passed set which will discard the oldest partial entries. The limited cache can discard oldest entries first (default: lifo = false) or newest first (lifo = true).

var On = require('onall');
var emitter = new events.EventEmitter();
var on = new On(emitter);
on.allCached(['event1', 'event2'], args => {
    console.log(args);
});
emitter.emit('event1', 'arg1');
emitter.emit('event1', 'arg3');
emitter.emit('event2', 'arg2');
// => { event1: ['arg1'], event2: ['arg2'] }
emitter.emit('event2', 'arg4');
 // => { event1: ['arg3'], event2: ['arg4'] }

On.allOnce()

On.allOnce({string[]} events, {function} callback, {boolean, optional} useFirst)

Will react as soon as all events have been fired at least once and then no longer listen to events. Only the first event is recorded so that subsequent events will be ignored.

var On = require('onall');
var emitter = new events.EventEmitter();
var on = new On(emitter);
on.allOnce(['event1', 'event2'], args => {
    console.log(args);
});
emitter.emit('event1', 'arg1');
emitter.emit('event2', 'arg2');
// => { event1: ['arg1'], event2: ['arg2'] }
emitter.emit('event1', 'arg3');
emitter.emit('event2', 'arg4');
// => ignored

On.allMany()

On.allOnce({string[]} events, {number} count, {function} callback, {boolean, optional} useFirst)

Will react as soon as all events have been fired at least once and then no longer listen to events. Only the configured number of events is recorded so that subsequent events will be ignored.

var On = require('onall');
var emitter = new events.EventEmitter();
var on = new On(emitter);
on.allMany(['event1', 'event2'], 2, args => {
    console.log(args);
});
emitter.emit('event1', 'arg1');
emitter.emit('event2', 'arg2');
// => { event1: ['arg1'], event2: ['arg2'] }
emitter.emit('event1', 'arg3');
emitter.emit('event2', 'arg4');
// => { event1: ['arg3'], event2: ['arg4'] }
emitter.emit('event1', 'arg5');
emitter.emit('event2', 'arg6');
// => ignored

On.any()

On.any({string[]} events, {function} callback)

Will react as soon as any of the given events are triggered.

var On = require('onall');
var emitter = new events.EventEmitter();
var on = new On(emitter);
on.any(['event1', 'event2'], (event, arg) => {
    console.log(event, arg);
});
emitter.emit('event1', 'arg1');
=> 'event1', 'arg1'
emitter.emit('event2', 'arg2');
=> 'event2', 'arg2'

On.anyOnce()

On.anyOnce({string[]} events, {function} callback)

Will react as soon as any of the given events are triggered, and then stops listening.

var On = require('onall');
var emitter = new events.EventEmitter();
var on = new On(emitter);
on.anyOnce(['event1', 'event2'], (event, arg) => {
    console.log(event, arg);
});
emitter.emit('event1', 'arg1');
=> 'event1', 'arg1'
emitter.emit('event2', 'arg2');
=> ignored

On.anyMany()

Will react as soon as a given number of events are triggered, and then stops listening.

var On = require('onall');
var emitter = new events.EventEmitter();
var on = new On(emitter);
on.anyMany(['event1', 'event2'], 2, (event, arg) => {
    console.log(event, arg);
});
emitter.emit('event1', 'arg1');
=> 'event1', 'arg1'
emitter.emit('event2', 'arg2');
=> 'event2', 'arg2'
emitter.emit('event1', 'arg3');
=> ignored

Tests

To run the tests simply run npm test

Bugs / Feature Requests / Questions

If you have any of those you can file a ticket on Github or send me an email to [email protected]