ckeditor / ckeditor5-design

☠ Early discussions about CKEditor 5's architecture. Closed now. Go to https://github.com/ckeditor/ckeditor5 ☠
58 stars 12 forks source link

Is using emitter#on an anti-pattern (for most scenarios)? #142

Closed Reinmar closed 7 years ago

Reinmar commented 8 years ago

If you use emitter#on you need to remember to off() the event unless you expect the emitter to die automatically together with the code where the listener was defined. The special case here is that the event listener is defined by the emitter itself (e.g. a constructor of a special kind of collection listens to #add/#remove on itself).

While destroying the editor we also destroy many of its components by calling their destroy() methods. Those methods usually call this.stopListening() which removes all listeners added by this.listenTo( emitter, eventName ). That nicely cleans up all the listeners except those added with on().

This means that there are two things we can do:

  1. Agree that you shouldn't use on() unless you know that the emitter will die with your object.
  2. When calling stopListening() remove also all listeners added with on() (that will make this.on() a shorthand for this.listenTo( this )).

I'm trying to understand if the latter is at all possible, because listenTo() uses on() internally. I can't answer this straight so I'll leave this open and let you comment :).

Reinmar commented 7 years ago

Moved to https://github.com/ckeditor/ckeditor5-utils/issues/144.