leptos_windowing/loaders/
loader.rs

1use std::{fmt::Debug, ops::Range};
2
3/// Loader trait for loading items on-demand from a data source.
4///
5/// This is the most generic loader trait. Please have a look first at the other loader traits as they
6/// usually are simpler to implement. Only if they are not sufficient, you should implement this trait.
7pub trait Loader {
8    /// If this Some(...) then the data will be loaded in chunks of this size.
9    /// This is useful for paginated data sources.
10    /// Please look at [`PaginatedLoader`] if you have such a paginated data source.
11    const CHUNK_SIZE: Option<usize> = None;
12
13    /// The type of items that will be loaded.
14    type Item;
15
16    /// The type of the query data that will be used to load items.
17    ///
18    /// Can be used to filter or sort the items for example.
19    type Query;
20
21    /// The type of errors that can occur during loading.
22    type Error: Debug + 'static;
23
24    /// Does the actual loading of items.
25    ///
26    /// This will be called with a range respecting the chunk size.
27    /// The query data can be used to filter or sort the items for example.
28    ///
29    /// It returns [`LoadedItems`] containing the loaded items as well with the loaded range
30    /// which can be different from the requested range if the data source can't provide the
31    /// exact requested range.
32    fn load_items(
33        &self,
34        range: Range<usize>,
35        query: &Self::Query,
36    ) -> impl Future<Output = Result<LoadedItems<Self::Item>, Self::Error>>;
37
38    /// The total number of items of this data source with respect to the query.
39    ///
40    /// Returns `Ok(None)` if unknown (which is the default).
41    fn item_count(
42        &self,
43        _query: &Self::Query,
44    ) -> impl Future<Output = Result<Option<usize>, Self::Error>> {
45        async { Ok(None) }
46    }
47}
48
49/// Return type of [`Loader::load_items`].
50pub struct LoadedItems<T> {
51    /// The loaded items.
52    pub items: Vec<T>,
53
54    /// The actual range of items that were loaded.
55    ///
56    /// This may be different from the requested range, for example if the data source is paginated.
57    pub range: Range<usize>,
58}