Socket
Socket
Sign inDemoInstall

medooze-event-emitter

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    medooze-event-emitter

type-safe, modified version of EventEmitter


Version published
Maintainers
1
Install size
9.03 kB
Created

Readme

Source

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.

Keywords

FAQs

Last updated on 21 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc