
The EventEmitter class is used to emit events and register listeners. The API is based on the Node.js EventEmitter API.
The on method adds a listener, which will be called when the event is emitted. Optionally the listener can be removed after the first call with the once option. Optionally the listener can be called before other listeners with the prepend option.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
emitter.on('event', (data) => {
// will log "hello"
console.log(data);
});
emitter.emit('event', 'hello');The once method adds a listener, which will be called when the event is emitted. The listener will be removed after the first call.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
emitter.once('event', (data) => {
// will log "hello" only once
console.log(data);
});
emitter.emit('event', 'hello');
emitter.emit('event', 'hello');The prepend method adds a listener, which will be called before other listeners when the event is emitted.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
emitter.on('event', (data) => {
// will log "hello" second
console.log('on', data);
});
emitter.prepend('event', (data) => {
// will log "hello" first
console.log('prepend', data);
});
emitter.emit('event', 'hello');The emit method emits an event.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
emitter.on('event', (data) => {
// will log "hello"
console.log(data);
});
emitter.emit('event', 'hello');The off method removes the listeners that match the name, callback and options. If no arguments are passed, all listeners will be removed.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
const listener = (data) => {
// will log "hello" only once
console.log(data);
};
emitter.on('event', listener);
emitter.emit('event', 'hello');
emitter.off('event', listener);
emitter.emit('event', 'hello');The getListeners method is used to return the listeners that match the name, callback and options. If no arguments are passed, all listeners will be returned.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
const listener = (data) => {
// will log "hello" only once
console.log(data);
};
emitter.on('event', listener);
// will log [listener]
console.log(emitter.getListeners('event'));The countListeners method is used to count the number of listeners that match the specified event name, callback function, and options. If no arguments are passed, all listeners will be counted.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
const listener = (data) => {
// will log "hello" only once
console.log(data);
};
emitter.on('event', listener);
// will log 1
console.log(emitter.countListeners('event'));The hasListeners method is used to check if the EventEmitter has listeners that match the specified event name, callback function, and options.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
const listener = (data) => {
// will log "hello" only once
console.log(data);
};
emitter.on('event', listener);
// will log true
console.log(emitter.hasListeners('event'));The getEventNames method is used to return an array of the names of all events for which the EventEmitter has registered listeners.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
const listener = (data) => {
// will log "hello" only once
console.log(data);
};
emitter.on('event', listener);
// will log ["event"]
console.log(emitter.getEventNames());The getMaxListeners method is used to return the maximum number of listeners that can be registered for a single event.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
// will log 10
console.log(emitter.getMaxListeners());The setMaxListeners method is used to set the maximum number of listeners that can be registered for a single event.
import { EventEmitter } from '@aracna/core';
const emitter = new EventEmitter();
emitter.setMaxListeners(1);
const listener = (data) => {
// will log "hello" only once
console.log(data);
};
// will register
emitter.on('event', listener);
// will not register
emitter.on('event', listener);