Skip to content

dispose() does not clear _anyListeners #430

@Lo4D

Description

@Lo4D

There is an onAny() function which adds listener to final List _anyListeners = []; in socket.dart.

  /// Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
  /// callback.
  ///
  /// @example
  /// socket.onAny((event, ...args) => {
  ///   console.log(`got ${event}`);
  /// });
  ///
  /// @param listener
  @override
  Socket onAny(AnyEventHandler handler) {
    _anyListeners.add(handler);
    return this;
  }

But dispose() which claims to clear all event listeners never touches that List _anyListeners .

  /// Disposes the socket manually which will destroy, close, disconnect the socket connection
  /// and clear all the event listeners. Unlike [close] or [disconnect] which won't clear
  /// all the event listeners
  ///
  /// @since 0.9.11
  void dispose() {
    disconnect();
    clearListeners();
  }

It does call clearListeners() which is a method in parent class \ file event_emitter.dart

  /// This function unbinds all the handlers for all the events.
  void clearListeners() {
    this._events = new HashMap<String, List<EventHandler>>();
    this._eventsOnce = new HashMap<String, List<EventHandler>>();
    this._eventsAny.clear();
  }

But it does clear only private fields in that file and doesn't touch List _anyListeners

Currently I use that workaround:

socket.dispose();
socket.offAny();

Because offAny() indeed does clear previously mentioned list:

  /// Removes the listener that will be fired when any event is emitted.
  ///
  /// @example
  /// const catchAllListener = (event, ...args) => {
  ///   console.log(`got event ${event}`);
  /// }
  ///
  /// socket.onAny(catchAllListener);
  ///
  /// // remove a specific listener
  /// socket.offAny(catchAllListener);
  ///
  /// // or remove all listeners
  /// socket.offAny();
  ///
  /// @param listener
  @override
  Socket offAny([AnyEventHandler? handler]) {
    if (handler != null) {
      _anyListeners.remove(handler);
    } else {
      _anyListeners.clear();
    }
    return this;
  }

Is that by design?

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