leptos_windowing/
window.rs

1use std::{ops::Range, sync::Arc};
2
3use leptos::prelude::*;
4
5use crate::cache::Cache;
6
7/// This is bascially a signal of a slice of the internal cache.
8///
9/// This is returned by `use_pagination` and `use_virtualization`.
10pub struct ItemWindow<T>
11where
12    T: Send + Sync + 'static,
13{
14    pub cache: Cache<T>,
15    pub range: Signal<Range<usize>>,
16}
17
18impl<T> Clone for ItemWindow<T>
19where
20    T: Send + Sync + 'static,
21{
22    fn clone(&self) -> Self {
23        *self
24    }
25}
26
27impl<T> Copy for ItemWindow<T> where T: Send + Sync + 'static {}
28
29impl<T> ItemWindow<T>
30where
31    T: Send + Sync + 'static,
32{
33    /// Updates an item in the cache at the specified index.
34    ///
35    /// The user is responsible to make sure that the data source is updated accordingly.
36    #[inline]
37    pub fn update_item(&self, index: usize, item: T) {
38        self.cache.update_item(index, item);
39    }
40
41    /// Inserts an item into the cache at the specified index.
42    ///
43    /// The user is responsible to make sure that the data source is updated accordingly.
44    #[inline]
45    pub fn insert_item(&self, index: usize, item: T) {
46        self.cache.insert_item(index, item);
47    }
48
49    /// Removes an item from the cache at the specified index.
50    ///
51    /// The user is responsible to make sure that the data source is updated accordingly.
52    #[inline]
53    pub fn remove_item(&self, index: usize) {
54        self.cache.remove_item(index);
55    }
56}
57
58/// Item in a [`ItemWindow`].
59pub struct WindowItem<T>
60where
61    T: Send + Sync + 'static,
62{
63    pub index: usize,
64    pub data: Arc<T>,
65    cache: Cache<T>,
66}
67
68impl<T> Clone for WindowItem<T>
69where
70    T: Send + Sync + 'static,
71{
72    fn clone(&self) -> Self {
73        Self {
74            index: self.index,
75            data: Arc::clone(&self.data),
76            cache: self.cache,
77        }
78    }
79}
80
81impl<T> WindowItem<T>
82where
83    T: Send + Sync + 'static,
84{
85    /// Creates a new `WindowItem` with the given index, data, and item window.
86    pub fn new(index: usize, data: Arc<T>, window: &ItemWindow<T>) -> Self {
87        Self {
88            index,
89            data,
90            cache: window.cache,
91        }
92    }
93
94    /// Updates the data in the cache associated with the item.
95    ///
96    /// The user is responsible for updating the data source accordingly.
97    #[inline]
98    pub fn update(&self, new: T) {
99        self.cache.update_item(self.index, new);
100    }
101
102    /// Removes the item from the cache.
103    ///
104    /// The user is responsible for updating the data source accordingly.
105    #[inline]
106    pub fn remove(&self) {
107        self.cache.remove_item(self.index);
108    }
109
110    /// Inserts an item before the current item in the cache.
111    ///
112    /// The user is responsible for updating the data source accordingly.
113    #[inline]
114    pub fn insert_before(&self, item: T) {
115        self.cache.insert_item(self.index, item);
116    }
117
118    /// Inserts an item after the current item in the cache.
119    ///
120    /// The user is responsible for updating the data source accordingly.
121    #[inline]
122    pub fn insert_after(&self, item: T) {
123        self.cache.insert_item(self.index + 1, item);
124    }
125
126    /// Inserts an item at the specified index in the cache.
127    ///
128    /// The user is responsible for updating the data source accordingly.
129    #[inline]
130    pub fn insert(&self, index: usize, item: T) {
131        self.cache.insert_item(index, item);
132    }
133}