Ascend Events
Installation
npm install ascend-events
yarn install ascend-events
API
EventEmitter
Create an Event Emitter.
import { EventEmitter } from 'ascend-events';
const eventEmitter = new EventEmitter();
class MyClass extends EventEmitter {
constructor() {
super();
}
}
addListener
Adds a new event listener to the end of the listener
array that is associated with the passed eventName
. If no key is found, it will set a new element using the passed eventName
and listener
.
params
eventName
{string} | {symbol} (required) The name of the event being addedlistener
{Function} (required) The callback function
returns
const eventEmitter = new EventEmitter();
eventEmitter.addListener('listener', () => console.log('listener'));
emit
Calls each listener associated with the passed eventName
in the order that they were added. Additional arguments are passed to each listener callback.
params
eventName
{string} | {symbol} (required) The name of the event being emitted...args
{any[]} (Optional) Arguments to be passed to each listener function
returns
const eventEmitter = new EventEmitter();
eventEmitter.add('test', (msg: string) => console.log('msg: ', msg));
eventEmitter.emit('test', 'hello world');
getListenerCount
Returns the number of listeners associated with the passed eventName
.
params
eventName
{string} | {symbol} (required) The name of the event to get the listener count of
returns
const eventEmitter = new EventEmitter();
eventEmitter.addListener('msg', (msg: msg) => console.log('msg: ', msg));
eventEmitter.getListenerCount('msg');
eventEmitter.getListenerCount('doesntExist');
setMaxListeners
Sets the maximum number of listeners on this instance of the EventEmitter.
params
num
{number} (required) The number of maximum listeners
returns
const eventEmitter = new EventEmitter();
eventEmitter.setMaxListeners(5);
console.log(eventEmitter.maxListeners);
listeners
Returns the listeners associated with the passed eventName
, if no key was found, returns an empty array []
.
params
eventName
{string} | {symbol} (required)
returns
const eventEmitter = new EventEmitter();
eventEmitter.addListener('event', () => null)
eventEmitter.listeners('event')
off
alias for EventEmitter.removeListener()
params
eventName
{string} | {symbol} (required)listener
{Function} (required)
returns
const eventEmitter = new EventEmitter();
eventEmitter.on('msg', (msg: string) => console.log('msg: ', msg));
eventEmitter.off('msg', (msg:string) => console.log('msg: ', msg));
on
alias for eventEmitter.addListener()
params
eventName
{string} | {symbol} (required)listener
{Function} (required)
returns
const eventEmitter = new EventEmitter();
eventEmitter.on('msg', (msg: string) => console.log('msg: ', msg));
eventEmitter.emit('msg', 'hello world');
once
Adds a listener associated to the passed eventName
that is triggered once and then removed.
params
eventName
{string} | {symbol} (required)listener
{Function} (required)
returns
const eventEmitter = new EventEmitter();
eventEmitter.once('msg', (msg: string) => console.log('msg: ', msg));
eventEmitter.emit('msg', 'hello world');
eventEmitter.listeners('msg');
prependListener
Adds the passed listener
to the beginning of the listener array associated with the given eventName
.
params
eventName
{string} | {symbol} (required)listener
{Function} (required)
returns
const eventEmitter = new EventEmitter();
eventEmitter.addListener('msg', () => 'called second');
eventEmitter.prependListener('msg', () => 'called first');
eventEmitter.emit('msg');
prependOnceListener
Adds the passed listener
to the beginning of the listener array associated with the given eventName
, once triggered will be removed.
params
eventName
{string} | {symbol} (required)listener
{Function} (required)
returns
const eventEmitter = new EventEmitter();
eventEmitter.prependOnceListener('msg', () => 'One time');
eventEmitter.emit('msg');
eventEmitter.listeners('msg');
removeAllListeners
Removes all listeners associated with the passed eventName
, if no eventName
was given, all listeners are removed entirely.
params
eventName
{string} | {symbol} (optional)
returns
const eventEmitter = new EventEmitter();
eventEmitter.addListener('msg', () => console.log('gets removed'));
eventEmitter.addListener('msg2', () => console.log('also gets removed'));
eventEmitter.removeAllListeners();
removeListener
Removes listener
associated with the passed eventName
params
eventName
{string} | {symbol} (required)listener
{Function} (required)
returns
const eventEmitter = new EventEmitter();
eventEmitter.addListener('msg', () => console.log('gets removed'));
eventEmitter.addListener('msg', () => console.log('does not get removed'));
eventEmitter.removeListener('msg', () => console.log('gets removed'));
eventEmitter.emit('msg');