leptos_windowing/loaders/paginated_loader.rs
1use std::fmt::Debug;
2
3/// Loader trait for loading items on-demand from a paginated data source.
4///
5/// Please note that this is independent of if you use pagination or virtualization in your UI.
6/// This just referrs to the data source. So if you have an API for example, that returns the
7/// data paginated, then this is for you.
8pub trait PaginatedLoader {
9 /// How many rows per page
10 const PAGE_ITEM_COUNT: usize;
11
12 /// The type of items that will be loaded.
13 type Item;
14
15 /// The type of the query data that will be used to load items.
16 ///
17 /// Can be used to filter or sort the items for example.
18 type Query;
19
20 /// The type of errors that can occur during loading.
21 type Error: Debug + 'static;
22
23 /// Get all data items specified by the page index (starts a 0) and the query.
24 ///
25 /// If you return less than `PAGE_ITEM_COUNT` rows, it is assumed that the end of the
26 /// data has been reached.
27 fn load_page(
28 &self,
29 page_index: usize,
30 query: &Self::Query,
31 ) -> impl Future<Output = Result<Vec<Self::Item>, Self::Error>>;
32
33 /// The total number of items of this data source with respect to the given query.
34 ///
35 /// Returns `Ok(None)` if unknown (which is the default).
36 fn count(
37 &self,
38 _query: &Self::Query,
39 ) -> impl Future<Output = Result<Option<PaginatedCount>, Self::Error>> {
40 async { Ok(None) }
41 }
42}
43
44/// Return type of [`PaginatedLoader::count`].
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum PaginatedCount {
47 /// If your data source tells you how many pages there are, then use this.
48 Pages(usize),
49
50 /// If your data source tells you how many items there are, then use this.
51 Items(usize),
52}