mirror of
https://github.com/Zenithsiz/dynatos.git
synced 2026-02-08 04:55:59 +00:00
Added dynatos_router::Location::{with, update}.
This commit is contained in:
parent
0b83a492ef
commit
184f026fd3
@ -28,7 +28,15 @@ impl Location {
|
||||
|
||||
/// Gets the location as a url
|
||||
pub fn get(&self) -> Url {
|
||||
self.0.with(|inner| inner.location.clone())
|
||||
self.with(|location| location.clone())
|
||||
}
|
||||
|
||||
/// Uses the location as a url
|
||||
pub fn with<F, O>(&self, f: F) -> O
|
||||
where
|
||||
F: FnOnce(&Url) -> O,
|
||||
{
|
||||
self.0.with(|inner| f(&inner.location))
|
||||
}
|
||||
|
||||
/// Sets the location.
|
||||
@ -56,6 +64,26 @@ impl Location {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Updates the location
|
||||
pub fn update<F, O>(&self, f: F) -> Result<O, anyhow::Error>
|
||||
where
|
||||
F: FnOnce(&mut Url) -> O,
|
||||
{
|
||||
self.0.update(|inner| {
|
||||
let output = f(&mut inner.location);
|
||||
|
||||
let window = web_sys::window().context("Unable to get window")?;
|
||||
let history = window.history().context("Unable to get history")?;
|
||||
|
||||
// Push the new location into history
|
||||
history
|
||||
.push_state_with_url(&JsValue::UNDEFINED, "", Some(inner.location.as_ref()))
|
||||
.expect("Unable to push history");
|
||||
|
||||
Ok(output)
|
||||
})
|
||||
}
|
||||
|
||||
/// Parses the location as url
|
||||
fn parse_location_url(document: web_sys::Document) -> Result<Url, anyhow::Error> {
|
||||
let location = document.location().context("Document had no location")?;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user