-
Notifications
You must be signed in to change notification settings - Fork 40
T/629b Alternative fix infinite selection loop. #671
Conversation
return this.isBackward === otherSelection.isBackward; | ||
// Every range from this selection... | ||
return Array.from( this.getRanges() ).every( ( rangeA ) => { | ||
// ...Has a range in other selection... |
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.
// ... has
:P
return Array.from( this.getRanges() ).every( ( rangeA ) => { | ||
// ...Has a range in other selection... | ||
return Array.from( otherSelection.getRanges() ).some( ( rangeB ) => { | ||
// That it is equal to. |
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.
// ... which it is equal to.
@@ -102,6 +102,9 @@ export default class SelectionObserver extends Observer { | |||
} | |||
|
|||
domDocument.addEventListener( 'selectionchange', () => this._handleSelectionChange( domDocument ) ); | |||
domDocument.addEventListener( 'keydown', () => this._clearInfiniteLoop() ); |
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.
As I commented in https://github.com/ckeditor/ckeditor5-engine/issues/535#issuecomment-258168263, I prefer the other solution.
} | ||
|
||
return this._lastRangeBackward === otherSelection._lastRangeBackward; | ||
// Every range from this selection... |
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.
Do I see correctly that the ranges can be in a different order? Why so? Then, the _lastRangeBackward
property would need a more precise check. Besides, if we don't expect this to happen frequently, I'd consider these selections different.
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.
Backward/forward selection check is indirectly implemented when we check anchor
and focus
. Basically, if anchor
and focus
are equal and ranges are equal, this means that selections are equal. If the last range was added in different direction, anchor
and focus
would differ.
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.
I was rather asking why is this method trying to find a range in the other selection which matches range from this selection... for each range in this selection? Moreover, this implementation is very ineffective, cause it creates new array on every call of every()
callback and makes huge number of comparisons (in a bit random way). And last but not least – since we're checking anchor and focus first, then checking ranges again will for a collapsed selection tripple the amount of work and for a single range non collapsed selection double it.
Therefore, I've been asking about the order of ranges (whether it shouldn't be the same in both selections) because the easiest possible implementation is to loop through both selections' ranges and compare the pairs. Not only this will be much faster, but also cleaner.
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.
Okay, I see.
Ranges are not sorted upon insertion. This means that we have to check whether every range in selection A has a matching range in selection B the way we do. If the ranges are ordered that would be easier.
It is a matter of us deciding whether the order range is important. Being strict, I think that we should not care about the ranges order, because from "outside" selection: [ 1, 4 ], [ 5, 7 ], [ 8, 9 ] forward
behaves exactly the same as [ 5, 7 ], [ 1, 4 ], [ 8, 9 ] forward
. On the other hand, the amount of situations when this will be important is close to 0.
I can change the implementation but... To be honest selection will usually have just one range, except of tables where it may be more but in 99% scenarios it will will be less than 10-20 ranges. I agree with changing so the arrays are not created. If you want to change "order logic" I'm fine with that as well, just confirm please whether we are doing it or leaving it as is.
And last but not least – since we're checking anchor and focus first, then checking ranges again will for a collapsed selection triple.
So, instead one operation we have three? And it is me who get complaints about premature optimization? :)
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.
was rather asking why is this method trying to find a range in the other selection which matches range from this selection... for each range in this selection
BTW. I don't know if you haven't got too confused...
For each range in selection A, we check if there is a matching range in selection B. The only way we can "speed" this up is if we already have sorted arrays... (or care about the order).
setTimeout( () => { | ||
expect( spy.called ).to.be.false; | ||
done(); | ||
}, 1500 ); |
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 there's a chance to avoid (or shorten) such tests, then please do.
I'm trying to match this code to the comment that you left me on priv. Is this really comparing the selection as you described it – after "unifying" it by conversion to model and back? In general, I like the idea very much. I'm not if it will be perfect (there are tricky cases, especially with block widgets), but the improved inf loop check should catch them. So I'm +1, but I'd like to understand how this code works :D. PS. It's not review ready, right? You wrote something about the tests. If you were worried whether it makes sense to work on them, then for me it does. |
This solution prevents creating infinite loop by not setting model selection if it has not changed. This results in not firing renderer and breaks the loop. It's done here. This was problem number one and it actually caused an infinite loop if not for the Your idea with checking time is good to. Conceptually it's similar to what I've done (we both want to check if something happens between selection changes) but I like your idea better. As far as tests are concerned, I feel like testing DOM selection behavior in unit test might be a mistake due to asynchronous nature of selectonchange event. I had to pick correct params for tests (timeouts) so they won't fail (just like I did on physics labs when I was studying 🎓 ). I think I'd rather have some manual tests for those cases. However, in my mind this is ready to review. There is just another (the first) solution available too. However I like this one better because less things changed and it works on model instead of view/DOM. In the first solution we check whether new view selection is touching old view selection and do not change it if they do. This means that this change: |
OK, now all it's clear. So the only thing that I'd like to improve before the review is the way how we test this. I agree with you that this deserves a manual test. Let's try to avoid such complicated (and slow) automated tests. |
I've changed |
@@ -179,12 +179,23 @@ export default class SelectionObserver extends Observer { | |||
this._loopbackCounter = 0; | |||
} | |||
|
|||
if ( this._loopbackCounter > 10 ) { | |||
if ( this._loopbackCounter > 50 ) { |
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.
Deserves some comment that you empirically tested that making more than 50 selection changes in 2s is not possible. BTW. What about making selections by dragging over the text?
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.
BTW. What about making selections by dragging over the text?
The selection has to be same as previous or pre-previous selection, so you would have to drag precisly, which means that it would be probably impossible to do 50 changes in 2 secs.
I merged master to this branch and fixed the manual test (which now should always be inside |
Interesting. This is my status:
On Travis there's less red tests, but the coverage is very low ;| |
I've got this on master now:
So it seems that some failing things outside of tests (in |
OK, the trick here is that the order of running tests on CI and locally may be a bit different. I've noticed e.g. that when I executed This means that some tests are not cleaning up after themselves correctly. |
BTW. running |
…ng the observer (so view#selChange won't be fired).
…s hard to find because it depended on how you executed the tests.
OK, I fixed some smaller things in the tests, but there's one, huge issue which is this ckeditor/ckeditor5#114 (comment). Run:
Open the debug mode (if you go to the Chrome window opened by Karma there's "debug mode" button), open console and run the tests there. See that there are multiple errors thrown. They are thrown because some old selection observers are not destroyed and affect other tests. There's no other solution, IMO, than working on view document destroy chain which at least should destroy observers which at least should remove all native event listeners. To do that we need DomEmitterMixin in ckeditor5-utils. It will need to be mixed into Observer class I guess. Then, using The last step is to ensure that |
@@ -103,6 +105,8 @@ export default class SelectionObserver extends Observer { | |||
|
|||
domDocument.addEventListener( 'selectionchange', () => this._handleSelectionChange( domDocument ) ); | |||
|
|||
setInterval( () => this._clearInfiniteLoop(), 2000 ); |
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.
This interval needs to be cleaned on destroy()
.
I've made changes to the commented parts of code and also worked on destroy chain for I've also, hopefully, got rid of inf-selection-loop warning in tests for good :). |
@@ -274,6 +277,8 @@ describe( 'EditingController', () => { | |||
} ); | |||
|
|||
expect( spy.called ).to.be.false; | |||
|
|||
editing.destroy(); | |||
} ); |
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.
I miss a test that destroy() destroys the view.
Manually all this works perfectly. The remaining issues are:
|
I've added requested test and changed implementation of |
This is an alternative way of solving ckeditor/ckeditor5#3843 (previous solution is in ckeditor/ckeditor5#3887)