Skip to content

Should EmitterMixin.stopListening() off all listeners (those added through on() too)? #4947

Closed
ckeditor/ckeditor5-utils
#190
@Reinmar

Description

@Reinmar

While working on ckeditor/ckeditor5-engine#904 @scofalik noticed that stopListening() is not removing listeners added using on().

I assume it was such a case, right @scofalik?

a.on( 'x',  callback );

a.stopListening();

If this is true, then it means that you can't easily clean all listeners that you registered. My first thought was that it makes this method useless.

However, as I found that I already explained in ckeditor/ckeditor5-design#142 (sic! :D), this.on() is to be used by a class internally. So if class X needs to listen to its own events, it can use this.on(). This means that together with the class its listeners will be gone because no more events will be fired as well.

However, again, this is a theory, because we don't enforce the rule that you can fire events only on yourself. So someone may mistakenly still fire events on an already destroyed instance of class X and the callbacks will be executed. This increases a chance of a bug.

Taken that, I'd say that:

  • either stopListening() should off() all listeners added by on(),
  • or we should always use listenTo() and remove on() (alternatively, on() should use listenTo() internally).

I wouldn't totally remove on() because its shortness is useful. Having to always do a.listenTo( a, ...) would be cumbersome. Also, we use on() mostly in tests and half of those cases where we use it source are incorrect (listenTo() should be used with a proper subject).

So, I'm either for making on() a shortcut to a.listenTo( a ) or stopListening() offing all events. In the former case we need to figure out what off() should do. In the latter, nothing changes AFAICT.

WDYT?

Metadata

Metadata

Assignees

Labels

package:utilstype:bugThis issue reports a buggy (incorrect) behavior.type:improvementThis issue reports a possible enhancement of an existing feature.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions