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

Remove extra loop (?) #11889

Merged
merged 2 commits into from
Jan 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions packages/react-dom/src/client/ReactDOMComponentTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export function getClosestInstanceFromNode(node) {
return node[internalInstanceKey];
}

// Walk up the tree until we find an ancestor whose instance we have cached.
let parents = [];
while (!node[internalInstanceKey]) {
parents.push(node);
if (node.parentNode) {
node = node.parentNode;
} else {
Expand All @@ -40,17 +37,13 @@ export function getClosestInstanceFromNode(node) {
}
}

let closest;
let inst = node[internalInstanceKey];
if (inst.tag === HostComponent || inst.tag === HostText) {
// In Fiber, this will always be the deepest root.
return inst;
}
for (; node && (inst = node[internalInstanceKey]); node = parents.pop()) {
closest = inst;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried to check back through the history to see what was going on here, and at least originally it used to try and precache nodes: 6d20556#diff-a2fd175216a5aa6425363e6773426f3c

But at some point that was removed but I can't find git blame is not helping :/

Then

Copy link
Contributor

Choose a reason for hiding this comment

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

When does line 50 trigger?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I mean like what code path causes closest to be assigned to the instance? And what codepath even lets this function get past return inst on line 47?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think in practical usage it doesn't get past 47, but I guess theoretically it could, if the fiber was a different type?

My guess tho is that this was for both stack and fiber and never got removed. Though even if it could get past 47 I don't think the loop does anything other than run one iteration, and return inst

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool. I'd love to drop this if the code path no longer fires after Stack's removal. I'm afraid to ping anyone to bug them on holiday, but I think it's worth investigating.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no rush clearly. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

It was hard to find due to a couple project reorganizations nuking the file history, but this was removed in #10798. I think the most likely explanation is that the whole loop could have been removed as well, but it wasn't clear that it was stack-only.

It's a little confusing, but at first glance it seems safe to remove 😄


return closest;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to return null for invalid fiber types? Maybe it should throw if it's not a HostComponent or HostText? I'm not sure if there are any situations where this should be called with any other type of node.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's a good question. I think your probably right, however i was trying to keep with the spirit (for lack of a better term) of the method by failing softly.

I do think tho that a situation where its not a component or text is kind of a different sort of error and maybe should fail hard. Are roots or portals cached on elements? Presumable you should hit a component before that tho

return null;
}

/**
Expand Down