-
Notifications
You must be signed in to change notification settings - Fork 12
T/144: Refactor EmitterMixin methods. #190
Conversation
I've found one bug in code. But probably it will be better to handle it with #191. The |
src/dom/emittermixin.js
Outdated
listenTo( ...args ) { | ||
const emitter = args[ 0 ]; | ||
|
||
listenTo( emitter, event, callback, options ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you used listenTo( emitter, ...rest )
here, you'd be able to use proxy.attach( ...rest );
and EmitterMixin.listenTo.call( this, emitter, ...rest );
later in the code. IMO, if the method does not process these arguments and just passes them through, it's safer to use rest.
src/emittermixin.js
Outdated
@@ -1,5 +1,5 @@ | |||
/** | |||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | |||
* @license Copyright (c) 2003-2017, CKSource -insteadico Knabben. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never heard of the guy 😛
I've rebased this branch and "fixed" coverage - I cannot test that check but I feel uncomfortable with just removing it so I've ignored it for coverage. |
EmitterMixin.stopListening.call( this, emitter, event, callback ); | ||
|
||
if ( emitter instanceof ProxyEmitter ) { | ||
emitter.detach( event ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICS, event
can be undefined. Which means calling emitter.detach()
but its first param is not optional. Could you verify that it all works fine in cases where stopListening()
's params are not passed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean besides tests? The event
parameter is check by the EmitterMixin.stopListening()
and also in here dom/EmitterMixinProxy.detach()
: https://github.com/ckeditor/ckeditor5-utils/pull/190/files/86c8efc9cff0415a01cdd3cd2a29aef81e1aadce#diff-99d3cd5d9460b6839252663e7c38b1c6R218 - actually it is checked if given event
is defined in _domListeners
so if it is falsy then detach
will do nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's handled very poorly there, because if event is undefined
then we do listeners[ undefined ]
. And the API docs of detach()
are also incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words – if we expect event
to be undefined then it should be handled explicitly. However, it's not the problem of your PR – it was like that in the past.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it seems to be a bug: https://github.com/ckeditor/ckeditor5-utils/issues/199
OK, unfortunately, this is an R- because of lack of tests. A change must always be accompanied by tests unless this is purely internal refactoring. |
What do you mean? |
@Reinmar: I've added this ignore ckeditor5-utils/src/emittermixin.js Lines 508 to 513 in f1a92c1
Because it's private method that in code is used either with registering event namespace: ckeditor5-utils/src/emittermixin.js Lines 151 to 152 in f1a92c1
or is indirectly checked (as in ckeditor5-utils/src/emittermixin.js Lines 203 to 204 in f1a92c1
In other words I'm not able to make a test for this one line. |
That line could be removed though - It's not covered in test for above reason - it is superflous in current code. I've done some monkey clicking/typing on editor and it seems that that part of code is just dead... |
An unnecessary code should be removed. There should be no zombie code. That the first thing. The second thing is – how did you check that it's really unnecessary? There were very little tests for these scenarios. So, this should be validated by writing tests, not assuming (which even when correct, is short-termed). I did both things in 4e3c1e9. Then, I turned back to tests. As I wrote – a change in the code must be accompanied by tests (unless it's internal). This change is not internal. You're fixing a bug here. Changing the behaviour of this mixin. This must be tested. Not once, manually. But by automated tests. I verify whether a change is tested like this: A test should be failing when I checked out |
@Reinmar I've added a bit more tests and it looks that mentioned code was indeed needed - My bad :(. Right now there are two failing tests both might be considered bugs or features:
listner.listenTo( emitter, 'foo' );
emitter.fire( 'foo' );
emitter.once( 'foo', doSomething );
emitter.fire( 'foo:bar' );
// will call doSomething
emitter.fire( 'foo:bar' );
// will call doSomething |
I've also included some more tests (mixed listeners) for |
The build fails. |
@Reinmar I've forgot to talk about it but to tests fails:
Should it be called only once on namespaced events or not? emitter.once( 'foo', spy );
emitter.fire( 'foo:bar' );
emitter.fire( 'foo:bar' );
// it will call `spy` indefinitely. |
If you consider these topics unrelated to the changes that we're doing in this PR, then sure – go ahead. They seem unrelated to me, but I haven't tried to analyse it too long. |
I've created: https://github.com/ckeditor/ckeditor5-utils/issues/204 & https://github.com/ckeditor/ckeditor5-utils/issues/203 for those issues. |
Suggested merge commit message (convention)
Other: Aligned behaviors of
EmitterMixin
methods responsible for adding end removing listeners. Closes ckeditor/ckeditor5#4947.The
emitter.on
now has the same behavior asemitter.listenTo( emitter )
as well asemitter.off
is the same asemitter.stopListening( emitter )
.Additional information
on
/listenTo
andoff
/stopListening
so theon
/off
are purely shorthands for base methods.Proxy
inDomEmitterMixin
as it usedlistenTo
/on
in pretty convoluted way. I've renamedProxy
methods to not confuse them withEmitterMixin
ones.EmitterMixin
that were required were inDomEmitterMixin
.