Skip to content

Commit

Permalink
Simplify navigate functions in router
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Nov 17, 2024
1 parent 0e82e2e commit caa668d
Showing 1 changed file with 22 additions and 39 deletions.
61 changes: 22 additions & 39 deletions packages/sycamore-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,11 @@ where
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn navigate(url: &str) {
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"navigate can only be used with a Router"
);

// Update History API.
let history = window().history().unwrap_throw();
history
.push_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
window().scroll_to_with_x_and_y(0.0, 0.0);

let pathname = pathname.get().unwrap_throw();
let path = url.strip_prefix(&base_pathname()).unwrap_or(url);
pathname.set(path.to_string());
});
let history = window().history().unwrap_throw();
history
.push_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
navigate_no_history(url);

Check warning on line 361 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L357-L361

Added lines #L357 - L361 were not covered by tests
}

/// Navigates to the specified `url` without adding a new history entry. Instead, this replaces the
Expand All @@ -382,23 +370,11 @@ pub fn navigate(url: &str) {
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn navigate_replace(url: &str) {
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"navigate_replace can only be used with a Router"
);

// Update History API.
let history = window().history().unwrap_throw();
history
.replace_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
window().scroll_to_with_x_and_y(0.0, 0.0);

let pathname = pathname.get().unwrap_throw();
let path = url.strip_prefix(&base_pathname()).unwrap_or(url);
pathname.set(path.to_string());
});
let history = window().history().unwrap_throw();
history
.replace_state_with_url(&JsValue::UNDEFINED, "", Some(url))
.unwrap_throw();
navigate_no_history(url);

Check warning on line 377 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L373-L377

Added lines #L373 - L377 were not covered by tests
}

/// Navigates to the specified `url` without touching the history API.
Expand All @@ -408,18 +384,25 @@ pub fn navigate_replace(url: &str) {
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
pub fn navigate_no_history(url: &str) {
window().scroll_to_with_x_and_y(0.0, 0.0);
update_pathname(url);
}

Check warning on line 389 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L387-L389

Added lines #L387 - L389 were not covered by tests

/// Internal function for getting the global pathname variable and updating it with the given `url`.
///
/// # Panics
/// This function will `panic!()` if a [`Router`] has not yet been created.
fn update_pathname(url: &str) {

Check warning on line 395 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L395

Added line #L395 was not covered by tests
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"navigate_no_history can only be used with a Router"
"cannot navigate outside of a Router",

Check warning on line 399 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L399

Added line #L399 was not covered by tests
);

window().scroll_to_with_x_and_y(0.0, 0.0);

let pathname = pathname.get().unwrap_throw();
let path = url.strip_prefix(&base_pathname()).unwrap_or(url);
pathname.set(path.to_string());
});
})

Check warning on line 405 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L405

Added line #L405 was not covered by tests
}

/// Preform a "soft" refresh of the current page.
Expand All @@ -433,7 +416,7 @@ pub fn refresh() {
PATHNAME.with(|pathname| {
assert!(
pathname.get().is_some(),
"refresh can only be used with a Router"
"cannot refresh outside of a Router"

Check warning on line 419 in packages/sycamore-router/src/router.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-router/src/router.rs#L419

Added line #L419 was not covered by tests
);

window().scroll_to_with_x_and_y(0.0, 0.0);
Expand Down

0 comments on commit caa668d

Please sign in to comment.