leptos_windowing/loaders/exact_loader.rs
1use std::{fmt::Debug, ops::Range};
2
3/// Trait for loading items on-demand from an data source that let's you request precise ranges.
4///
5/// Implement this if your data source actually returns exactly the range of items requested and
6/// if it can provide the total number of items.
7pub trait ExactLoader {
8 /// The type of items that will be loaded.
9 type Item;
10
11 /// The type of the query data that will be used to load items.
12 ///
13 /// Can be used to filter or sort the items for example.
14 type Query;
15
16 /// The type of errors that can occur during loading.
17 type Error: Debug + 'static;
18
19 /// Does the actual loading of items.
20 ///
21 /// This will be called with a range respecting the chunk size.
22 /// The query data can be used to filter or sort the items for example.
23 ///
24 /// It returns a list of items. If the number of items is less than the requested range,
25 /// it means that the end of the data source has been reached.
26 fn load_items(
27 &self,
28 range: Range<usize>,
29 query: &Self::Query,
30 ) -> impl Future<Output = Result<Vec<Self::Item>, Self::Error>>;
31
32 /// The total number of items of this data source with respect to the query.
33 ///
34 /// Returns `Ok(None)` if unknown (which is the default).
35 fn item_count(
36 &self,
37 _query: &Self::Query,
38 ) -> impl Future<Output = Result<Option<usize>, Self::Error>> {
39 async move { Ok(None) }
40 }
41}