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: Clone> {
9    pub(crate) get_loaded_rows: Rc<RefCell<Box<dyn Fn(usize) -> RowState<Row>>>>,
10}
11
12impl<Row: Clone> Default for RowReader<Row> {
13    fn default() -> Self {
14        Self {
15            get_loaded_rows: Rc::new(RefCell::new(Box::new(|_| RowState::Placeholder))),
16        }
17    }
18}
19
20impl<Row: Clone> RowReader<Row> {
21    /// Returns the cached state of the row at the given index
22    pub fn cached_row(&self, index: usize) -> RowState<Row> {
23        (*self.get_loaded_rows.borrow())(index)
24    }
25}