Skip to content
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

refactor() change common ancenstors returned data to help isInFrontOf #7906

Merged
merged 7 commits into from
Apr 28, 2022

Conversation

asturur
Copy link
Member

@asturur asturur commented Apr 26, 2022

Have findCommonAncestors return 3 arrays, representing, the common ancestors, and the forks of non common ancestors down to the objects themselves.

Rules:

  • an object is not parent of itself
  • there is always something to return from findCommonAncestors ( at worst 2 disjunted forks )
  • an empty array of common ancestors means no common ancestors, regardless of where objects live ( same canvas, different apps entirely )
  • EDITED common ancestors can include this | other, forks are completely different

@asturur asturur requested a review from ShaMan123 April 26, 2022 07:25
@asturur
Copy link
Member Author

asturur commented Apr 26, 2022

@ShaMan123 this is how i would like to change findCommonAncestors so that we don't have to repeat the logic in isInFronOf. Code amount is the same ( i think i added 3 lines ) but the logic is less.
Also i find the data returned in this case from findCommonAncestors more easily understandble compared to indexes of arrays we don't have anymore.

Let me know what do you think

Copy link
Contributor

@ShaMan123 ShaMan123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much better!

  1. Now I think of another change that will make it more comprehensible.
    I think we should rename ancestors prop to common
    so the return value will be an object of fork, otherFork, common.
    Then we handle the 2 cases of this other that are descendants of each other as follows:
    If b is a child of a then:
var retVal = b.findCommonAncestors(a);
retVal === { fork: [b], otherFork: [], common: [a] }

instead of

var retVal = b.findCommonAncestors(a);
retVal === { fork: [b, a], otherFork: [a], ancestors: [] }

because strictly thinking of the meaning of fork isn't suitable to how it's used currently.

  1. The other suggestion is logic. Don't you prefer unshifting the ancestor arrays and removing the equality checks of this === otherAncestor[j] | other=== ancestor[i]
    I think it's less confusing.

  2. Maybe add some more comments as it is logically complicated.

  3. we might want to rename the method to compareTrees or compareAncestors

@ShaMan123
Copy link
Contributor

updated review comment

@asturur
Copy link
Member Author

asturur commented Apr 26, 2022

The only reason for not unshifiting is not readibility, but is that once you put the object inside the array, you don't know anymore which is a parent and which not. If it stay outside for the internal logic is better.

I have no issue with having the function oriented on commonTree VS commonAncestors, but if there is not a direct practical advantage in doing that now, i prefer to move forward.

@asturur
Copy link
Member Author

asturur commented Apr 26, 2022

How else this function is used? right now just for isInFrontOf and is doing its job fine enough.

@ShaMan123
Copy link
Contributor

Only isInFrontOf uses it. but devs will use it probably. And it is healthier what I suggested.
As you say, we can iterate later on. I don't mind patching it now.
I leave the decision at your hands.

@ShaMan123
Copy link
Contributor

Ahh the return value needs to be changed to utilize the typedef Ancestors

@asturur
Copy link
Member Author

asturur commented Apr 26, 2022

Only isInFrontOf uses it. but devs will use it probably. And it is healthier what I suggested. As you say, we can iterate later on. I don't mind patching it now. I leave the decision at your hands.

What does it mean healtier? in this case we have 3 parts, the common trunk and the branches. in the other case we have a slightly different point of view of the same data structure.
Where is the advantage?

@ShaMan123
Copy link
Contributor

The advantage is that the forks are entirely different.
Currently they might have the same top ancestor. I think it's confusing.

@ShaMan123
Copy link
Contributor

ShaMan123 commented Apr 26, 2022

I have in mind what you said about absolutePositioned - once it's out there we can't take it back as easily.
That's why we should commit something mature/final.

@asturur
Copy link
Member Author

asturur commented Apr 27, 2022

no they can't have the same top ancestor right now, how would that happen?

@ShaMan123
Copy link
Contributor

I meant 2 forks can have the same headOfFork.

@ShaMan123
Copy link
Contributor

Pushed 2319d4f
Are you OK with it?

@@ -62,28 +66,37 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
}
var ancestors = this.getAncestors(strict);
var otherAncestors = other.getAncestors(strict);
// if `this` has no ancestors and `this` is top ancestor of `other` we must handle the following case
Copy link
Contributor

@ShaMan123 ShaMan123 Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this condition should come after the for loop for readability

@asturur
Copy link
Member Author

asturur commented Apr 28, 2022

I meant 2 forks can have the same headOfFork.

Not sure why that would happen, but is fine by me changing, i wouldn't have re-typed everything, but since you did is fine.
I find the difference a matter of preference.

Pushed 2319d4f
Are you OK with it?

In general try to don't push on others PRs without a prior agreement.

@asturur asturur merged commit 97886a6 into v6-group-patch2 Apr 28, 2022
@asturur asturur deleted the discuss-common-ancenstor branch September 11, 2022 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants