Skip to content

Conversation

@krispya
Copy link
Member

@krispya krispya commented Mar 9, 2025

This guarantees that the React instance children array does not desync from the Three object chidlren array. However, it is computationally expensive to be searching all of these arrays each time so I am keen to think of ways to optimize these operations.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 9, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c6bbc0f:

Sandbox Source
example Configuration

@krispya
Copy link
Member Author

krispya commented Mar 31, 2025

@CodyJasonBennett I wanted to come back to this as desyncs currently happen whenever a list is updated. I know we weren't happy to turn the push into an insert, but this emulates the DOM behavior of appendChild where it moves the child to the end of the array if it is already a child: https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

Maybe we can think of a faster algorithm?

Comment on lines -340 to 348
if (!child) return

// Link instances
child.parent = parent

// Remove existing child if it exists
const existingIndex = parent.children.indexOf(child)
if (existingIndex !== -1) parent.children.splice(existingIndex, 1)

// Append child
parent.children.push(child)
Copy link
Member

@CodyJasonBennett CodyJasonBennett Apr 6, 2025

Choose a reason for hiding this comment

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

#3490 (comment) Maybe we can move the check before mutating anything.

// Remove existing child if it exists (re-order to last)
if (child.parent === parent) {
  const existingIndex = parent.children.indexOf(child)
  if (existingIndex !== -1) parent.children.splice(existingIndex, 1)
}

// Link instances
child.parent = parent

// Append child
parent.children.push(child)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes this makes sense. I think further optimizations would require convincing React to provide a path for append separate from reorder since it has this information internally. However, for now we can probably not worry about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants