medooze-event-emitter
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

medooze-event-emitter

This is a modified version of Node.js EventEmitter class for use within Medooze's codebase.

It's exported directly by this module, and meant to be extended rather than used as a standalone object.

The changes are:

  • the event definitions are type checked (see below)
  • emit() is marked protected to prevent it from accidentally being called from outside of the class
  • only a small subset of the API is exported (we can expand it if needed)
  • the maxListener limit is removed
  • implements an additional stop() method, which removes all listeners and causes future calls (except emit()) to be no-ops
  • exceptions thrown from event handlers are prevented from propagating out of emit() (thrown at the next tick instead)

Use with type checking

Using the same mechanism as tiny-typed-emitter, this class allows users to declare the events that a class may emit, as well as the arguments each one accepts. Calls to on() / once() / emit() are then typechecked accordingly.

Usage is as follows: projects that use type checking must supply an interface as generic parameter to the class when extending it. Each property of this interface is an event, and its type must be that of the listener (a function accepting some arguments and returning void).

Here's an example of how that would look in a project that uses JSDoc annotations (see tiny-typed-emitter for an example using TypeScript syntax):

const Emitter = require("medooze-event-emitter");

/**
 * @typedef {Object} SubscriptionEvents
 * @property {(transport: Transport, kind: boolean) => void} inited - subscription has initialized
 * @property {() => void} stopped - subscription has stopped
 */

/** @extends {Emitter<SubscriptionEvents>} */
class Subscription extends Emitter {
    // ...
}

Here we define a Subscription class that may emit two events: stopped, with no arguments, and inited with an argument of type Transport and another of type boolean.

Readme

Keywords

Package Sidebar

Install

npm i medooze-event-emitter

Weekly Downloads

87

Version

1.2.1

License

MIT

Unpacked Size

10.1 kB

Total Files

6

Last publish

Collaborators

  • bryan-lester-dolby
  • mildsunrise
  • murillo128
  • sgarcia