Removing event listener in event handler causes other handlers to not always fire
Created by: justinanastos
Steps to reproduce
- Attach two event handlers to listen to a single event using
mediaPlayer.on()
- In the first event handler, call
mediaPlayer.off()
to remove the handler being responded to - The second event will not fire
Observed behaviour
I expect all event handlers to fire, but they will not.
Reason
The cause is found here:
https://github.com/Dash-Industry-Forum/dash.js/blob/development/src/core/EventBus.js#L75
handlers[type] = handlers[type].splice(idx, 1);
interacting with
https://github.com/Dash-Industry-Forum/dash.js/blob/development/src/core/EventBus.js#L87
handlers[type].forEach( handler => handler.callback.call(handler.scope, payload) );
The handlers[type]
array is mutated in place and short-circuits the handlers[type].forEach()
loop. If I change the off()
behavior the following:
handlers[type] = handlers[type].slice(0).splice(idx, 1);
The original array is not mutated and all handlers will fire as expected.