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

Add safe first_node fn #2094

Merged
merged 1 commit into from
Oct 2, 2021
Merged

Add safe first_node fn #2094

merged 1 commit into from
Oct 2, 2021

Conversation

mc1098
Copy link
Contributor

@mc1098 mc1098 commented Oct 1, 2021

first_node function can fail, like when the VNode
is not mounted, so this adds a safe version that returns an
Option<Node> and refactors the previous version to use the "unchecked"
prefix as it can and should panic if the first node cannot be found.

This change resolves @intendednull's particular issue and there seems to be
a few different versions of panics that occur that all have this function in common
so it might fix them all. I think the issue comes about when trying to insert a
node before a component that is no longer mounted - trying to find that component's
first node fails and causes the panic when we could just ignore it and then render the
node on its own.

Description

Fixes #1946
If this is accepted before it is merged a caveat I have is - I can't accept the reward so
I don't know whether @intendednull can cancel the funded issue or whether
we have to? (not quite sure how it works 🙃)

Checklist

  • I have run cargo make pr-flow
  • I have reviewed my own code
  • I have added tests

`first_node` function can fail and in some situations, when the VNode
has not been mounted yet, so this adds a safe version that returns an
`Option<Node>` and refactors the previous version to use the "unchecked"
prefix as it can and should panic if the first node cannot be found.
@mc1098 mc1098 added the A-yew Area: The main yew crate label Oct 1, 2021
@siku2 siku2 merged commit ae2ebc9 into yewstack:master Oct 2, 2021
@mc1098 mc1098 deleted the safe-first-node branch October 2, 2021 11:41
.map(JsCast::unchecked_into),
VNode::VComp(vcomp) => vcomp.node_ref.get(),
VNode::VList(vlist) => vlist.get(0).and_then(VNode::first_node),
VNode::VRef(node) => Some(node.clone()),

Choose a reason for hiding this comment

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

VNode::VList(vlist) => vlist.get(0).and_then(VNode::first_node)

would vlist be empty and cause out of index range error?

Copy link
Member

@siku2 siku2 Dec 4, 2021

Choose a reason for hiding this comment

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

VList::get is the same as Vec::get, so it returns Option::None when the index is out of bounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew Area: The main yew crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VComp is not mounted
3 participants