1pub mod index;
24
25use crate::utils::PageFunction;
26use rumtk_core::cache::{new_cache, LazyRUMCache, LazyRUMCacheValue};
27use rumtk_core::strings::RUMString;
28use rumtk_core::{rumtk_cache_get, rumtk_cache_push};
29
30pub type PageCache = LazyRUMCache<RUMString, PageFunction>;
31pub type PageItem<'a> = (&'a str, PageFunction);
32pub type UserPages<'a> = Vec<PageItem<'a>>;
33pub type PageCacheItem = LazyRUMCacheValue<PageFunction>;
34
35static mut PAGE_CACHE: PageCache = new_cache();
36static DEFAULT_PAGE: PageFunction = index::index;
37
38pub fn register_page(name: &str, component_fxn: PageFunction) {
39 let key = RUMString::from(name);
40 let _ = rumtk_cache_push!(&raw mut PAGE_CACHE, &key, &component_fxn);
41}
42
43pub fn get_page(name: &str) -> PageCacheItem {
44 rumtk_cache_get!(&raw mut PAGE_CACHE, &RUMString::from(name), &DEFAULT_PAGE)
45}
46
47pub fn init_pages(user_components: &UserPages) {
48 for itm in user_components {
50 let (name, value) = itm;
51 register_page(name, *value);
52 }
53}
54
55#[macro_export]
56macro_rules! rumtk_web_register_page {
57 ( $key:expr, $fxn:expr ) => {{
58 use $crate::pages::register_page;
59 register_page($key, $fxn)
60 }};
61}
62
63#[macro_export]
64macro_rules! rumtk_web_get_page {
65 ( $key:expr ) => {{
66 use $crate::pages::get_page;
67 get_page($key)
68 }};
69}
70
71#[macro_export]
72macro_rules! rumtk_web_init_pages {
73 ( $pages:expr ) => {{
74 use $crate::pages::init_pages;
75 init_pages($pages)
76 }};
77}
78
79#[macro_export]
80macro_rules! rumtk_web_collect_page {
81 ( $page:expr, $app_state:expr ) => {{
82 use $crate::rumtk_web_get_page;
83
84 let page = rumtk_web_get_page!(&$page);
85
86 page($app_state.clone())
87 }};
88}