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

typed-event-emitter

v3.0.0

Published

Alternative event emitter for JavaScript and TypeScript.

Downloads

44,638

Readme

typed-event-emitter

This module provides an alternative API than the well known event emitting interfaces used in the browser (DOM) or node.js. Instead of accepting arbitrary strings as the event name, this module forces you to register your events in your class. Consequently, the style of binding and emitting events differs a little bit, ensuring already at binding time that the events actually exists.

Install

Via npm:

$ npm install typed-event-emitter

Usage

Take a look at the following snippet (TypeScript):

import { EventEmitter } from 'typed-event-emitter';

class MyClass extends EventEmitter {
  public readonly onValueChanged = this.registerEvent<[number]>();
  
  private _value: number;
  
  constructor(value: number) {
    // initialize EventEmitter
    super();
    
    this._value = value;
  }
  
  get value() {
    return this._value;
  }
  
  set value(value: number) {
    this._value = value;
    this.emit(this.onValueChanged, this._value);
  }
}

let instance = new MyClass(0);
instance.onValueChanged(newValue => {
  console.log(`Value changed: ${newValue}`);
});

instance.value = 27;

First, the EventEmitter is loaded from the module. Any class that shall emit events must extend that EventEmitter. If your class has its own constructor, make sure to call super().

Any events your class shall be able to emit must be registered in the form:

onFooBar = this.registerEvent<callbackArgTypes>();

Where onFooBar can be any name (it doesn't need to begin with on) and callbackArgTypes must be an array of the argument types the callback accepts. With this, you can see the signature your function must have when you're about to bind a listener to that event.

To fire/emit an event (only possible from within your event emitter), you have to call this.emit(this.onFooBar, ...), where this.onFooBar is the event to emit and ... any number of parameters that will be passed to the listeners.

JavaScript

The code shown above can also be written in JavaScript (node.js):

const EventEmitter = require('typed-event-emitter').EventEmitter;

class MyClass extends EventEmitter {
  constructor(value) {
    // initialize EventEmitter
    super();
    
    /* newValue: number */
    this.onValueChanged = this.registerEvent();
    
    this._value = value;
  }
  
  get value() {
    return this._value;
  }
  
  set value(value) {
    this._value = value;
    this.emit(this.onValueChanged, this._value);
  }
}
 
let instance = new MyClass(0);
instance.onValueChanged(newValue => {
  console.log(`Value changed: ${newValue}`);
});
 
instance.value = 27;

Note that the events are registered explicitly within the constructor. Make sure to initialize them after calling super().

Changelog

3.0.0 (2021-09-07)

  • BREAKING CHANGE (TypeScript): registerEvent<(arg: number) => any>() now is registerEvent<[number]>()
  • Add unit tests (run npm test)

2.0.0 (2019-08-06)

  • Make methods more type safe. This is a breaking change if used in a TypeScript project (rather than plain JavaScript), as this requires TypeScript 3.0+.

1.1.0 (2018-10-01)

  • Add support for ES5.

1.0.0 (2016-07-18)

  • Initial release.

License

typed-event-emitter is licensed under the MIT License.