Bug Report
Description
When is called with a listener function that was never added, it incorrectly removes the last listener in the array instead of doing nothing.
Root Cause
In , line 100-101:
const index = listeners.indexOf(listenerFunc);
this.listeners[eventName].splice(index, 1);
When listenerFunc is not found, indexOf returns -1, and splice(-1, 1) removes the last element.
Expected Behavior
Calling removeListener with a non-existent listener should do nothing.
Actual Behavior
The last listener in the array is incorrectly removed.
Steps to Reproduce
- Add two listeners to an event
- Try to remove a third listener that was never added
- Observe that the second listener is removed instead
Proposed Fix
Add a check before splicing:
const index = listeners.indexOf(listenerFunc);
if (index !== -1) {
this.listeners[eventName].splice(index, 1);
}
Impact
- Severity: Medium
- Affected: Web platform implementations using WebPlugin
- Breaking: No
Additional Context
I have a PR ready with the fix and test case.