项目作者: dmnsgn

项目描述 :
An event utils system written in ES6.
高级语言: JavaScript
项目地址: git://github.com/dmnsgn/event-utils.git
创建时间: 2015-06-27T13:32:15Z
项目社区:https://github.com/dmnsgn/event-utils

开源协议:MIT License

下载


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

  1. npm install event-utils

Usage

  1. import EventEmitter from "event-utils";
  2. const emitter = new EventEmitter();
  3. // foo on
  4. function callback1(arg) {
  5. console.log("callback1 called", arg);
  6. }
  7. function callback2() {
  8. console.log("callback2 called");
  9. }
  10. function callbackOnce() {
  11. console.log("callback called once");
  12. }
  13. emitter.on("foo", callback1);
  14. emitter.on("foo", callback2);
  15. emitter.once("foo", callbackOnce);
  16. // bar on
  17. function callbackBar() {
  18. console.log("callbackBar called");
  19. }
  20. emitter.on("bar", callbackBar);
  21. // Emit foo and bar
  22. emitter.emit("foo", "arg");
  23. emitter.emit("bar");
  24. emitter.emit("foo"); // To test once
  25. // Off
  26. emitter.off("foo");
  27. // Emit bar
  28. emitter.emit("bar");
  29. // off all = off bar
  30. emitter.off("bar");
  31. // Emit with no callbacks
  32. emitter.emit("bar");
  33. 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.

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.