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

squee

v1.1.0

Published

Super quick event emitter!

Downloads

1,164

Readme

Squee!

Build Status npm version Downloads

💨✨ Super Quick Event Emitters! ✨💨

Squee lets you create a hub for triggerable application events your components need to fire and/or listen to. Event emitters provide both traditional Node-based .on() and Promise-based waitFor hooks.

No dependencies. Tiny size. Easy breezy.

Usage

When in Node or bundled environments like Browserify or Webpack, import from "squee" directly:

import { EventEmitter } from "squee";

const emitter = new EventEmitter();

emitter.on("noise", sound => console.log(`${sound}!`));
emitter.emit("noise", "MOO"); // "MOO!"

EventEmitter is also exported under the name Squee.

Squee also ships with dist/(amd|system)-(es3|es2015).js files. So, to use a version that works in all browsers with RequireJS, use dist/amd-es3.js.

Examples

Emitting an event every seconds for ten seconds:

import { EventEmitter } from "squee";

const emitter = new EventEmitter();
const listener = sound => console.log(`${sound}!`);

emitter.on("noise", listener);

setInterval(
    () => emitter.emit("noise", "MOO"),
    1000);

setTimeout(
    () => emitter.off("noise", listener),
    10000);

Waiting for events with Promises:

import { EventEmitter } from "squee";

const emitter = new EventEmitter();

emitter.emit("noise", "Warm it up");

emitter.waitFor("noise")
    .then(sound => console.log(`Later: ${sound}!`));

emitter.emit("noise", "All nine thousand taste buds");

emitter.waitForFirst("noise")
    .then(sound => console.log(`First: ${sound}!`));
Later: All nine thousand taste buds!
First: Warm it up!

Usage with TypeScript

Good news: Squee is written in TypeScript! You'll never have to worry about @types mismatches here!

EventEmitters may optionally specify a templated interface or type mapping event name keys to their expected argument type. Very snazzy.

import { EventEmitter } from "squee";

interface IEventEmissions {
    noise: string;
    taste: number;
}

const emitter = new EventEmitter<IEventEmissions>();

emitter.emit("noise", "MOO");
emitter.emit("taste", 9000);

// These will give compiler errors:
emitter.emit("unknown");
emitter.emit("noise", true);

Squee also exports an IEventReceiver interface that contains all but the emit operation and an IEventSubmitter interface that only contains the emit operation. Use the if you'd like to restrict which application components may send or receive events. EventEmitter implements the IEventReceiver and IEventSubmitter interfaces.

Note that until TypeScript supports variadic kinds (issue here), only one type is supported for all arguments. If you need complex objects it's probably semantically more clear to pass an object with multiple fields anyway.

API

on

Binds an event listener to an event name.

Parameters:

  • eventName: string
  • listener: (...args: any[]) => void

off

Removes an event listener from an event name. If no listener is provided, it removes all listeners for that event name.

Parameters:

  • eventName: string
  • listener?: (...args: any[]) => void

Throws an error if the listener wasn't added for that event name.

onAny

Binds an event listener to all events. onAny listeners are fired after all events and receive the event name followed by any event arguments.

Parameters:

  • listener: (eventName: string, ...args: any[]) => void

emit

Emits an event, along with any amount of additional information.

Parameters:

  • eventName: string
  • ...args: any[]

waitFor

Creates a Promise to be resolved the next time an event is fired. The Promise is resolved with the args passed with the event.

Parameters:

  • eventName: string

waitForFirst

Creates a Promise to be resolved once an event has fired. If the event was already fired, it resolves immediately. The Promise is resolved with the args passed with the first event of that name.

Parameters:

  • eventName: string

Comparison with event-emitter

event-emitter is a very popular package used in many other npm packages. However, it has two dependencies (es5-ext and d), the size of which are concerning for performance-critical applications.