leptos-windowing 0.3.0

Common functionality for pagination and virtualization with cached loading.
Documentation
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use leptos::prelude::*;
use reactive_stores::{Store, StoreFieldIterator, Subfield};
use std::{
    ops::{Index, Range},
    sync::Arc,
};

use crate::{ItemWindow, LoadedItems, item_state::ItemState};

/// This is a cache for items used internally to track
/// which items are already loaded, which are still loading and which are missing.
pub struct Cache<T>
where
    T: Send + Sync + 'static,
{
    inner: Store<CacheInner<T>>,
    pub(crate) pause_reactive_loading: Callback<()>,
    pub(crate) resume_reactive_loading: Callback<()>,
    pub(crate) is_reactive_loading_active: Signal<bool>,
}

impl<T> Clone for Cache<T>
where
    T: Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Cache<T> where T: Send + Sync + 'static {}

#[derive(Store, Debug)]
pub struct CacheInner<T>
where
    T: Send + Sync + 'static,
{
    items: Vec<ItemState<T>>,
    item_count: Option<usize>,
}

impl<T: Send + Sync + 'static> Default for CacheInner<T> {
    fn default() -> Self {
        Self {
            items: Vec::new(),
            item_count: None,
        }
    }
}

impl<T: Send + Sync + 'static> Cache<T> {
    /// Create a new store of the cache.
    pub(crate) fn new() -> Self {
        Self {
            inner: Store::new(CacheInner::default()),
            pause_reactive_loading: (|| {}).into(),
            resume_reactive_loading: (|| {}).into(),
            is_reactive_loading_active: Signal::stored(true),
        }
    }

    #[inline]
    /// After calling this, changes to the cache will not trigger (re)loading with the loader
    pub fn pause_reactive_loading(&self) {
        self.pause_reactive_loading.run(());
    }

    #[inline]
    /// After calling this, changes to the cache will resume triggering (re)loading with the loader
    pub fn resume_reactive_loading(&self) {
        self.resume_reactive_loading.run(());
    }

    /// Pauses reactive loading before executing `f` and then resumes reactive loading if it hadn't been
    /// paused before.
    pub fn with_reactive_loading_paused<O>(&self, f: impl FnOnce() -> O) -> O {
        let is_active = self.is_reactive_loading_active.get_untracked();
        self.pause_reactive_loading();

        let ret = f();

        if is_active {
            self.resume_reactive_loading();
        }

        ret
    }

    #[inline]
    pub fn track(&self) {
        self.inner.track();
    }

    #[inline]
    /// Length of the items of cache.
    pub fn len(&self) -> usize {
        self.inner.items().read().len()
    }

    #[inline]
    /// True when there are no items in the cache.
    pub fn is_empty(&self) -> bool {
        self.inner.items().read().is_empty()
    }

    #[inline]
    /// Item count subfield
    pub fn item_count(&self) -> Subfield<Store<CacheInner<T>>, CacheInner<T>, Option<usize>> {
        self.inner.item_count()
    }

    #[inline]
    pub fn items(&self) -> Subfield<Store<CacheInner<T>>, CacheInner<T>, Vec<ItemState<T>>> {
        self.inner.items()
    }

    #[inline]
    /// Resize the cache to the specified length.
    pub fn resize(&mut self, len: usize) {
        self.inner
            .items()
            .write()
            .resize(len, ItemState::Placeholder);
    }

    /// Grow the cache size to the specified length.
    pub fn grow(&mut self, len: usize) {
        if self.inner.items().read().len() < len {
            self.inner
                .items()
                .write()
                .resize(len, ItemState::Placeholder);
        }
    }

    /// Marks the specified range of items as loading.
    pub fn write_loading(&self, range: Range<usize>) {
        if range.end > self.inner.items().read().len() {
            self.inner
                .items()
                .write()
                .resize(range.end, ItemState::Placeholder);
        }

        for row in &mut self
            .inner
            .items()
            .iter_unkeyed()
            .skip(range.start)
            .take(range.len())
        {
            if let Some(mut row) = row.try_write() {
                *row = ItemState::Loading;
            }
        }
    }

    /// Called after the loader has finished loading items.
    ///
    /// This will update the respective range of items with the loaded data (or errors).
    pub fn write_loaded(
        &self,
        loading_result: Result<LoadedItems<T>, String>,
        requested_load_range: Range<usize>,
    ) {
        match loading_result {
            Ok(LoadedItems { items, range }) => {
                #[cfg(debug_assertions)]
                let _z = leptos::reactive::diagnostics::SpecialNonReactiveZone::enter();

                if range.end > self.inner.items().read_untracked().len()
                    && let Some(mut writer) = self.inner.items().try_write()
                {
                    writer.resize(range.end, ItemState::Placeholder);
                }

                for (self_row, loaded_row) in self
                    .inner
                    .items()
                    .iter_unkeyed()
                    .skip(range.start)
                    .zip(items)
                {
                    if let Some(mut writer) = self_row.try_write() {
                        *writer = ItemState::Loaded(Arc::new(loaded_row));
                    }
                }
            }
            Err(error) => {
                let range = requested_load_range.start
                    ..requested_load_range
                        .end
                        .min(self.inner.items().read().len());
                if range.start >= range.end {
                    return;
                }

                for row in self.inner.items().iter_unkeyed() {
                    if let Some(mut writer) = row.try_write() {
                        *writer = ItemState::Error(error.clone());
                    }
                }
            }
        }
    }

    #[inline]
    /// Returns the range of items that are missing from the cache inside the given range.
    ///
    /// Used to know what items should be loaded and which ones are already loaded or in the process of being loaded.
    /// Errored items are not considered missing here.
    pub fn missing_range(&self, range_to_load: Range<usize>) -> Option<Range<usize>> {
        let do_load_predicate = |item: &ItemState<T>| matches!(item, &ItemState::Placeholder);

        if range_to_load.end <= range_to_load.start {
            return None;
        }

        if range_to_load.start >= self.inner.items().read().len() {
            return Some(range_to_load);
        }

        let existing_range_end = self.inner.items().read().len().min(range_to_load.end);

        let slice = &self.inner.items().read()[range_to_load.start..existing_range_end];

        let start = slice
            .iter()
            .position(do_load_predicate)
            .unwrap_or(slice.len());
        let start = start + range_to_load.start;

        let mut end = if range_to_load.end >= self.inner.items().read().len() {
            range_to_load.end
        } else {
            slice.iter().rposition(do_load_predicate)? + range_to_load.start + 1
        };

        if let Some(item_count) = self.inner.item_count().get() {
            end = end.min(item_count);
        }

        if end <= start {
            return None;
        }

        Some(
            start
                ..end
                    .max(range_to_load.end)
                    .min(self.inner.item_count().get().unwrap_or(usize::MAX)),
        )
    }

    #[inline]
    /// Sets all items in the cache to the placeholder state.
    pub fn clear(&self) {
        self.inner.items().write().fill(ItemState::Placeholder);
        self.inner.item_count().set(None);
    }

    /// Updates an item in the cache.
    ///
    /// This doesn't trigger a reload.
    ///
    /// The user is responsible for updating the data source accordingly.
    pub fn update_item(&self, index: usize, new: T) {
        self.with_reactive_loading_paused(|| {
            *self.inner.items().at_unkeyed(index).write() = ItemState::Loaded(Arc::new(new));
        });
    }

    /// Removes the item at the given index from the cache and updates the item count.
    ///
    /// This doesn't trigger a reload.
    ///
    /// The user is responsible for updating the data source accordingly.
    pub fn remove_item(&self, index: usize) {
        self.with_reactive_loading_paused(|| {
            self.inner.items().write().remove(index);

            if let Some(len) = self.inner.item_count().get_untracked() {
                self.inner.item_count().set(Some(len - 1));
            }
        });
    }

    /// Inserts an item at the given index in the cache and updates the item count.
    ///
    /// This doesn't trigger a reload.
    ///
    /// The user is responsible for updating the data source accordingly.
    pub fn insert_item(&self, index: usize, new: T) {
        self.with_reactive_loading_paused(|| {
            self.inner
                .items()
                .write()
                .insert(index, ItemState::Loaded(Arc::new(new)));

            if let Some(len) = self.inner.item_count().get_untracked() {
                self.inner.item_count().set(Some(len + 1));
            }
        });
    }
}

impl<T: Sync + Send> Index<Range<usize>> for CacheInner<T> {
    type Output = [ItemState<T>];

    #[inline]
    fn index(&self, index: Range<usize>) -> &Self::Output {
        &self.items[index]
    }
}

impl<T: Send + Sync> Index<usize> for CacheInner<T> {
    type Output = ItemState<T>;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.items[index]
    }
}

/// This can be used to get write access to the cache.
pub struct CacheController<T>
where
    T: Send + Sync + 'static,
{
    cache: StoredValue<Option<Cache<T>>>,
}

impl<T> Clone for CacheController<T>
where
    T: Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for CacheController<T> where T: Send + Sync + 'static {}

impl<T> Default for CacheController<T>
where
    T: Send + Sync + 'static,
{
    fn default() -> Self {
        Self {
            cache: StoredValue::new(None),
        }
    }
}

impl<T> CacheController<T>
where
    T: Send + Sync + 'static,
{
    /// You know what this does.
    pub fn new() -> Self {
        Default::default()
    }

    /// This is called in the components to init the connection to the cache
    pub fn init_with_item_window(&self, window: ItemWindow<T>) {
        self.cache.set_value(Some(window.cache));
    }

    /// Updates an item in the cache.
    ///
    /// This doesn't trigger a reload.
    ///
    /// The user is responsible for updating the data source accordingly.
    pub fn update_item(&self, index: usize, new: T) {
        if let Some(cache) = self.cache.get_value() {
            cache.update_item(index, new);
        } else {
            leptos::logging::error!(
                "Update item is called on a cache controller before the controller has been initialized."
            )
        }
    }

    /// Removes the item at the given index from the cache and updates the item count.
    ///
    /// This doesn't trigger a reload.
    ///
    /// The user is responsible for updating the data source accordingly.
    pub fn remove_item(&self, index: usize) {
        if let Some(cache) = self.cache.get_value() {
            cache.remove_item(index);
        } else {
            leptos::logging::error!(
                "Remove item is called on a cache controller before the controller has been initialized."
            )
        }
    }

    /// Inserts an item at the given index in the cache and updates the item count.
    ///
    /// This doesn't trigger a reload.
    ///
    /// The user is responsible for updating the data source accordingly.
    pub fn insert_item(&self, index: usize, new: T) {
        if let Some(cache) = self.cache.get_value() {
            cache.insert_item(index, new);
        } else {
            leptos::logging::error!(
                "Insert item is called on a cache controller before the controller has been initialized."
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_missing_range() {
        let cache = Cache::<i32>::new();

        assert_eq!(cache.missing_range(0..10), Some(0..10));
        assert_eq!(cache.missing_range(5..10), Some(5..10));

        cache.write_loaded(
            Ok(LoadedItems {
                items: (0..5).collect::<Vec<_>>(),
                range: 0..5,
            }),
            0..5,
        );

        assert_eq!(cache.missing_range(0..10), Some(5..10));
        assert_eq!(cache.missing_range(5..10), Some(5..10));
        assert_eq!(cache.missing_range(5..20), Some(5..20));

        cache.write_loading(5..9);

        assert_eq!(cache.missing_range(0..10), Some(9..10));
        assert_eq!(cache.missing_range(5..10), Some(9..10));
        assert_eq!(cache.missing_range(5..20), Some(9..20));
    }
}