2.1.0 • Published 3 years ago

event-utils v2.1.0

Weekly downloads
40
License
MIT
Repository
github
Last release
3 years ago

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

npm.io

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

ParamType
eventstring
cbfunction

eventEmitter.once(event, cb) ⇒ EventEmitter

Add callback to event and remove on first call

Kind: instance method of EventEmitter

ParamType
eventstring
cbfunction

eventEmitter.off(event, cb) ⇒ EventEmitter

Remove callback from event

Kind: instance method of EventEmitter

ParamType
eventstring
cbfunction

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

Emit an event with arguments

Kind: instance method of EventEmitter

ParamType
eventstring
...cbsargs

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

Return all callbacks attached to an event

Kind: instance method of EventEmitter

ParamType
eventstring

eventEmitter.hasListeners(event) ⇒ boolean

Return a boolean if the event has listeners

Kind: instance method of EventEmitter

ParamType
eventstring

License

MIT. See license file.

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago