pub struct SelectorScope<Lens> { /* private fields */ }Expand description
SelectorScope is the primitive that backs the store system.
Under the hood stores consist of two different parts:
- The underlying lock that contains the data in the store.
- A tree of subscriptions used to make the store reactive.
The SelectorScope contains a view into the lock (Lens) and a path into the subscription tree. When
the selector is read to, it will track the current path in the subscription tree. When it it written to
it marks itself and all its children as dirty.
When you derive the Store macro on your data structure,
it generates methods that map the lock to a new type and scope the path to a specific part of the subscription structure.
For example, a Counter store might look like this:
#[derive(Store)]
struct Counter {
count: i32,
}
impl CounterStoreExt for Store<Counter> {
fn count(
self,
) -> dioxus_stores::Store<
i32,
dioxus_stores::macro_helpers::dioxus_signals::MappedMutSignal<i32, __W>,
> {
let __map_field: fn(&CounterTree) -> &i32 = |value| &value.count;
let __map_mut_field: fn(&mut CounterTree) -> &mut i32 = |value| &mut value.count;
let scope = self.selector().scope(0u32, __map_field, __map_mut_field);
dioxus_stores::Store::new(scope)
}
}The count method maps the lock to the i32 type and creates a child 0 path in the subscription tree. Only writes
to that 0 path or its parents will trigger a re-render of the components that read the count field.
Implementations§
Source§impl<Lens> SelectorScope<Lens>
impl<Lens> SelectorScope<Lens>
Sourcepub fn hash_child<U: ?Sized, T, F, FMut>(
self,
index: &impl Hash,
map: F,
map_mut: FMut,
) -> SelectorScope<MappedMutSignal<U, Lens, F, FMut>>
pub fn hash_child<U: ?Sized, T, F, FMut>( self, index: &impl Hash, map: F, map_mut: FMut, ) -> SelectorScope<MappedMutSignal<U, Lens, F, FMut>>
Create a child selector scope for a hash key. The scope will only be marked as dirty when a write occurs to that key or its parents.
Note the hash is lossy, so there may rarely be collisions. If a collision does occur, it may cause reruns in a part of the app that has not changed. As long as derived data is pure, this should not cause issues.
Sourcepub fn child<U: ?Sized, T, F, FMut>(
self,
index: u16,
map: F,
map_mut: FMut,
) -> SelectorScope<MappedMutSignal<U, Lens, F, FMut>>
pub fn child<U: ?Sized, T, F, FMut>( self, index: u16, map: F, map_mut: FMut, ) -> SelectorScope<MappedMutSignal<U, Lens, F, FMut>>
Create a child selector scope for a specific index. The scope will only be marked as dirty when a write occurs to that index or its parents.
Sourcepub fn hash_child_unmapped(self, index: &impl Hash) -> SelectorScope<Lens>
pub fn hash_child_unmapped(self, index: &impl Hash) -> SelectorScope<Lens>
Create a hashed child selector scope for a specific index without mapping the writer. The scope will only be marked as dirty when a write occurs to that index or its parents.
Sourcepub fn child_unmapped(self, index: u16) -> SelectorScope<Lens>
pub fn child_unmapped(self, index: u16) -> SelectorScope<Lens>
Create a child selector scope for a specific index without mapping the writer. The scope will only be marked as dirty when a write occurs to that index or its parents.
Sourcepub fn map<U: ?Sized, T, F, FMut>(
self,
map: F,
map_mut: FMut,
) -> SelectorScope<MappedMutSignal<U, Lens, F, FMut>>
pub fn map<U: ?Sized, T, F, FMut>( self, map: F, map_mut: FMut, ) -> SelectorScope<MappedMutSignal<U, Lens, F, FMut>>
Map the view into the writable data without creating a child selector scope
Sourcepub fn track_shallow(&self)
pub fn track_shallow(&self)
Track this scope shallowly.
Sourcepub fn mark_dirty(&self)
pub fn mark_dirty(&self)
Mark this scope as dirty recursively.
Sourcepub fn mark_dirty_shallow(&self)
pub fn mark_dirty_shallow(&self)
Mark this scope as dirty shallowly.
Sourcepub fn mark_dirty_at_and_after_index(&self, index: usize)
pub fn mark_dirty_at_and_after_index(&self, index: usize)
Mark this scope as dirty at and after the given index.
Sourcepub fn map_writer<W2>(self, map: impl FnOnce(Lens) -> W2) -> SelectorScope<W2>
pub fn map_writer<W2>(self, map: impl FnOnce(Lens) -> W2) -> SelectorScope<W2>
Map the writer to a new type.
Sourcepub fn write_untracked(&self) -> WritableRef<'static, Lens>where
Lens: Writable,
pub fn write_untracked(&self) -> WritableRef<'static, Lens>where
Lens: Writable,
Write without notifying subscribers.