Repository: leptos-rs/leptos
File: router/src/location/history.rs (Rust)

Code before:
                 clicks",
            );

        // handle popstate event (forward/back navigation)
        let cb = {
            let url = self.url.clone();
            let path_stack = self.path_stack.clone();
            let is_back = self.is_back.clone();
            move || match Self::current() {
                Ok(new_url) => {
                    let mut stack = path_stack.write_value();
                    let is_navigating_back = stack.len() == 1
                        || (stack.len() >= 2
                            && stack.get(stack.len() - 2) == Some(&new_url));

                    if is_navigating_back {
                        stack.pop();
                    }

                    is_back.set(is_navigating_back);

                    url.set(new_url);
                }
                Err(e) => {
                    #[cfg(feature = "tracing")]
                    tracing::error!("{e:?}");
                    #[cfg(not(feature = "tracing"))]
                    web_sys::console::error_1(&e);
                }
            }
        };
        let closure =
            Closure::wrap(Box::new(cb) as Box<dyn Fn()>).into_js_value();
        window
            .add_event_listener_with_callback(
                "popstate",
                closure.as_ref().unchecked_ref(),
            )
            .expect("couldn't add `popstate` listener to `window`");
    }

    fn ready_to_complete(&self) {
        if let Some(tx) = self.pending_navigation.lock().or_poisoned().take() {
            _ = tx.send(());
        }
    }

    fn complete_navigation(&self, loc: &LocationChange) {
        let history = window().history().unwrap();

        let current_path = self

Review comment (refactor):
I was originally going to just do window.remove_event_listener() to follow the above convention but the window object and send wrapper stuff was being annoying and this looked cleaner/reuses helpers.

Code after:
                    let is_navigating_back = stack.len() == 1
                        || (stack.len() >= 2
                            && stack.get(stack.len() - 2) == Some(&new_url));

                    if is_navigating_back {
                        stack.pop();
                    }

                    is_back.set(is_navigating_back);

                    url.set(new_url);
                }
                Err(e) => {
                    #[cfg(feature = "tracing")]
                    tracing::error!("{e:?}");
                    #[cfg(not(feature = "tracing"))]
                    web_sys::console::error_1(&e);
                }
            }
        };

        let popstate_handle =
            window_event_listener(ev::popstate, move |_| popstate_cb());

        on_cleanup(|| {
            click_handle.remove();
            popstate_handle.remove();
        });
    }

    fn ready_to_complete(&self) {
        if let Some(tx) = self.pending_navigation.lock().or_poisoned().take() {
            _ = tx.send(());
        }
    }

    fn complete_navigation(&self, loc: &LocationChange) {
        let history = window().history().unwrap();

        let current_path = self
            .path_stack
            .read_value()
            .last()
            .map(|url| url.to_full_path());
        let add_to_stack = current_path.as_ref() != Some(&loc.value);

        if loc.replace {
            history
                .replace_state_with_url(
                    &loc.state.to_js_value(),
                    "",