Skip to main content

removeEventListenersForInstance

Remove every listener that was added with an instanceId filter for this id, whatever event type it listens to. The hub calls this itself when a sound stops or ends, so you rarely need it.

removeEventListenersForInstance(instanceId: string): void;

Parameters​

ParameterTypeDefaultDescription
instanceIdstringrequiredThe instance id used in the filter, such as 'laser:3', or a plain sound id such as 'music'.

Returns​

Nothing.

Example​

import { SoundHub, SoundEventsEnum } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('laser', '/audio/laser.mp3');

soundHub.play('laser', { overlap: true, trackProgress: true }); // becomes 'laser:1'

soundHub.addEventListener(SoundEventsEnum.PROGRESS, (event) => {
console.log(event.progress);
}, { instanceId: 'laser:1' });

soundHub.addEventListener(SoundEventsEnum.ENDED, () => {
console.log('laser:1 done');
}, { instanceId: 'laser:1' });

// Drop both at once
soundHub.removeEventListenersForInstance('laser:1');

Good to know​

  • Only listeners whose filter has exactly this instanceId are removed. Listeners filtered on soundId, originalId or instancePattern, and listeners without a filter, stay.
  • The hub already runs this for a sound or instance when it is stopped, when it ends (after the ended event has been delivered) and when it is cleaned up. An instanceId listener therefore lasts for one playback.
  • No event is dispatched.

See also​