A helper module for creating objects with event emitter capabilities.
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.
import EventEmitter from "@braintree/event-emitter";
class MyClass extends EventEmitter() {
// my class definition
}
const emitter = new MyClass();
emitter.on("event-name", function (data) {
console.log("called with", data.payload, "!");
});
emitter.emit("event-name", { payload: "foo" }); // logs "called with foo!"
const cb = function () {};
emitter.on("event-name", cb);
emitter.off("event-name", cb);
emitter.emit("event-name", { payload: "foo" }); // cb is not called
npm test