leptos_struct_table/
row_reader.rs

1use crate::loaded_rows::RowState;
2use std::cell::RefCell;
3use std::rc::Rc;
4
5/// Allows you to read the cached state of rows from inside the table component which handles
6/// loading and caching automatically.
7#[derive(Clone)]
8pub struct RowReader<Row: Send + Sync + 'static> {
9    pub(crate) get_loaded_rows: LoadedRowsGetter<Row>,
10}
11
12pub type LoadedRowsGetter<Row> = Rc<RefCell<Box<dyn Fn(usize) -> RowState<Row>>>>;
13
14impl<Row: Send + Sync + 'static> Default for RowReader<Row> {
15    fn default() -> Self {
16        Self {
17            get_loaded_rows: Rc::new(RefCell::new(Box::new(|_| RowState::Placeholder))),
18        }
19    }
20}
21
22impl<Row: Send + Sync + 'static> RowReader<Row> {
23    /// Returns the cached state of the row at the given index
24    pub fn cached_row(&self, index: usize) -> RowState<Row> {
25        (*self.get_loaded_rows.borrow())(index)
26    }
27}