Skip to content

Commit 5010f34

Browse files
committed
Merge remote-tracking branch 'upstream/master' into dom-bundle
2 parents c713027 + 81bce94 commit 5010f34

97 files changed

Lines changed: 3276 additions & 1886 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<a href="https://crates.io/crates/yew"><img alt="Crate Info" src="https://img.shields.io/crates/v/yew.svg"/></a>
1212
<a href="https://docs.rs/yew/"><img alt="API Docs" src="https://img.shields.io/badge/docs.rs-yew-green"/></a>
1313
<a href="https://discord.gg/VQck8X4"><img alt="Discord Chat" src="https://img.shields.io/discord/701068342760570933"/></a>
14-
<a href="https://gitlocalize.com/repo/4999/whole_project?utm_source=badge"> <img src="https://gitlocalize.com/repo/4999/whole_project/badge.svg" /> </a>
14+
<a href="https://gitlocalize.com/repo/7052/whole_project?utm_source=badge"> <img src="https://gitlocalize.com/repo/7052/whole_project/badge.svg" /> </a>
1515
<a href="https://blog.rust-lang.org/2020/12/31/Rust-1.56.0.html"><img alt="Rustc Version 1.56.0+" src="https://img.shields.io/badge/rustc-1.56%2B-lightgrey.svg"/></a>
1616
</p>
1717

@@ -70,7 +70,7 @@ Please [report all bugs!](https://github.com/yewstack/yew/issues/new/choose) We
7070

7171
#### 🤓 Want to help translate?
7272

73-
Translations can be submitted on the [Yew GitLocalize Repo](https://gitlocalize.com/repo/4999). If you are interested in being the official moderator for a language, please reach out on Discord.
73+
Translations can be submitted on the [Yew GitLocalize Repo](https://gitlocalize.com/repo/7052). If you are interested in being the official moderator for a language, please reach out on Discord.
7474

7575
## Contributors
7676

packages/yew-router/tests/basename.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use gloo::timers::future::sleep;
12
use serde::{Deserialize, Serialize};
3+
use std::time::Duration;
24
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
35
use yew::functional::function_component;
46
use yew::prelude::*;
@@ -112,19 +114,29 @@ fn root() -> Html {
112114
// - query parameters
113115
// - 404 redirects
114116
#[test]
115-
fn router_works() {
117+
async fn router_works() {
116118
yew::start_app_in_element::<Root>(gloo::utils::document().get_element_by_id("output").unwrap());
117119

120+
sleep(Duration::ZERO).await;
121+
118122
assert_eq!("Home", obtain_result_by_id("result"));
119123

124+
sleep(Duration::ZERO).await;
125+
120126
let initial_length = history_length();
121127

128+
sleep(Duration::ZERO).await;
129+
122130
click("button"); // replacing the current route
131+
132+
sleep(Duration::ZERO).await;
123133
assert_eq!("2", obtain_result_by_id("result-params"));
124134
assert_eq!("bar", obtain_result_by_id("result-query"));
125135
assert_eq!(initial_length, history_length());
126136

127137
click("button"); // pushing a new route
138+
139+
sleep(Duration::ZERO).await;
128140
assert_eq!("3", obtain_result_by_id("result-params"));
129141
assert_eq!("baz", obtain_result_by_id("result-query"));
130142
assert_eq!(initial_length + 1, history_length());

packages/yew-router/tests/browser_router.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use gloo::timers::future::sleep;
12
use serde::{Deserialize, Serialize};
3+
use std::time::Duration;
24
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
35
use yew::functional::function_component;
46
use yew::prelude::*;
@@ -112,19 +114,29 @@ fn root() -> Html {
112114
// - query parameters
113115
// - 404 redirects
114116
#[test]
115-
fn router_works() {
117+
async fn router_works() {
116118
yew::start_app_in_element::<Root>(gloo::utils::document().get_element_by_id("output").unwrap());
117119

120+
sleep(Duration::ZERO).await;
121+
118122
assert_eq!("Home", obtain_result_by_id("result"));
119123

124+
sleep(Duration::ZERO).await;
125+
120126
let initial_length = history_length();
121127

128+
sleep(Duration::ZERO).await;
129+
122130
click("button"); // replacing the current route
131+
132+
sleep(Duration::ZERO).await;
123133
assert_eq!("2", obtain_result_by_id("result-params"));
124134
assert_eq!("bar", obtain_result_by_id("result-query"));
125135
assert_eq!(initial_length, history_length());
126136

127137
click("button"); // pushing a new route
138+
139+
sleep(Duration::ZERO).await;
128140
assert_eq!("3", obtain_result_by_id("result-params"));
129141
assert_eq!("baz", obtain_result_by_id("result-query"));
130142
assert_eq!(initial_length + 1, history_length());

packages/yew-router/tests/hash_router.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use gloo::timers::future::sleep;
12
use serde::{Deserialize, Serialize};
3+
use std::time::Duration;
24
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
35
use yew::functional::function_component;
46
use yew::prelude::*;
@@ -112,19 +114,29 @@ fn root() -> Html {
112114
// - query parameters
113115
// - 404 redirects
114116
#[test]
115-
fn router_works() {
117+
async fn router_works() {
116118
yew::start_app_in_element::<Root>(gloo::utils::document().get_element_by_id("output").unwrap());
117119

120+
sleep(Duration::ZERO).await;
121+
118122
assert_eq!("Home", obtain_result_by_id("result"));
119123

124+
sleep(Duration::ZERO).await;
125+
120126
let initial_length = history_length();
121127

128+
sleep(Duration::ZERO).await;
129+
122130
click("button"); // replacing the current route
131+
132+
sleep(Duration::ZERO).await;
123133
assert_eq!("2", obtain_result_by_id("result-params"));
124134
assert_eq!("bar", obtain_result_by_id("result-query"));
125135
assert_eq!(initial_length, history_length());
126136

127137
click("button"); // pushing a new route
138+
139+
sleep(Duration::ZERO).await;
128140
assert_eq!("3", obtain_result_by_id("result-params"));
129141
assert_eq!("baz", obtain_result_by_id("result-query"));
130142
assert_eq!(initial_length + 1, history_length());

packages/yew/src/dom_bundle/app_handle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ where
3737

3838
/// Schedule the app for destruction
3939
pub fn destroy(self) {
40-
self.scope.destroy()
40+
self.scope.destroy(false)
4141
}
4242
}
4343

packages/yew/src/dom_bundle/bcomp.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ impl fmt::Debug for BComp {
4040
}
4141

4242
impl DomBundle for BComp {
43-
fn detach(self, _parent: &Element) {
44-
self.scope.destroy_boxed();
43+
fn detach(self, _parent: &Element, parent_to_detach: bool) {
44+
self.scope.destroy_boxed(parent_to_detach);
4545
}
4646

4747
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
@@ -264,9 +264,9 @@ impl ComponentRenderState {
264264
}
265265
}
266266
/// Detach the rendered content from the DOM
267-
pub(crate) fn detach(self) {
267+
pub(crate) fn detach(self, parent_to_detach: bool) {
268268
if let Some(ref m) = self.parent {
269-
self.root_node.detach(m);
269+
self.root_node.detach(m, parent_to_detach);
270270
}
271271
}
272272

@@ -282,14 +282,15 @@ pub trait Scoped {
282282
/// Shift the node associated with this scope to a new place
283283
fn shift_node(&self, parent: Element, next_sibling: NodeRef);
284284
/// Process an event to destroy a component
285-
fn destroy(self);
286-
fn destroy_boxed(self: Box<Self>);
285+
fn destroy(self, parent_to_detach: bool);
286+
fn destroy_boxed(self: Box<Self>, parent_to_detach: bool);
287287
}
288288

289289
#[cfg(test)]
290290
mod tests {
291291
use super::*;
292292
use crate::dom_bundle::{DomBundle, Reconcilable};
293+
use crate::scheduler;
293294
use crate::{
294295
html,
295296
virtual_dom::{Key, VChild, VNode},
@@ -341,6 +342,7 @@ mod tests {
341342

342343
let comp = html! { <Comp></Comp> };
343344
let (_, mut bundle) = comp.attach(&parent_scope, &parent_element, NodeRef::default());
345+
scheduler::start_now();
344346

345347
for _ in 0..10000 {
346348
let node = html! { <Comp></Comp> };
@@ -350,6 +352,7 @@ mod tests {
350352
NodeRef::default(),
351353
&mut bundle,
352354
);
355+
scheduler::start_now();
353356
}
354357
}
355358

@@ -504,6 +507,7 @@ mod tests {
504507
parent.set_inner_html("");
505508

506509
node.attach(scope, parent, NodeRef::default());
510+
scheduler::start_now();
507511
parent.inner_html()
508512
}
509513

@@ -563,9 +567,11 @@ mod tests {
563567
let node_ref = NodeRef::default();
564568
let elem = html! { <Comp ref={node_ref.clone()}></Comp> };
565569
let (_, elem) = elem.attach(&scope, &parent, NodeRef::default());
570+
scheduler::start_now();
566571
let parent_node = parent.deref();
567572
assert_eq!(node_ref.get(), parent_node.first_child());
568-
elem.detach(&parent);
573+
elem.detach(&parent, false);
574+
scheduler::start_now();
569575
assert!(node_ref.get().is_none());
570576
}
571577
}

packages/yew/src/dom_bundle/blist.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl BList {
151151
if lefts.len() < rights.len() {
152152
for r in rights.drain(lefts.len()..) {
153153
test_log!("removing: {:?}", r);
154-
r.detach(parent);
154+
r.detach(parent, false);
155155
}
156156
}
157157

@@ -336,7 +336,7 @@ impl BList {
336336
// Step 2.3. Remove any extra rights
337337
for KeyedEntry(_, r) in spare_bundles.drain() {
338338
test_log!("removing: {:?}", r);
339-
r.detach(parent);
339+
r.detach(parent, false);
340340
}
341341

342342
// Step 3. Diff matching children at the start
@@ -354,9 +354,9 @@ impl BList {
354354
}
355355

356356
impl DomBundle for BList {
357-
fn detach(self, parent: &Element) {
357+
fn detach(self, parent: &Element, parent_to_detach: bool) {
358358
for child in self.rev_children.into_iter() {
359-
child.detach(parent);
359+
child.detach(parent, parent_to_detach);
360360
}
361361
}
362362

packages/yew/src/dom_bundle/bnode.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ impl BNode {
4343

4444
impl DomBundle for BNode {
4545
/// Remove VNode from parent.
46-
fn detach(self, parent: &Element) {
46+
fn detach(self, parent: &Element, parent_to_detach: bool) {
4747
match self {
48-
Self::BTag(vtag) => vtag.detach(parent),
49-
Self::BText(btext) => btext.detach(parent),
50-
Self::BComp(bsusp) => bsusp.detach(parent),
51-
Self::BList(blist) => blist.detach(parent),
48+
Self::BTag(vtag) => vtag.detach(parent, parent_to_detach),
49+
Self::BText(btext) => btext.detach(parent, parent_to_detach),
50+
Self::BComp(bsusp) => bsusp.detach(parent, parent_to_detach),
51+
Self::BList(blist) => blist.detach(parent, parent_to_detach),
5252
Self::BRef(ref node) => {
53+
// Always remove user-defined nodes to clear possible parent references of them
5354
if parent.remove_child(node).is_err() {
5455
console::warn!("Node not found to remove VRef");
5556
}
5657
}
57-
Self::BPortal(bportal) => bportal.detach(parent),
58-
Self::BSuspense(bsusp) => bsusp.detach(parent),
58+
Self::BPortal(bportal) => bportal.detach(parent, parent_to_detach),
59+
Self::BSuspense(bsusp) => bsusp.detach(parent, parent_to_detach),
5960
}
6061
}
6162

packages/yew/src/dom_bundle/bportal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ pub struct BPortal {
2020
}
2121

2222
impl DomBundle for BPortal {
23-
fn detach(self, _: &Element) {
23+
fn detach(self, _: &Element, _parent_to_detach: bool) {
2424
test_log!("Detaching portal from host{:?}", self.host.outer_html());
25-
self.node.detach(&self.host);
25+
self.node.detach(&self.host, false);
2626
test_log!("Detached portal from host{:?}", self.host.outer_html());
2727
}
2828

packages/yew/src/dom_bundle/bsuspense.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ impl BSuspense {
3030
}
3131

3232
impl DomBundle for BSuspense {
33-
fn detach(self, parent: &Element) {
33+
fn detach(self, parent: &Element, parent_to_detach: bool) {
3434
if let Some(fallback) = self.fallback_bundle {
35-
fallback.detach(parent);
36-
self.children_bundle.detach(&self.detached_parent);
35+
fallback.detach(parent, parent_to_detach);
36+
self.children_bundle.detach(&self.detached_parent, false);
3737
} else {
38-
self.children_bundle.detach(parent);
38+
self.children_bundle.detach(parent, parent_to_detach);
3939
}
4040
}
4141

@@ -164,7 +164,11 @@ impl Reconcilable for VSuspense {
164164
}
165165
// Freshly unsuspended. Detach fallback from the DOM, then shift children into it.
166166
(false, Some(_)) => {
167-
suspense.fallback_bundle.take().unwrap().detach(parent);
167+
suspense
168+
.fallback_bundle
169+
.take()
170+
.unwrap() // We just matched Some(_)
171+
.detach(parent, false);
168172

169173
children_bundle.shift(parent, next_sibling.clone());
170174
children.reconcile_node(parent_scope, parent, next_sibling, children_bundle)

0 commit comments

Comments
 (0)