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