@braintree/event-emitter
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.0
    3
    • latest

Version History

Package Sidebar

Install

npm i @braintree/event-emitter

Weekly Downloads

188,676

Version

2.0.0

License

MIT

Unpacked Size

4.87 kB

Total Files

5

Last publish

Collaborators

  • braintree