-
Notifications
You must be signed in to change notification settings - Fork 130
Description
Hi All,
I've just spent quite a while tracking down an issue that results in multiple triggering of the on('xxxx') functions.
I have a react application that moves between pages, and loads/unloads components as it should. But, when I unload this module, it maintains the listener state, despite calling removeControl.
Essentially the issue (other than annoyance of multiple callbacks) is that eventually you will hit the maxListeners limit of 10 if you call the on handlers. E.g. on('route') because there is no way to unregister listeners, not even by using removeControl.
I've got a workaround in my code that sets a static variable, and checks that before registering the listener, but feelsbadman.
I think there are two things that could resolve this.
- Remove listeners on removeControl
{
key: 'onRemove',
value: function onRemove(map) {
this._events[type] = null; // <<====== ADD THIS LINE
and/or
- Add an
unfunction to remove a listener. A bit harder..
{
key: 'un',
value: function on(type, fn) {
this.actions.eventUnSubscribe(type, fn);
return this;
}
function eventUnSubscribe(type, fn) {
return function (dispatch, getState) {
var _getState8 = getState(),
events = _getState8.events;
events[type] = events[type] || [];
// >>>>> FIND AND REMOVE THE EVENT <<<<<
return {
type: types.EVENTS,
events: events
};
};
}