pub struct Selector<T>{ /* private fields */ }Expand description
A conditional signal that only notifies subscribers when a change in the source signal’s value changes whether the given function is true.
You probably don’t need this, but it can be a very useful optimization
in certain situations (e.g., “set the class selected if selected() == this_row_index)
because it reduces them from O(n) to O(1).
let a = RwSignal::new(0);
let is_selected = Selector::new(move || a.get());
let total_notifications = StoredValue::new(0);
Effect::new_isomorphic({
let is_selected = is_selected.clone();
move |_| {
if is_selected.selected(&5) {
total_notifications.update_value(|n| *n += 1);
}
}
});
assert_eq!(is_selected.selected(&5), false);
assert_eq!(total_notifications.get_value(), 0);
a.set(5);
assert_eq!(is_selected.selected(&5), true);
assert_eq!(total_notifications.get_value(), 1);
a.set(5);
assert_eq!(is_selected.selected(&5), true);
assert_eq!(total_notifications.get_value(), 1);
a.set(4);
assert_eq!(is_selected.selected(&5), false);Implementations§
Source§impl<T> Selector<T>
impl<T> Selector<T>
Sourcepub fn new(
source: impl Fn() -> T + Send + Sync + Clone + 'static,
) -> Selector<T>
pub fn new( source: impl Fn() -> T + Send + Sync + Clone + 'static, ) -> Selector<T>
Creates a new selector that compares values using PartialEq.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Selector<T>
impl<T> !RefUnwindSafe for Selector<T>
impl<T> Send for Selector<T>
impl<T> Sync for Selector<T>
impl<T> Unpin for Selector<T>
impl<T> !UnwindSafe for Selector<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> SerializableKey for T
impl<T> SerializableKey for T
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Borrows the value.
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Takes the value.