-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Should EmitterMixin.stopListening() off all listeners (those added through on() too)? #4947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
👍 I'm really surprised it works that way ATM. I'm pretty sure outsiders will be thrice as surprised because of that and I think it's a good enough reason to rectify |
Actually, please guys note that making So, this is kinda additional question – do you think that this should also be done? |
|
I would go with anything that is more common. The |
I'd go even further and either remove Edit: Okay, maybe not that far, using |
Yes, using |
Okay so I see that we all agree that |
I R-ed @scofalik's PR: ckeditor/ckeditor5-utils#145 (comment) The PR implements the simplest solution (forced So, right now |
OK I've dig into this and checking how @scofalik fixed memory leaks in ckeditor/ckeditor5-utils#145 PR. Also I've refactore (in In term of memory leaks we have to remember which object called Event callbacks will always be stored at this.listenTo( someEmitter, 'foo', () => {} );
// other palce in code:
this.listenTo( someEmitter, 'bar', () => {} );
// later in code: when someEmitter has reached End Of Life
this.stopListening( someEmitter ); B: someEmitter.listenTo( someEmitter, 'foo', () => {} );
// same as:
someEmitter.on( 'foo', () => {} );
// other palce in code:
someEmitter.listenTo( someEmitter, 'bar', () => {} );
// here nothing have to be done as log no reference to `someEmitter` exists
// it will be garbage collected. The most problematic part of code as I see it is in @pjasiun said that from documantation POV it might be clearer when there will be two separate classes: |
The B option is much better. If you're using an object which is supposed to be temporary, you should pick it as the observer (so use |
Other: Aligned behaviors of `EmitterMixin` methods responsible for adding end removing listeners. Closes #144. The `emitter.on()` now has the same behavior as `emitter.listenTo( emitter )` as well as `emitter.off()` is the same as `emitter.stopListening( emitter )`. This made `emitter.stopListening()` correctly remove all listeners added in any way to it which prevents memory leaks.
While working on ckeditor/ckeditor5-engine#904 @scofalik noticed that
stopListening()
is not removing listeners added usingon()
.I assume it was such a case, right @scofalik?
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 usethis.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:
stopListening()
shouldoff()
all listeners added byon()
,listenTo()
and removeon()
(alternatively,on()
should uselistenTo()
internally).I wouldn't totally remove
on()
because its shortness is useful. Having to always doa.listenTo( a, ...)
would be cumbersome. Also, we useon()
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 toa.listenTo( a )
orstopListening()
offing all events. In the former case we need to figure out whatoff()
should do. In the latter, nothing changes AFAICT.WDYT?
The text was updated successfully, but these errors were encountered: