-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Add children getter to entity #10324
Conversation
Thanks for the pull request @jdfwarrior!
Reviewers, don't forget to make sure that:
|
Thanks for the PR @jdfwarrior. @mramato Is there a reason we don't currently expose |
From #2600
So there's no explicit reason other than "Are we sure this won't create problems or cases where it doesn't work as expected". It's been a while, so my memory is fuzzy but from what I remember the API itself isn't really set up for it, for example, what should happen if an entity has a parent and you push it onto the children array of another entity? Ideally the If someone thinks through all of the edge cases and documents/handles them in a sensible way, I have no objections |
@ggetz @mramato And so, ust for clarification, thats why a setter wasn't even looked at. I only made the getter. I don't want to be able to manipulate it. For the application that I currently work on, I have UI components that, when I click on an entity, I have my own custom info box that appears and one of the sections within it, lists the entities that are children of the current selected entity. If this is approved, my next step would be to look into adding an event for when the childrens list is updated so that I have an event to trigger an update of the list. |
Source/DataSources/Entity.js
Outdated
/** | ||
* Gets the list of all children currently associated with this entity | ||
* @memberof Entity.prototype | ||
* @type {Entity[]} |
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.
* @type {Entity[]} | |
* @readonly | |
* @type {Entity[]} |
This will mark the property explicitly as read only in the docs and the generated typescript definitions.
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.
Also I believe what @mramato was alluding to is that while, yes, this is only a getter and not a setter, its possible to get the array of children and then mutate it by adding or removing elements. Either of which could have adverse effects.
We work around this in other classes such as ClippingPlaneCollection
by not providing direct access to the internal array, and instead providing interfaces for getting the array length (length
) and accessing each element by index (get(i)
).
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.
@ggetz So is there any point in adding the @readonly argument to the jsdoc block or should there just be a different implementation of this?
We could also spread or copy the original array to prevent it from being mutated as well. If that is considered a viable alternative, I can make that change. If so, seeing as how there are multiple methods of cloning an array, is there a preferred method?
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.
So is there any point in adding the @readonly argument to the jsdoc block or should there just be a different implementation of this?
Not if this specific getter goes away, I just wanted to quickly point it out in case it comes up again.
We could also spread or copy the original array to prevent it from being mutated as well.
I see what you're saying, but I'd hesitate to recommend this way. I don't see anything similar in the rest of the codebase, most likely for performance reasons. It could get expensive if the getter is called frequently and if there are enough entities.
Hi @jdfwarrior, do you think you'll get back to this shortly? If not, in order to keep things tidy, I would ask to close this until then and re-open when it's ready for another review. |
@ggetz Yeah I'd love to get this done. I guess I just want to know what the implementation is that the team would actually want. From what it seemed, the getter is acceptable except for the fact that, even though it's a getter, because it's an array it can still be mutated, which, is understandable. Is returning a copy of the array 100% off the table as an option? There are several options for doing this. You could spread or slice the original array to create a new one. I can't unequivocally say that nobody would ever do it but, I don't see a case where anyone would ever want to call this repeatedly in a tight loop where performance would become an issue. It's a simple object but I created an object with several properties and filled an array with 100k of that object. Just using a basic console.time to check time on the spread and slice, both seemed to copy the array in 1-2ms over and over. If returning a copy of the array is still considered not to be a good option and you'd prefer that I use a method to get children at a certain index, I can do that. Would there be a recommended function name? The other areas that I know of that functions like that exist are on objects that are collections, |
Yes, I think we want to go that route for consistency with the rest of the API.
Good point. I think |
… method to get child at index
Ok. I removed the previous Does this seem like a better approach? |
Thanks again for your contribution @jdfwarrior! No one has commented on this pull request in 90 days. Maintainers, can you review, merge or close to keep things tidy? I'm going to re-bump this in 90 days. If you'd like me to stop, just comment with |
4 similar comments
Thanks again for your contribution @jdfwarrior! No one has commented on this pull request in 90 days. Maintainers, can you review, merge or close to keep things tidy? I'm going to re-bump this in 90 days. If you'd like me to stop, just comment with |
Thanks again for your contribution @jdfwarrior! No one has commented on this pull request in 90 days. Maintainers, can you review, merge or close to keep things tidy? I'm going to re-bump this in 90 days. If you'd like me to stop, just comment with |
Thanks again for your contribution @jdfwarrior! No one has commented on this pull request in 90 days. Maintainers, can you review, merge or close to keep things tidy? I'm going to re-bump this in 90 days. If you'd like me to stop, just comment with |
Thanks again for your contribution @jdfwarrior! No one has commented on this pull request in 90 days. Maintainers, can you review, merge or close to keep things tidy? I'm going to re-bump this in 90 days. If you'd like me to stop, just comment with |
It's kind of crazy that something so small sat here for over a year |
Adds a
children
getter to the Entity prototype so that you can retrieve a list of children associated to the entity without having to access the private_children
property of the entity.It does not provide a setter since that is outside of the scope of my needs. I currently use the
_children
property but causes issues in TypeScript because I'm accessing a property that it believes doesn't exist on the object.