Struct leptos_reactive::Selector
source · 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.
Implementations§
source§impl<T> Selector<T>
impl<T> Selector<T>
sourcepub fn new(source: impl Fn() -> T + Clone + 'static) -> Self
pub fn new(source: impl Fn() -> T + Clone + 'static) -> Self
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> 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