Synphonyte / leptos-use

Collection of essential Leptos utilities inspired by React-Use / VueUse
https://leptos-use.rs/
Apache License 2.0
328 stars 72 forks source link

`use_raf_fn` trying to access disposed signal? #135

Closed frnsys closed 3 months ago

frnsys commented 3 months ago

Hi, thanks for this library. I can't seem to get use_raf_fn working correctly, however.

panicked at /home/francis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/leptos-use-0.10.10/src/use_raf_fn.rs:92:27:
Attempted to get a signal after it was disposed.
signal created here: /home/francis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/leptos-use-0.10.10/src/use_raf_fn.rs:55:35
warning happened here: /home/francis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/leptos-use-0.10.10/src/use_raf_fn.rs:92:27

Looking at the referenced lines it's because in the loop_fn this signal is being accessed:

            if !is_active.get_untracked() {
                return;
            }

but I suppose it's after the hosting component goes away, and the RAF function is called once more afterwards.

I thought the lines:

    let pause = move || {
        set_active.set(false);

        let handle = raf_handle.get();
        if let Some(handle) = handle {
            let _ = window().cancel_animation_frame(handle);
        }
        raf_handle.set(None);
    };
    on_cleanup(pause.clone());

would stop the handler in time but I guess not? Or perhaps I'm overlooking something?

thank you.

maccesch commented 3 months ago

Thanks for the kind words.

You're definitely on the right track. The piece of code you're referencing is meant to do that but obiously it doesn't. Let me fix it...

frnsys commented 3 months ago

amazing, thank you!