'how to fix listeners called more than once

How to check if listener is already present, eventCallbackSpyTrack is called 2 times it seems. When someone subscribes to the same track more than once? I guess all the track listeners called more than once, How to fix this ?

// expect(eventCallbackSpyTrack.calledOnce).to.eq(true);

eventCallbackSpyTrack is called 2 times, eventCallbackSpyTrack.callCount is 2 but it should be 1

How to fix this ? I tried to remove the removeEventListener but this doesn't work. Please help.

async subscribe(eventName: string, listener: () => void): Promise<subscription> {
    const subscriptionListener = {
      id: uuidv4(),
      method: listener,
    };

    subscriptions.events[eventName].set(subscriptionListener.id, {
      module: 'track',
      method: subscriptionListener.method,
    });
    const thisEventListeners = subscriptions.events[eventName];

    switch (eventName) {
      case 'track:mute': {
        if (thisEventListeners.size >= 1) {
            this.#mediaStreamTrack.addEventListener('mute', (event) => {
              trackMutePublisher(event, this, 'track');
            });
         
        } 
        
        break;
      }

      default:
        break;
    }

    return new Promise((resolve) => {
      resolve({
        type: eventName,
        listener: subscriptionListener,
      });
    });
  }

In my unit test it says

// expect(eventCallbackSpyTrack.calledOnce).to.eq(true);

eventCallbackSpyTrack is called 2 times, eventCallbackSpyTrack.callCount is 2 but it should be 1

unit test -

describe.only('track mute event & publisher', () => {
      before(async () => {
        [videoTrack] = (await navigator.mediaDevices.getUserMedia({video: true})).getVideoTracks();
        track = new Track(videoTrack as MediaStreamTrack);
        setupMediaTrackMocks();
        subscription = await track.subscribe('track:mute', eventCallbackSpyTrack);
        subscription2 = await track.subscribe('track:mute', eventCallbackSpyTrack2);
      });

      after(() => {
        resetMediaTrackMocks();
      });


      it('should have called the callback once', () => {
        expect(subscription.listener.method).to.be.equal(eventCallbackSpyTrack);
        expect(subscriptions.events['track:mute'].get(subscription.listener.id)?.method).to.be.equal(eventCallbackSpyTrack);
        expect(subscription2.listener.method).to.be.equal(eventCallbackSpyTrack2);
        expect(subscriptions.events['track:mute'].get(subscription2.listener.id)?.method).to.be.equal(eventCallbackSpyTrack2);

        track.getMediaStreamTrack().dispatchEvent(new Event('mute'));

        Sinon.assert.called(eventCallbackSpyTrack);
        // expect(eventCallbackSpyTrack.calledOnce).to.eq(true);
        expect(eventCallbackSpyTrack.callCount).to.be.equal(1);
      });

I tried to remove the removeEventListener but this doesn't work. Please help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source