leptos-rs / leptos

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

Signal::into_inner #3068

Open stefnotch opened 1 month ago

stefnotch commented 1 month ago

Is your feature request related to a problem? Please describe.

I am using Leptos to manage large objects, including WebGPU objects that cannot be cloned. I would wrap the object in a signal, do some reactive operations with it, and would subsequently like to get the object back.

A very similar case came up while I was trying to implement a pop method for struct SignalVec<T> { ArcRwSignal<Vec<ArcRwSignal<T>>> } . See https://github.com/leptos-rs/leptos/discussions/3066

Describe the solution you'd like I'd love it if there were a function analogous to the standard library RwLock/Arc::into_inner.

// for the ArcRwSignal

/// Returns the inner value if this is the only reference to to the signal.
/// Otherwise, returns `None`.
/// # Panics
/// Panics if the inner lock is poisoned.
#[track_caller]
pub fn into_inner(self) -> Option<T> {
    // I believe the subscribers don't matter here, because they aren't holding a reference to the value.
    Arc::into_inner(self.value)?.into_inner().ok()
}

Describe alternatives you've considered Using clunky workarounds, such as wrapping my objects into Options, and taking the values out of the options.

stefnotch commented 1 month ago

I would be willing to contribute a pull request with such a feature and unit tests.

gbj commented 1 month ago

PR very welcome! Sounds like a great addition.

(The same can be done for arena-stored types as well, if they are first disposed.)