Fix: occasional incorrect tree order in getChildrenAtPosition
#583
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #554 - getChildrenByPosition wrong order
Might fix #129 - zIndexedChildren order obsolete when out of bounds
Issue
Stage.getChildrenByPosition(x, y)collects elements at the (global)x, ylocation, internally callingElementCore.collectAtCoord(x, y, children)Internally,
collectAtCoordsorts the result usingElementCore.sortZIndexedChildren. The issue with this sorting function is that it doesn't just sort byzIndex, but also bycore._updateTreeOrder, but this value can be wrong.One may think that
this.root.core.update()would ensure the tree order is correct, but this is wrong: tree ordering depends onctx.updateTreeOrderto be reset before running the update.The result is randomly wrong elements ordering:
ctx.updateTreeOrder,Stagecallsroot.core.update(), some parts of the tree may be ordered based on the current (non reinitialised) value ofctx.updateTreeOrder, resulting in some subtree having randomly a "high" tree order value, meaning these elements will appear to be on top during in the resultingcollectAtCoord.Solution
Stageshould only request a tree order update, ensuringctx.updateTreeOrderis properly reset.Additionally,
collectAtCoordquite inefficiently re-sorts the children on every iteration, so it's better to only sort at the very end.Testing
This issue was happening in our (large and complex) Lightning application in a fairly consistent way (but very hard to extract in a sample).
We did work around it by doing a brute-force updating of all the elements' tree order before calling
root.collectAtCoorddirectly. Now this PR attempts at fixing the root cause properly and for all Lightning users.We could confirm this finer fix allowed us to remove our workaround without causing regressions. However we don't use any
zIndexso can't verify this use case.