1. @braintree/event-emitter
A simple event emitter.
@braintree/event-emitter
Package: @braintree/event-emitter
Created by: braintree
Last modified: Tue, 07 Nov 2023 21:48:15 GMT
Version: 2.0.0
License: MIT
Downloads: 965,984
Repository: https://github.com/braintree/event-emitter

Install

npm install @braintree/event-emitter
yarn add @braintree/event-emitter

event-emitter

A helper module for creating objects with event emitter capabilities.

Installation

npm

 npm install --save @braintree/event-emitter

This module uses commonjs. You must use a build tool such as Browserify or Webpack to include it in your frontend project.

Usage

Creating an Object that Inherits from Event Emitter

 import EventEmitter from "@braintree/event-emitter";

class MyClass extends EventEmitter() {
  // my class definition
}

const emitter = new MyClass();

Listen for events

 emitter.on("event-name", function (data) {
  console.log("called with", data.payload, "!");
});

emitter.emit("event-name", { payload: "foo" }); // logs "called with foo!"

Unsubscribe from events

 const cb = function () {};

emitter.on("event-name", cb);
emitter.off("event-name", cb);

emitter.emit("event-name", { payload: "foo" }); // cb is not called

Tests

 npm test

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code