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

@foxify/events

v2.1.0

Published

A high performance EventEmitter alternative for Node.js and browser

Downloads

349

Readme

Events

@foxify/events is a EventEmitter alternative for Node.js and browser that has been optimized for better performance compared to the native version.

Build Status Coverage Status NPM Version npm bundle size (minified) npm bundle size (minified + gzip) NPM Monthly Downloads NPM Total Downloads Dependencies Status Open Issues Pull Requests License

This module is API compatible with the EventEmitter that ships by default with Node.js but there are some slight differences:

  • The newListener and removeListener events have been removed as they are useful only in some uncommon use-cases.
  • The setMaxListeners and getMaxListeners methods are not available.
  • Support for custom context for events so there is no need to use bind.
  • Support for strict events in TypeScript.

Table of Contents

Installation

npm i @foxify/events

Usage

JavaScript:

const EventEmitter = require("@foxify/events").default;

TypeScript:

import EventEmitter from "@foxify/events";

For the API documentation, please follow the official Node.js documentation.

Strict events

"error" event is always defined by default because of its different behavior

first create events type (optional)

type Events = {
  foo: (bar: string) => void;
  withContextEnforcement: (this: number, bar: number) => void;
}

then create a new direct/extended instance

const eventEmitter = new EventEmitter<Events>();
class Emitter extends EventEmitter<Events> {
}

const eventEmitter = new Emitter();

then start emitting & listening to events

// Works just fine. so don't worry about "ImplicitAny" config, since type of "bar" is defined as "string"
eventEmitter.on("foo", bar => 1);

// This works fine as well
eventEmitter.on("withContextEnforcement", function (bar) {
  return this + bar;
}, 1);

// Throws an error (TS compile time), since this event requires the "bar" argument of type "string"
eventEmitter.emit("foo");

// Works just fine
eventEmitter.emit("foo", "bar");

Contextual emits

We've upgraded the API of the on, once, addListener, prependListener and prependOnceListener to accept an extra argument which is the context or this value that should be set for the emitted events. This means you no longer have the overhead of an event that required bind in order to get a custom this value.

const eventEmitter = new EventEmitter();
const context = { foo: "bar" };

function listener() {
  console.log(this === context); // true
}

eventEmitter.on("event:1", listener, context);
eventEmitter.once("event:2", listener, context);
eventEmitter.addListener("event:3", listener, context);
eventEmitter.prependListener("event:4", listener, context);
eventEmitter.prependOnceListener("event:5", listener, context);

Benchmarks

npm run benchmarks

Tests

npm test

Coverage

npm run test:coverage

Versioning

We use SemVer for versioning. For the versions available, see the releases on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details