Struct leptos::signal_prelude::Selector
source · pub struct Selector<T>where
T: PartialEq + Eq + Clone + Hash + 'static,{ /* 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.
Implementations§
source§impl<T> Selector<T>where
T: PartialEq + Eq + Clone + Hash + 'static,
impl<T> Selector<T>where T: PartialEq + Eq + Clone + Hash + 'static,
sourcepub fn new(source: impl Fn() -> T + Clone + 'static) -> Selector<T>
pub fn new(source: impl Fn() -> T + Clone + 'static) -> Selector<T>
Creates a conditional signal that only notifies subscribers when a change
in the source signal’s value changes whether it is equal to the key value
(as determined by PartialEq.)
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 = Rc::new(RefCell::new(0));
let not = Rc::clone(&total_notifications);
create_isomorphic_effect({
let is_selected = is_selected.clone();
move |_| {
if is_selected.selected(5) {
*not.borrow_mut() += 1;
}
}
});
assert_eq!(is_selected.selected(5), false);
assert_eq!(*total_notifications.borrow(), 0);
a.set(5);
assert_eq!(is_selected.selected(5), true);
assert_eq!(*total_notifications.borrow(), 1);
a.set(5);
assert_eq!(is_selected.selected(5), true);
assert_eq!(*total_notifications.borrow(), 1);
a.set(4);
assert_eq!(is_selected.selected(5), false);Trait Implementations§
Auto Trait Implementations§
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