logo

EventEmitter

The EventEmitter class is used to emit events and register listeners. The API is based on the Node.js EventEmitter API.

Listen to an event

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');

Listen to an event only once

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');

Listen to an event before other listeners

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');

Emit an event

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');

Remove listeners

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');

Get listeners

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'));

Count listeners

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'));

Check if an event has listeners

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'));

Get event names

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());

Get max listeners

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());

Set max listeners

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);