Skip to content

Commit 0eb58be

Browse files
committed
Finish debugging.
1 parent 2b1156f commit 0eb58be

File tree

2 files changed

+32
-53
lines changed

2 files changed

+32
-53
lines changed

packages/yew-router/src/router.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use std::borrow::Cow;
33
use std::rc::Rc;
44

5-
use gloo::console::console_dbg;
65
use yew::prelude::*;
76
use yew::virtual_dom::AttrValue;
87

@@ -78,35 +77,25 @@ fn base_router(props: &RouterProps) -> Html {
7877
let basename = basename.map(|m| strip_slash_suffix(&m).to_string());
7978
let navigator = Navigator::new(history.clone(), basename.clone());
8079

81-
{
82-
let history = history.clone();
83-
let navigator = navigator.clone();
84-
let basename = basename.clone();
85-
let old_basename = use_state_eq(|| Option::<String>::None);
86-
console_dbg!("render");
87-
// Can't use `use_Effect_with` since need to track old and new, not just new.
88-
use_effect(move || {
89-
console_dbg!(format!("{basename:?} {:?}", *old_basename));
90-
if basename != *old_basename {
91-
// If `old_basename` is `Some`, path is probably prefixed with `old_basename`.
92-
// If `old_basename` is `None`, path may or may not be prefixed with the new `basename`,
93-
// depending on whether this is the first render.
94-
let old_navigator: Navigator = Navigator::new(
95-
history.clone(),
96-
old_basename.as_ref().or(basename.as_ref()).cloned(),
97-
);
98-
old_basename.set(basename.clone());
99-
let location = history.location();
100-
let stripped = old_navigator.strip_basename(Cow::from(location.path()));
101-
let prefixed = navigator.prefix_basename(&stripped);
102-
console_dbg!(format!("s {stripped} {prefixed}"));
103-
if false && prefixed != location.path() {
104-
history
105-
.replace_with_query(prefixed, Raw(location.query_str()))
106-
.unwrap_or_else(|never| match never {})
107-
}
108-
}
109-
});
80+
let old_basename = use_state_eq(|| Option::<String>::None);
81+
if basename != *old_basename {
82+
// If `old_basename` is `Some`, path is probably prefixed with `old_basename`.
83+
// If `old_basename` is `None`, path may or may not be prefixed with the new `basename`,
84+
// depending on whether this is the first render.
85+
let old_navigator = Navigator::new(
86+
history.clone(),
87+
old_basename.as_ref().or(basename.as_ref()).cloned(),
88+
);
89+
old_basename.set(basename.clone());
90+
let location = history.location();
91+
let stripped = old_navigator.strip_basename(Cow::from(location.path()));
92+
let prefixed = navigator.prefix_basename(&stripped);
93+
94+
if prefixed != location.path() {
95+
history
96+
.replace_with_query(prefixed, Raw(location.query_str()))
97+
.unwrap_or_else(|never| match never {});
98+
}
11099
}
111100

112101
let navi_ctx = NavigatorContext { navigator };

packages/yew-router/tests/link.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::time::Duration;
22

3-
use gloo::console::console_dbg;
43
use serde::{Deserialize, Serialize};
54
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
65
use yew::functional::function_component;
@@ -94,7 +93,7 @@ async fn link_in_browser_router() {
9493
let div = gloo::utils::document().create_element("div").unwrap();
9594
let _ = div.set_attribute("id", "browser-router");
9695
let _ = gloo::utils::body().append_child(&div);
97-
yew::Renderer::<RootForBrowserRouter>::with_root(div).render();
96+
let handle = yew::Renderer::<RootForBrowserRouter>::with_root(div).render();
9897

9998
sleep(Duration::ZERO).await;
10099

@@ -113,6 +112,8 @@ async fn link_in_browser_router() {
113112
"/search?q=Rust&lang=en_US",
114113
link_href("#browser-router ul > li.search-q-lang > a")
115114
);
115+
116+
handle.destroy();
116117
}
117118

118119
#[derive(PartialEq, Properties)]
@@ -122,7 +123,6 @@ struct BasenameProps {
122123

123124
#[function_component(RootForBasename)]
124125
fn root_for_basename(props: &BasenameProps) -> Html {
125-
console_dbg!("outer render");
126126
html! {
127127
<BrowserRouter basename={props.basename.clone()}>
128128
<NavigationMenu />
@@ -142,16 +142,12 @@ async fn link_with_basename() {
142142
)
143143
.render();
144144

145-
for _ in 0..10 {
146-
sleep(Duration::from_millis(100)).await;
147-
}
145+
sleep(Duration::ZERO).await;
148146

149-
/*
150147
assert_eq!(
151148
"/base/",
152149
gloo::utils::window().location().pathname().unwrap()
153150
);
154-
*/
155151

156152
assert_eq!("/base/posts", link_href("#with-basename ul > li.posts > a"));
157153
assert_eq!(
@@ -177,18 +173,12 @@ async fn link_with_basename() {
177173
basename: Some("/bayes/".to_owned()),
178174
});
179175

180-
for _ in 0..10 {
181-
sleep(Duration::from_millis(100)).await;
182-
}
183-
184-
console_dbg!("before assert");
176+
sleep(Duration::ZERO).await;
185177

186-
/*
187178
assert_eq!(
188179
"/bayes/",
189180
gloo::utils::window().location().pathname().unwrap()
190181
);
191-
*/
192182

193183
assert_eq!(
194184
"/bayes/posts",
@@ -198,29 +188,27 @@ async fn link_with_basename() {
198188
// Some -> None
199189
handle.update(BasenameProps { basename: None });
200190

201-
for _ in 0..10 {
202-
sleep(Duration::from_millis(100)).await;
203-
}
191+
sleep(Duration::ZERO).await;
204192

205193
assert_eq!("/", gloo::utils::window().location().pathname().unwrap());
206194

207195
assert_eq!("/posts", link_href("#with-basename ul > li.posts > a"));
208196

209197
// None -> Some
210198
handle.update(BasenameProps {
211-
basename: Some("bass".to_string()),
199+
basename: Some("/bass/".to_string()),
212200
});
213201

214-
for _ in 0..10 {
215-
sleep(Duration::from_millis(100)).await;
216-
}
202+
sleep(Duration::ZERO).await;
217203

218204
assert_eq!(
219205
"/bass/",
220206
gloo::utils::window().location().pathname().unwrap()
221207
);
222208

223209
assert_eq!("/bass/posts", link_href("#with-basename ul > li.posts > a"));
210+
211+
handle.destroy();
224212
}
225213

226214
#[function_component(RootForHashRouter)]
@@ -236,7 +224,7 @@ async fn link_in_hash_router() {
236224
let div = gloo::utils::document().create_element("div").unwrap();
237225
let _ = div.set_attribute("id", "hash-router");
238226
let _ = gloo::utils::body().append_child(&div);
239-
yew::Renderer::<RootForHashRouter>::with_root(div).render();
227+
let handle = yew::Renderer::<RootForHashRouter>::with_root(div).render();
240228

241229
sleep(Duration::ZERO).await;
242230

@@ -255,6 +243,8 @@ async fn link_in_hash_router() {
255243
"#/search?q=Rust&lang=en_US",
256244
link_href("#hash-router ul > li.search-q-lang > a")
257245
);
246+
247+
handle.destroy();
258248
}
259249

260250
// These cannot be run in concurrently because they all read/write the URL.

0 commit comments

Comments
 (0)