Skip to content

Unable to unsubscribe from event listeners #193

@hutch120

Description

@hutch120

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.

  1. Remove listeners on removeControl
{
    key: 'onRemove',
    value: function onRemove(map) {
      this._events[type] = null; // <<====== ADD THIS LINE

and/or

  1. Add an un function 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
    };
  };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions