Skip to main content

dispatchEvent

Send an event through the hub's event bus yourself. The hub uses this internally for every event it fires. Call it when you want your own code to raise an event that existing listeners already handle, or to test a listener.

dispatchEvent(event: SoundEvent): void;

Parameters​

ParameterTypeDefaultDescription
eventSoundEventrequiredThe event to send. type is required and must be a SoundEventsEnum value; every other field is optional.

Returns​

Nothing.

Example​

import { SoundHub, SoundEventsEnum } from 'soundhub';

const soundHub = new SoundHub();

soundHub.addEventListener(SoundEventsEnum.ERROR, (event) => {
showToast(event.error?.message ?? 'Audio problem');
});

// Report your own audio problem through the same listener
soundHub.dispatchEvent({
type: SoundEventsEnum.ERROR,
error: new Error('Microphone not available'),
timestamp: soundHub.getContext().currentTime,
});

Good to know​

  • Only listeners for event.type receive it, and their filters apply as usual. A listener filtered on { soundId: 'music' } only hears the event when you set soundId: 'music'.
  • Nothing is filled in for you. Set timestamp, soundId and whatever else your listeners read.
  • The event does not change any state in the hub. It does keep the lock screen in step when soundId is the sound on setMediaSession, and with autoSuspend on, a started, stopped, paused, resumed or ended event makes the hub check again whether anything is playing.
  • A listener that throws is caught and logged to the console, and the rest still run.

See also​