leptos_struct_table/
row_reader.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use crate::loaded_rows::RowState;
use std::cell::RefCell;
use std::rc::Rc;

/// Allows you to read the cached state of rows from inside the table component which handles
/// loading and caching automatically.
#[derive(Clone)]
pub struct RowReader<Row: Send + Sync + 'static> {
    pub(crate) get_loaded_rows: LoadedRowsGetter<Row>,
}

pub type LoadedRowsGetter<Row> = Rc<RefCell<Box<dyn Fn(usize) -> RowState<Row>>>>;

impl<Row: Send + Sync + 'static> Default for RowReader<Row> {
    fn default() -> Self {
        Self {
            get_loaded_rows: Rc::new(RefCell::new(Box::new(|_| RowState::Placeholder))),
        }
    }
}

impl<Row: Send + Sync + 'static> RowReader<Row> {
    /// Returns the cached state of the row at the given index
    pub fn cached_row(&self, index: usize) -> RowState<Row> {
        (*self.get_loaded_rows.borrow())(index)
    }
}