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

walkUp function #20

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

walkUp function #20

wants to merge 5 commits into from

Conversation

adamjanicki2
Copy link
Contributor

A part of use cases discussed in #11

Copy link

netlify bot commented Mar 12, 2024

Deploy Preview for treecle ready!

Name Link
🔨 Latest commit f3fb087
🔍 Latest deploy log https://app.netlify.com/sites/treecle/deploys/6605bd5364659f00087265f6
😎 Deploy Preview https://deploy-preview-20--treecle.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

src/walkUp.js Outdated Show resolved Hide resolved
src/walkUp.js Outdated Show resolved Hide resolved
src/walkUp.js Outdated Show resolved Hide resolved
Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

See comments. Also, please run ESLint and fix issues.

@adamjanicki2
Copy link
Contributor Author

Added in a call to assertParentPointers here

if (ret !== undefined) {
return ret;
}
assertParentPointers.call(this, node, "Cannot walk up the tree from a node with no parent pointer.");
Copy link
Member

Choose a reason for hiding this comment

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

Can we print something more useful? Since we check on every node, it would help debugging to know which node actually failed.

*/
export default function walkUp (node, callback) {
while (node) {
const ret = callback(node);
Copy link
Member

Choose a reason for hiding this comment

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

Currently even in a tree with zero parent pointers you get the callback executed once. Is that what's best for the use cases that use this? Not a rhetorical question, I don't know. We need to look at the code using this function to see what would work well for these use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main issue I see is the fact that we could execute an arbitrary number of callbacks before running into an error in the case where some ancestor node n nodes away doesn't have parent pointers set. The best option might even be to walk up the entire tree before executing any callbacks, and asserting parent pointers, and then after completing the first walk up, doing a second pass where you execute the callback. But of course, this also has a performance increase factor of 2, but might be worth it! Of course, if the callback didn't do any sort of mutation, then it would be safe to call before discovering an error, but I don't think we can count on that. I'd probably lean toward the above idea (walking up twice). What do you think @LeaVerou?

import copy from "./utils/copy.js";

const tree = copy(trees[0]);
updateParents(tree);
Copy link
Member

Choose a reason for hiding this comment

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

Might want to have some throws tests too, for when we don't have parents. Or should that just be handled in the parents tests?

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.

None yet

2 participants