Skip to content

Commit

Permalink
Renamed debounced to distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamschi committed Nov 22, 2024
1 parent 220ee30 commit 353dcd9
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions flourish/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let _ = Signal::cell_cyclic_reactive_mut(|_weak| ((), move |_value, _status| Pro

// Not evaluated unless subscribed.
let _ = Signal::computed(|| ());
let _ = Signal::debounced(|| ());
let _ = Signal::distinct(|| ());
let _ = Signal::computed_uncached(|| ()); // `Fn` closure. The others take `FnMut`s.
let _ = Signal::computed_uncached_mut(|| ());
let _ = Signal::folded((), |_value| Propagation::Propagate);
Expand Down Expand Up @@ -102,7 +102,7 @@ signals_helper! {
// The closure type is erased!
// Not evaluated unless subscribed.
let _source = computed!(|| ());
let _source = debounced!(|| ());
let _source = distinct!(|| ());
let _source = computed_uncached!(|| ());
let _source = computed_uncached_mut!(|| ());
let _source = folded!((), |_value| Propagation::Propagate);
Expand Down
18 changes: 9 additions & 9 deletions flourish/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
signal_arc::SignalWeakDynCell,
traits::{UnmanagedSignal, UnmanagedSignalCell},
unmanaged::{
computed, computed_uncached, computed_uncached_mut, debounced, folded, reduced, InertCell,
computed, computed_uncached, computed_uncached_mut, distinct, folded, reduced, InertCell,
ReactiveCell, ReactiveCellMut,
},
Guard, SignalArc, SignalArcDyn, SignalArcDynCell, SignalWeak, Subscription,
Expand Down Expand Up @@ -134,22 +134,22 @@ impl<T: ?Sized + Send, SR: ?Sized + SignalsRuntimeRef> Signal<T, Opaque, SR> {
/// type Signal<T, S> = flourish::Signal<T, S, GlobalSignalsRuntime>;
///
/// # let input = Signal::cell(1);
/// Signal::debounced(|| input.get() + 1);
/// Signal::distinct(|| input.get() + 1);
/// # }
/// ```
///
/// Note that iff there is no subscriber,
/// this signal and its dependents will still become stale unconditionally.
///
/// Wraps [`debounced`](`debounced()`).
pub fn debounced<'a>(
/// Wraps [`distinct`](`distinct()`).
pub fn distinct<'a>(
fn_pin: impl 'a + Send + FnMut() -> T,
) -> SignalArc<T, impl 'a + Sized + UnmanagedSignal<T, SR>, SR>
where
T: 'a + Sized + PartialEq,
SR: 'a + Default,
{
Self::debounced_with_runtime(fn_pin, SR::default())
Self::distinct_with_runtime(fn_pin, SR::default())
}

/// A simple cached computation.
Expand All @@ -161,23 +161,23 @@ impl<T: ?Sized + Send, SR: ?Sized + SignalsRuntimeRef> Signal<T, Opaque, SR> {
/// # #![cfg(feature = "global_signals_runtime")] // flourish feature
/// # use flourish::{GlobalSignalsRuntime, Signal};
/// # let input = Signal::cell_with_runtime(1, GlobalSignalsRuntime);
/// Signal::debounced_with_runtime(|| input.get() + 1, input.clone_runtime_ref());
/// Signal::distinct_with_runtime(|| input.get() + 1, input.clone_runtime_ref());
/// # }
/// ```
///
/// Note that iff there is no subscriber,
/// this signal and its dependents will still become stale unconditionally.
///
/// Wraps [`debounced`](`debounced()`).
pub fn debounced_with_runtime<'a>(
/// Wraps [`distinct`](`distinct()`).
pub fn distinct_with_runtime<'a>(
fn_pin: impl 'a + Send + FnMut() -> T,
runtime: SR,
) -> SignalArc<T, impl 'a + Sized + UnmanagedSignal<T, SR>, SR>
where
T: 'a + Sized + PartialEq,
SR: 'a,
{
SignalArc::new(debounced(fn_pin, runtime))
SignalArc::new(distinct(fn_pin, runtime))
}

/// A simple **uncached** computation.
Expand Down
4 changes: 2 additions & 2 deletions flourish/src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<T: ?Sized + Send, S: Sized + UnmanagedSignalCell<T, SR>, SR: ?Sized + Signa
///
/// # Omissions
///
/// The "uncached" and "debounced" versions of [`computed`](`computed()`) are
/// The "uncached" and "distinct" versions of [`computed`](`computed()`) are
/// intentionally not wrapped here, as their behaviour may be unexpected at first glance.
///
/// You can still easily construct them as [`SignalArc`] and subscribe afterwards:
Expand All @@ -221,7 +221,7 @@ impl<T: ?Sized + Send, S: Sized + UnmanagedSignalCell<T, SR>, SR: ?Sized + Signa
///
/// // The closure re-runs on each refresh, even if the inputs are equal!
/// // However, dependent signals are only invalidated if the result changed.
/// let sub_debounced = Signal::debounced(|| ()).into_subscription();
/// let sub_distinct = Signal::distinct(|| ()).into_subscription();
/// # }
/// ```
impl<T: ?Sized + Send, SR: ?Sized + SignalsRuntimeRef> Subscription<T, Opaque, SR> {
Expand Down
22 changes: 11 additions & 11 deletions flourish/src/unmanaged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ macro_rules! computed_with_runtime {
#[doc(hidden)]
pub use crate::computed_with_runtime;

/// Unmanaged version of [`Signal::debounced_with_runtime`](`crate::Signal::debounced_with_runtime`).
pub fn debounced<
/// Unmanaged version of [`Signal::distinct_with_runtime`](`crate::Signal::distinct_with_runtime`).
pub fn distinct<
'a,
T: 'a + Send + PartialEq,
F: 'a + Send + FnMut() -> T,
Expand All @@ -218,22 +218,22 @@ pub fn debounced<
}
#[macro_export]
#[doc(hidden)]
macro_rules! debounced {
macro_rules! distinct {
($fn_pin:expr$(,)?) => {{
::core::compile_error!("Using this macro directly would require `super let`. For now, please wrap the binding(s) in `signals_helper! { … }`.");
}};
}
#[doc(hidden)]
pub use crate::debounced;
pub use crate::distinct;
#[macro_export]
#[doc(hidden)]
macro_rules! debounced_with_runtime {
macro_rules! distinct_with_runtime {
($source:expr, $runtime:expr$(,)?) => {{
::core::compile_error!("Using this macro directly would require `super let`. For now, please wrap the binding(s) in `signals_helper! { … }`.");
}};
}
#[doc(hidden)]
pub use crate::debounced_with_runtime;
pub use crate::distinct_with_runtime;

/// Unmanaged version of [`Signal::computed_uncached_with_runtime`](`crate::Signal::computed_uncached_with_runtime`).
pub fn computed_uncached<
Expand Down Expand Up @@ -432,12 +432,12 @@ macro_rules! signals_helper {
let $name = ::core::pin::pin!($crate::unmanaged::computed($fn_pin, $runtime));
let $name = ::core::pin::Pin::into_ref($name) as ::core::pin::Pin<&dyn $crate::unmanaged::UnmanagedSignal<_, _>>;
};
{let $name:ident = debounced!($fn_pin:expr$(,)?);} => {
let $name = ::core::pin::pin!($crate::unmanaged::debounced($fn_pin, $crate::GlobalSignalsRuntime));
{let $name:ident = distinct!($fn_pin:expr$(,)?);} => {
let $name = ::core::pin::pin!($crate::unmanaged::distinct($fn_pin, $crate::GlobalSignalsRuntime));
let $name = ::core::pin::Pin::into_ref($name) as ::core::pin::Pin<&dyn $crate::unmanaged::UnmanagedSignal<_, _>>;
};
{let $name:ident = debounced_with_runtime!($fn_pin:expr, $runtime:expr$(,)?);} => {
let $name = ::core::pin::pin!($crate::unmanaged::debounced($fn_pin, $runtime));
{let $name:ident = distinct_with_runtime!($fn_pin:expr, $runtime:expr$(,)?);} => {
let $name = ::core::pin::pin!($crate::unmanaged::distinct($fn_pin, $runtime));
let $name = ::core::pin::Pin::into_ref($name) as ::core::pin::Pin<&dyn $crate::unmanaged::UnmanagedSignal<_, _>>;
};
{let $name:ident = computed_uncached!($fn_pin:expr$(,)?);} => {
Expand Down Expand Up @@ -506,7 +506,7 @@ macro_rules! signals_helper {
// The compiler still squiggles the entire macro, unfortunately.
::core::compile_error!(::core::concat!(
"Unrecognised macro name or wrong argument count (for) `", ::core::stringify!($macro), "`. The following macros are supported:\n",
"inert_cell[_with_runtime]!(1/2), reactive_cell[_mut][_with_runtime]!(2/3), cached!(1), debounced[_with_runtime]!(1/2), ",
"inert_cell[_with_runtime]!(1/2), reactive_cell[_mut][_with_runtime]!(2/3), cached!(1), distinct[_with_runtime]!(1/2), ",
"computed[_uncached[_mut]][_with_runtime]!(1/2), folded[_with_runtime]!(2/3), reduced[_with_runtime]!(2/3), ",
"subscription[_with_runtime]!(1/2), subscription_from_source!(1), effect[_with_runtime]!(2/3)"
));
Expand Down
6 changes: 3 additions & 3 deletions flourish/tests/debounce.rs → flourish/tests/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ mod _validator;
use _validator::Validator;

#[test]
fn debounce_test() {
fn distinct() {
let v = &Validator::new();
let x = &Validator::new();

let (signal, cell) = Signal::cell(0).into_dyn_read_only_and_self();
let debounced = Signal::debounced(move || {
let distinct = Signal::distinct(move || {
x.push("d");
signal.get()
});
let _sub = Subscription::computed(move || {
x.push("s");
v.push(debounced.get())
v.push(distinct.get())
});
v.expect([0]);
x.expect(["s", "d"]);
Expand Down
2 changes: 1 addition & 1 deletion flourish/tests/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn heap() {
}

#[test]
fn effect_drop_is_debounced() {
fn effect_drop_is_distinct() {
let constructions = &Validator::new();
let destructions = &Validator::new();

Expand Down
2 changes: 1 addition & 1 deletion isoprenoid/src/runtime/a_signals_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ unsafe impl SignalsRuntimeRef for &ASignalsRuntime {
.is_empty()
{
// It's not necessary to check if the dependency is actually new here,
// as `subscribe_to_with` debounces automatically.
// as `subscribe_to_with` filters that automatically.

// The subscription happens before dependency wiring.
// This is important to avoid infinite recursion!
Expand Down

0 comments on commit 353dcd9

Please sign in to comment.