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

Fix scope_depth to return 0 for root scope #424

Merged
merged 4 commits into from
May 25, 2022

Conversation

mc1098
Copy link
Contributor

@mc1098 mc1098 commented May 20, 2022

Hi 👋,

Just a quick fix so that the scope_depth function returns 0 for the root scope as stated by the comment -
I also added some tests for the function too.

This function is only ever used in this repository for the following SSR test:

#[test]
fn using_cx_in_dyn_node_creates_nested_scope() {
let _ = sycamore::render_to_string(|cx| {
let outer_depth = scope_depth(cx);
let inner_depth = create_ref(cx, Cell::new(0));
let node = view! { cx,
p {
({
inner_depth.set(scope_depth(cx));
view! { cx, }
})
}
};
assert_eq!(inner_depth.get(), outer_depth + 1);
node
});
}

that relies solely on the fact that depth is incremented on child scopes.

@codecov
Copy link

codecov bot commented May 20, 2022

Codecov Report

Merging #424 (f11d992) into master (487877f) will increase coverage by 0.09%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #424      +/-   ##
==========================================
+ Coverage   64.87%   64.96%   +0.09%     
==========================================
  Files          52       52              
  Lines        8256     8278      +22     
==========================================
+ Hits         5356     5378      +22     
  Misses       2900     2900              
Impacted Files Coverage Δ
packages/sycamore-reactive/src/context.rs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 487877f...f11d992. Read the comment docs.

Copy link
Member

@lukechu10 lukechu10 left a comment

Choose a reason for hiding this comment

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

I left a few comments. Also thanks for adding tests!

Comment on lines 114 to 117
fn unchecked_as_ref(x: *const ScopeRaw) -> &ScopeRaw {
// SAFETY: `current.parent` necessarily lives longer than `current`.
this = current.parent.map(|x| unsafe { &*x });
unsafe { &*x }
}
Copy link
Member

Choose a reason for hiding this comment

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

Although this is a scoped function, I believe it technically still needs to be unsafe. In that case, it might just be better to inline unsafe { &*x }

Comment on lines 119 to 126
let mut depth = 0;
let mut next = cx.raw.parent.map(unchecked_as_ref);

while let Some(current) = next {
next = current.parent.map(unchecked_as_ref);
depth += 1;
}
depth
Copy link
Member

Choose a reason for hiding this comment

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

I have another idea of an implementation that might be simpler:

while let Some(next) = current.parent.map(unsafe { &*x }) { ... }

@lukechu10 lukechu10 merged commit bc30dfa into sycamore-rs:master May 25, 2022
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.

2 participants