aelliott1485 / emitter

Event Emitter module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

emitter Build Status

This Event Emitter module allows one or more functions to be attached to named events and those named events to be emitted.

Check out the Code Review for feedback by community members.

This was originally created in 2018.


Installation

First, clone the repository:

$ git clone https://github.com/aelliott1485/emitter.git

Then go into the local directory for the repository:

$ cd emitter

Next install the dependencies (e.g. test frameworks)

$ npm install

Then the tests can be run to ensure it works as expected:

$ npm test

Example:

var emitter = new EventEmitter();
//register the handler function (defined below)
emitter.on('sent', receive);
//later, emit the event and pass data, which will be sent to the handler function(s)
emitter.emit('sent', {"foo": "bar"});

function receive(data) {
  //handle data
}

API

emitter.emit(eventName[, ...args])

Emits a named event with any number of arguments.

emitter.on(eventName, callback)

Registers a handler function to be called whenever the named event is emitted.

emitter.once(eventName, callback)

Registers a handler function to only be called the first time the named event is emitted.

emitter.off(eventName[, callback])

Removes a specific previously-registered event handler and/or all previously-registered event handlers.

About

Event Emitter module

License:MIT License


Languages

Language:JavaScript 100.0%