From 157b72c1d3f0460a7620a781c19337f0a3ad0cf0 Mon Sep 17 00:00:00 2001 From: Mario Carbajal Date: Mon, 16 Dec 2024 11:45:45 -0300 Subject: [PATCH 1/2] impl Dispose for Callback types and add try_run to the Callable trait --- leptos/src/callback.rs | 53 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/leptos/src/callback.rs b/leptos/src/callback.rs index f4e43bc943..da784dad53 100644 --- a/leptos/src/callback.rs +++ b/leptos/src/callback.rs @@ -43,13 +43,20 @@ use reactive_graph::{ owner::{LocalStorage, StoredValue}, - traits::WithValue, + traits::{Dispose, WithValue}, }; use std::{fmt, rc::Rc, sync::Arc}; /// A wrapper trait for calling callbacks. pub trait Callable { /// calls the callback with the specified argument. + /// + /// Returns None if the callback has been disposed + fn try_run(&self, input: In) -> Option; + /// calls the callback with the specified argument. + /// + /// # Panics + /// Panics if you try to run a callback that has been disposed fn run(&self, input: In) -> Out; } @@ -72,6 +79,12 @@ impl Clone for UnsyncCallback { } } +impl Dispose for UnsyncCallback { + fn dispose(self) { + self.0.dispose(); + } +} + impl UnsyncCallback { /// Creates a new callback from the given function. pub fn new(f: F) -> UnsyncCallback @@ -83,6 +96,10 @@ impl UnsyncCallback { } impl Callable for UnsyncCallback { + fn try_run(&self, input: In) -> Option { + self.0.try_with_value(|fun| fun(input)) + } + fn run(&self, input: In) -> Out { self.0.with_value(|fun| fun(input)) } @@ -158,10 +175,12 @@ impl fmt::Debug for Callback { } impl Callable for Callback { + fn try_run(&self, input: In) -> Option { + self.0.try_with_value(|fun| fun(input)) + } + fn run(&self, input: In) -> Out { - self.0 - .try_with_value(|f| f(input)) - .expect("called a callback that has been disposed") + self.0.with_value(|f| f(input)) } } @@ -171,6 +190,12 @@ impl Clone for Callback { } } +impl Dispose for Callback { + fn dispose(self) { + self.0.dispose(); + } +} + impl Copy for Callback {} macro_rules! impl_callable_from_fn { @@ -216,8 +241,12 @@ impl Callback { #[cfg(test)] mod tests { + use reactive_graph::traits::Dispose; + use crate::callback::{Callback, UnsyncCallback}; + use super::Callable; + struct NoClone {} #[test] @@ -246,4 +275,20 @@ mod tests { let _callback: UnsyncCallback<(i32, String), String> = (|num, s| format!("{num} {s}")).into(); } + + #[test] + fn sync_callback_try_run() { + let callback = Callback::new(move |arg| arg); + assert_eq!(callback.try_run((0,)), Some((0,))); + callback.dispose(); + assert_eq!(callback.try_run((0,)), None); + } + + #[test] + fn unsync_callback_try_run() { + let callback = UnsyncCallback::new(move |arg| arg); + assert_eq!(callback.try_run((0,)), Some((0,))); + callback.dispose(); + assert_eq!(callback.try_run((0,)), None); + } } From 771b5fe89eebc86080e2d3285bfa34fa8b79f9fc Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 00:31:44 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- leptos/src/callback.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/leptos/src/callback.rs b/leptos/src/callback.rs index da784dad53..c6aaa62e84 100644 --- a/leptos/src/callback.rs +++ b/leptos/src/callback.rs @@ -241,11 +241,9 @@ impl Callback { #[cfg(test)] mod tests { - use reactive_graph::traits::Dispose; - - use crate::callback::{Callback, UnsyncCallback}; - use super::Callable; + use crate::callback::{Callback, UnsyncCallback}; + use reactive_graph::traits::Dispose; struct NoClone {}