Skip to content

Commit

Permalink
feat(client): implement memory cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nazo6 committed Dec 11, 2024
1 parent 86d6895 commit 77de2b7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
37 changes: 37 additions & 0 deletions tools/rktk-client/src/app/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::{any::Any, cell::RefCell, collections::HashMap, future::Future, rc::Rc};

use dioxus::hooks::{use_context, use_context_provider};

#[derive(Clone)]
pub struct Cache(Rc<RefCell<HashMap<&'static str, Box<dyn Any>>>>);

pub async fn with_cache<T: Clone + 'static, E, F: Future<Output = Result<T, E>>>(
cache: Cache,
key: &'static str,
fut: F,
) -> Result<T, E> {
if let Some(val) = cache.0.borrow_mut().get(key) {
if let Some(val) = val.downcast_ref::<T>() {
return Ok(val.clone());
}
}

let res = fut.await;
if let Ok(val) = &res {
cache.0.borrow_mut().insert(key, Box::new(val.clone()));
}

res
}

pub fn invalidate_cache(cache: Cache, key: &'static str) {
cache.0.borrow_mut().remove(key);
}

pub fn use_cache_context_provider() -> Cache {
use_context_provider(|| Cache(Rc::new(RefCell::new(HashMap::new()))))
}

pub fn use_cache() -> Cache {
use_context::<Cache>()
}
1 change: 1 addition & 0 deletions tools/rktk-client/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dioxus::prelude::*;

use crate::TAILWIND_CSS;

mod cache;
mod components;
mod disconnect;
mod page;
Expand Down
16 changes: 13 additions & 3 deletions tools/rktk-client/src/app/page/connected/config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
use dioxus::prelude::*;
use rktk_keymanager::state::config::StateConfig;

use crate::app::components::notification::{push_notification, Notification, NotificationLevel};
use crate::app::{
cache::{invalidate_cache, use_cache, with_cache},
components::notification::{push_notification, Notification, NotificationLevel},
};

#[component]
pub fn Config() -> Element {
let mut config_res = use_resource(fetcher::get_config);
let cache = use_cache();
let mut config_res = use_resource({
let cache = cache.clone();
move || with_cache(cache.clone(), "get_config", fetcher::get_config())
});

match &*config_res.value().read() {
Some(Ok(config)) => rsx! {
div { class: "w-full flex justify-center",
ConfigInner {
initial_config: config.to_owned(),
refetch: Callback::new(move |_| config_res.restart()),
refetch: Callback::new(move |_| {
invalidate_cache(cache.clone(), "get_config");
config_res.restart()
}),
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions tools/rktk-client/src/app/page/connected/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use dioxus::prelude::*;

use crate::app::cache::use_cache_context_provider;

mod config;
mod log;
mod remap;
Expand All @@ -13,6 +15,8 @@ enum Tabs {

#[component]
pub fn Connected() -> Element {
use_cache_context_provider();

let mut tab = use_signal(|| Tabs::Remap);

rsx! {
Expand Down
23 changes: 19 additions & 4 deletions tools/rktk-client/src/app/page/connected/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use fetcher::KeymapData;
use rktk_rrp::endpoints::{get_keyboard_info::KeyboardInfo, rktk_keymanager::keycode::KeyAction};

use crate::app::{
cache::{invalidate_cache, use_cache, with_cache},
components::{
notification::{push_notification, Notification, NotificationLevel},
selector::key_action::KeyActionSelector,
Expand All @@ -19,7 +20,18 @@ mod keyboard;

#[component]
pub fn Remap() -> Element {
let mut res = use_resource(|| async { (fetcher::get_keymap().await, js_sys::Date::now()) });
let cache = use_cache();
let mut res = use_resource({
let cache = cache.clone();
move || {
with_cache(cache.clone(), "get_keymap", async {
match fetcher::get_keymap().await {
Ok(data) => Ok((data, js_sys::Date::now())),
Err(e) => Err(e),
}
})
}
});

let keyboard = CONN
.read()
Expand All @@ -29,15 +41,18 @@ pub fn Remap() -> Element {
.clone();

match &*res.value().read() {
Some((Ok(keymap), time)) => {
Some(Ok((keymap, time))) => {
rsx! {
div { class: "h-full",
// Using array as re-rendering using key only works for list
{[rsx! {
RemapInner {
keyboard,
keymap: keymap.to_owned(),
refetch: Callback::new(move |_| res.restart()),
refetch: Callback::new(move |_| {
invalidate_cache(cache.clone(), "get_keymap");
res.restart()
}),
key: "{time}",
}
}].iter()}
Expand All @@ -52,7 +67,7 @@ pub fn Remap() -> Element {
}
}
}
Some((Err(e), _)) => {
Some(Err(e)) => {
rsx! {
div {
h1 { "Error" }
Expand Down

0 comments on commit 77de2b7

Please sign in to comment.