Skip to content

Commit

Permalink
Finish debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Sep 12, 2024
1 parent 2b1156f commit 0eb58be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 53 deletions.
49 changes: 19 additions & 30 deletions packages/yew-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::borrow::Cow;
use std::rc::Rc;

use gloo::console::console_dbg;
use yew::prelude::*;
use yew::virtual_dom::AttrValue;

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

{
let history = history.clone();
let navigator = navigator.clone();
let basename = basename.clone();
let old_basename = use_state_eq(|| Option::<String>::None);
console_dbg!("render");
// Can't use `use_Effect_with` since need to track old and new, not just new.
use_effect(move || {
console_dbg!(format!("{basename:?} {:?}", *old_basename));
if basename != *old_basename {
// If `old_basename` is `Some`, path is probably prefixed with `old_basename`.
// If `old_basename` is `None`, path may or may not be prefixed with the new `basename`,
// depending on whether this is the first render.
let old_navigator: Navigator = Navigator::new(
history.clone(),
old_basename.as_ref().or(basename.as_ref()).cloned(),
);
old_basename.set(basename.clone());
let location = history.location();
let stripped = old_navigator.strip_basename(Cow::from(location.path()));
let prefixed = navigator.prefix_basename(&stripped);
console_dbg!(format!("s {stripped} {prefixed}"));
if false && prefixed != location.path() {
history
.replace_with_query(prefixed, Raw(location.query_str()))
.unwrap_or_else(|never| match never {})
}
}
});
let old_basename = use_state_eq(|| Option::<String>::None);
if basename != *old_basename {
// If `old_basename` is `Some`, path is probably prefixed with `old_basename`.
// If `old_basename` is `None`, path may or may not be prefixed with the new `basename`,
// depending on whether this is the first render.
let old_navigator = Navigator::new(
history.clone(),
old_basename.as_ref().or(basename.as_ref()).cloned(),
);
old_basename.set(basename.clone());
let location = history.location();
let stripped = old_navigator.strip_basename(Cow::from(location.path()));
let prefixed = navigator.prefix_basename(&stripped);

if prefixed != location.path() {
history
.replace_with_query(prefixed, Raw(location.query_str()))
.unwrap_or_else(|never| match never {});
}
}

let navi_ctx = NavigatorContext { navigator };
Expand Down
36 changes: 13 additions & 23 deletions packages/yew-router/tests/link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::time::Duration;

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

sleep(Duration::ZERO).await;

Expand All @@ -113,6 +112,8 @@ async fn link_in_browser_router() {
"/search?q=Rust&lang=en_US",
link_href("#browser-router ul > li.search-q-lang > a")
);

handle.destroy();
}

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

#[function_component(RootForBasename)]
fn root_for_basename(props: &BasenameProps) -> Html {
console_dbg!("outer render");
html! {
<BrowserRouter basename={props.basename.clone()}>
<NavigationMenu />
Expand All @@ -142,16 +142,12 @@ async fn link_with_basename() {
)
.render();

for _ in 0..10 {
sleep(Duration::from_millis(100)).await;
}
sleep(Duration::ZERO).await;

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

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

for _ in 0..10 {
sleep(Duration::from_millis(100)).await;
}

console_dbg!("before assert");
sleep(Duration::ZERO).await;

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

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

for _ in 0..10 {
sleep(Duration::from_millis(100)).await;
}
sleep(Duration::ZERO).await;

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

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

// None -> Some
handle.update(BasenameProps {
basename: Some("bass".to_string()),
basename: Some("/bass/".to_string()),
});

for _ in 0..10 {
sleep(Duration::from_millis(100)).await;
}
sleep(Duration::ZERO).await;

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

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

handle.destroy();
}

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

sleep(Duration::ZERO).await;

Expand All @@ -255,6 +243,8 @@ async fn link_in_hash_router() {
"#/search?q=Rust&lang=en_US",
link_href("#hash-router ul > li.search-q-lang > a")
);

handle.destroy();
}

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

0 comments on commit 0eb58be

Please sign in to comment.