Skip to content

Commit

Permalink
feat: allow disposing of Signal & StoredValue (leptos-rs#1849)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWagener authored and maccesch committed Oct 7, 2023
1 parent 988d2b2 commit 8c28b15
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions leptos_reactive/src/signal_wrappers_read.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
create_isomorphic_effect, on_cleanup, runtime::untrack, store_value, Memo,
ReadSignal, RwSignal, SignalGet, SignalGetUntracked, SignalStream,
SignalWith, SignalWithUntracked, StoredValue,
ReadSignal, RwSignal, SignalDispose, SignalGet, SignalGetUntracked,
SignalStream, SignalWith, SignalWithUntracked, StoredValue,
};

/// Helper trait for converting `Fn() -> T` closures into
Expand Down Expand Up @@ -320,6 +320,16 @@ impl<T: Clone> SignalGet for Signal<T> {
}
}

impl<T> SignalDispose for Signal<T> {
fn dispose(self) {
match self.inner {
SignalTypes::ReadSignal(s) => s.dispose(),
SignalTypes::Memo(m) => m.dispose(),
SignalTypes::DerivedSignal(s) => s.dispose(),
}
}
}

impl<T: Clone> SignalStream<T> for Signal<T> {
fn to_stream(&self) -> std::pin::Pin<Box<dyn futures::Stream<Item = T>>> {
match self.inner {
Expand Down
7 changes: 7 additions & 0 deletions leptos_reactive/src/stored_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ impl<T> StoredValue<T> {
.flatten()
}

/// Disposes of the stored value
pub fn dispose(self) {
_ = with_runtime(|runtime| {
runtime.stored_values.borrow_mut().remove(self.id);
});
}

/// Sets the stored value.
///
/// # Examples
Expand Down

0 comments on commit 8c28b15

Please sign in to comment.