Skip to main content

hasEventListener

Check whether any listener is registered for an event type. Useful in tests, or to skip work when nobody is listening.

hasEventListener(type: SoundEventsEnum): boolean;

Parameters​

ParameterTypeDefaultDescription
typeSoundEventsEnumrequiredThe event type to check.

Returns​

boolean: true when at least one listener is registered for type, false otherwise.

Example​

import { SoundHub, SoundEventsEnum } from 'soundhub';

const soundHub = new SoundHub();

soundHub.hasEventListener(SoundEventsEnum.ENDED); // false

const off = soundHub.addEventListener(SoundEventsEnum.ENDED, () => {}, { soundId: 'music' });
soundHub.hasEventListener(SoundEventsEnum.ENDED); // true

off();
soundHub.hasEventListener(SoundEventsEnum.ENDED); // false

Good to know​

  • Filters are ignored. A listener for { soundId: 'music' } makes this true even when you are asking on behalf of another sound.
  • A once listener counts until it has fired or been cancelled.

See also​