1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::iter::FusedIterator;
use crate::{impls::index::IndexWrite, store::Store};
use dioxus_signals::{Readable, ReadableExt};
impl<Lens, I> Store<Vec<I>, Lens>
where
Lens: Readable<Target = Vec<I>> + 'static,
I: 'static,
{
/// Returns the length of the slice. This will only track the shallow state of the slice.
/// It will only cause a re-run if the length of the slice could change.
///
/// # Example
/// ```rust, no_run
/// use dioxus_stores::*;
/// let store = use_store(|| vec![1, 2, 3]);
/// assert_eq!(store.len(), 3);
/// ```
pub fn len(&self) -> usize {
self.selector().track_shallow();
self.selector().peek().len()
}
/// Checks if the slice is empty. This will only track the shallow state of the slice.
/// It will only cause a re-run if the length of the slice could change.
///
/// # Example
/// ```rust, no_run
/// use dioxus_stores::*;
/// let store = use_store(|| vec![1, 2, 3]);
/// assert!(!store.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.selector().track_shallow();
self.selector().peek().is_empty()
}
/// Returns an iterator over the items in the slice. This will only track the shallow state of the slice.
/// It will only cause a re-run if the length of the slice could change.
/// # Example
/// ```rust, no_run
/// use dioxus_stores::*;
/// let store = use_store(|| vec![1, 2, 3]);
/// for item in store.iter() {
/// println!("{}", item);
/// }
/// ```
pub fn iter(
&self,
) -> impl ExactSizeIterator<Item = Store<I, IndexWrite<usize, Lens>>>
+ DoubleEndedIterator
+ FusedIterator
+ '_
where
Lens: Clone,
{
(0..self.len()).map(move |i| self.clone().index(i))
}
/// Try to get an item from slice. This will only track the shallow state of the slice.
/// It will only cause a re-run if the length of the slice could change. The new store
/// will only update when the item at the index changes.
///
/// # Example
/// ```rust, no_run
/// use dioxus_stores::*;
/// let store = use_store(|| vec![1, 2, 3]);
/// let indexed_store = store.get(1).unwrap();
/// // The indexed store can access the store methods of the indexed store.
/// assert_eq!(indexed_store(), 2);
/// ```
pub fn get(&self, index: usize) -> Option<Store<I, IndexWrite<usize, Lens>>>
where
Lens: Clone,
{
if index >= self.len() {
None
} else {
Some(self.clone().index(index))
}
}
}