leptos-rs / leptos

Build fast web applications with Rust.
https://leptos.dev
MIT License
15.96k stars 627 forks source link

`try_with` on disposed resource panics instead of returning `None` #2620

Closed otopetrik closed 3 months ago

otopetrik commented 3 months ago

Describe the bug According to documentation, calling try_with should return None if signal is no longer valid. Instead it calls panic!.

It calls runtime.resource: https://github.com/leptos-rs/leptos/blob/2ef27cb0bb485b7ad33e0e3d5f144718504ad757/leptos_reactive/src/resource.rs#L801 which panics: https://github.com/leptos-rs/leptos/blob/2ef27cb0bb485b7ad33e0e3d5f144718504ad757/leptos_reactive/src/runtime.rs#L1279-L1284

Leptos Dependencies

leptos = { version = "0.6.12", features = ["csr"] }
leptos_router = { version = "0.6.12"}

Expected behavior try_with to return None for disposed resources.

Additional context Attempted to implement resource caching as described here.

Navigating away from the page causes resources in the cache to be disposed, after returning to the page, the cached (and now disposed) resources are unusable, with no way to test if cached resource needs to be replaced by new one.

The only user of runtime.try_resource() without unwrap is try_update, which cannot be used for testing whether resource is disposed, calling try_update breaks loading for non-disposed resources.