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

2.1.0 • Public • Published

event-utils

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

An event utils system written in ES6.

paypal coinbase twitter

Installation

npm install event-utils

Usage

import EventEmitter from "event-utils";
const emitter = new EventEmitter();

// foo on
function callback1(arg) {
  console.log("callback1 called", arg);
}
function callback2() {
  console.log("callback2 called");
}
function callbackOnce() {
  console.log("callback called once");
}
emitter.on("foo", callback1);
emitter.on("foo", callback2);
emitter.once("foo", callbackOnce);

// bar on
function callbackBar() {
  console.log("callbackBar called");
}
emitter.on("bar", callbackBar);

// Emit foo and bar
emitter.emit("foo", "arg");
emitter.emit("bar");
emitter.emit("foo"); // To test once

// Off
emitter.off("foo");

// Emit bar
emitter.emit("bar");

// off all = off bar
emitter.off("bar");

// Emit with no callbacks
emitter.emit("bar");
emitter.emit("foo");

API

EventEmitter

An event utils system written in ES6.

Kind: global class

new EventEmitter()

Creates an instance of EventEmitter.

eventEmitter.on(event, cb) ⇒ EventEmitter

Add callback to event

Kind: instance method of EventEmitter

Param Type
event string
cb function

eventEmitter.once(event, cb) ⇒ EventEmitter

Add callback to event and remove on first call

Kind: instance method of EventEmitter

Param Type
event string
cb function

eventEmitter.off(event, cb) ⇒ EventEmitter

Remove callback from event

Kind: instance method of EventEmitter

Param Type
event string
cb function

eventEmitter.emit(event, ...cbs) ⇒ EventEmitter

Emit an event with arguments

Kind: instance method of EventEmitter

Param Type
event string
...cbs args

eventEmitter.listeners(event) ⇒ Array.<function()>

Return all callbacks attached to an event

Kind: instance method of EventEmitter

Param Type
event string

eventEmitter.hasListeners(event) ⇒ boolean

Return a boolean if the event has listeners

Kind: instance method of EventEmitter

Param Type
event string

License

MIT. See license file.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.1.0
    80
    • latest

Version History

Package Sidebar

Install

npm i event-utils

Weekly Downloads

62

Version

2.1.0

License

MIT

Unpacked Size

12.3 kB

Total Files

6

Last publish

Collaborators

  • dmnsgn