Merge branch 'master' into jk/release-new-versions

This commit is contained in:
Jonathan Kelley 2023-08-01 16:35:19 -07:00
commit ccbbd701d9
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE

View file

@ -59,8 +59,6 @@ pub struct WebHistory<R: Routable> {
do_scroll_restoration: bool, do_scroll_restoration: bool,
history: History, history: History,
listener_navigation: Option<EventListener>, listener_navigation: Option<EventListener>,
#[allow(dead_code)]
listener_scroll: Option<EventListener>,
listener_animation_frame: Arc<Mutex<Option<AnimationFrame>>>, listener_animation_frame: Arc<Mutex<Option<AnimationFrame>>>,
prefix: Option<String>, prefix: Option<String>,
window: Window, window: Window,
@ -98,27 +96,7 @@ impl<R: Routable> WebHistory<R> {
where where
<R as std::str::FromStr>::Err: std::fmt::Display, <R as std::str::FromStr>::Err: std::fmt::Display,
{ {
let w = window().expect("access to `window`"); let myself = Self::new_inner(prefix, do_scroll_restoration);
let h = w.history().expect("`window` has access to `history`");
let document = w.document().expect("`window` has access to `document`");
let myself = Self::new_inner(
prefix,
do_scroll_restoration,
EventListener::new(&document, "scroll", {
let mut last_updated = 0.0;
move |evt| {
// the time stamp in milliseconds
let time_stamp = evt.time_stamp();
// throttle the scroll event to 100ms
if (time_stamp - last_updated) < 100.0 {
return;
}
update_scroll::<R>(&w, &h);
last_updated = time_stamp;
}
}),
);
let current_route = myself.current_route(); let current_route = myself.current_route();
let current_url = current_route.to_string(); let current_url = current_route.to_string();
@ -169,32 +147,23 @@ impl<R: Routable> WebHistory<R> {
myself myself
} }
fn new_inner( fn new_inner(prefix: Option<String>, do_scroll_restoration: bool) -> Self
prefix: Option<String>,
do_scroll_restoration: bool,
event_listener: EventListener,
) -> Self
where where
<R as std::str::FromStr>::Err: std::fmt::Display, <R as std::str::FromStr>::Err: std::fmt::Display,
{ {
let window = window().expect("access to `window`"); let window = window().expect("access to `window`");
let history = window.history().expect("`window` has access to `history`"); let history = window.history().expect("`window` has access to `history`");
let listener_scroll = match do_scroll_restoration { if do_scroll_restoration {
true => {
history history
.set_scroll_restoration(ScrollRestoration::Manual) .set_scroll_restoration(ScrollRestoration::Manual)
.expect("`history` can set scroll restoration"); .expect("`history` can set scroll restoration");
Some(event_listener)
} }
false => None,
};
Self { Self {
do_scroll_restoration, do_scroll_restoration,
history, history,
listener_navigation: None, listener_navigation: None,
listener_scroll,
listener_animation_frame: Default::default(), listener_animation_frame: Default::default(),
prefix, prefix,
window, window,
@ -302,6 +271,13 @@ where
// don't push the same state twice // don't push the same state twice
return; return;
} }
let w = window().expect("access to `window`");
let h = w.history().expect("`window` has access to `history`");
// update the scroll position before pushing the new state
update_scroll::<R>(&w, &h);
let path = self.full_path(&state); let path = self.full_path(&state);
let state = self.create_state(state); let state = self.create_state(state);
@ -372,6 +348,13 @@ where
// don't push the same state twice // don't push the same state twice
return; return;
} }
let w = window().expect("access to `window`");
let h = w.history().expect("`window` has access to `history`");
// update the scroll position before pushing the new state
update_scroll::<R>(&w, &h);
let path = self.full_path(&state); let path = self.full_path(&state);
let state: [f64; 2] = self.create_state(state); let state: [f64; 2] = self.create_state(state);