make caching system more robust
This commit is contained in:
@@ -6,12 +6,10 @@ use epaint::Color32;
|
||||
use shadow_rs::shadow;
|
||||
shadow!(build);
|
||||
|
||||
pub const COMMIT: &str = build::SHORT_COMMIT;
|
||||
|
||||
// Constant string that has a string containing information about the build.
|
||||
pub const BUILD_INFO: &str = formatc!(
|
||||
"Commit: {} ({})\nBuild Date: {}\nPackage Version: {}\nRust Channel: {}\nRust Version: {}",
|
||||
&COMMIT,
|
||||
&build::SHORT_COMMIT,
|
||||
&build::BRANCH,
|
||||
&build::BUILD_TIME,
|
||||
&build::PKG_VERSION,
|
||||
|
||||
@@ -23,7 +23,10 @@ mod widgets;
|
||||
pub use crate::{
|
||||
function_entry::{FunctionEntry, Riemann},
|
||||
math_app::AppSettings,
|
||||
misc::{decimal_round, option_vec_printer, resolution_helper, SteppedVector},
|
||||
misc::{
|
||||
decimal_round, option_vec_printer, resolution_helper, storage_create, storage_read,
|
||||
SteppedVector,
|
||||
},
|
||||
widgets::{AutoComplete, Movement},
|
||||
};
|
||||
|
||||
|
||||
@@ -116,8 +116,6 @@ impl MathApp {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
use wasm_bindgen::JsCast;
|
||||
use web_sys::{HtmlElement, Window};
|
||||
|
||||
if let Some(web_info) = &cc.integration_info.web_info {
|
||||
tracing::info!("Web Info: {:?}", web_info);
|
||||
}
|
||||
@@ -132,17 +130,25 @@ impl MathApp {
|
||||
.dyn_into::<web_sys::HtmlElement>()
|
||||
.unwrap();
|
||||
|
||||
fn update_loading(loading_element: &HtmlElement, add: i32) {
|
||||
fn update_loading(loading_element: &web_sys::HtmlElement, add: i32) {
|
||||
let value =
|
||||
unsafe { loading_element.get_attribute("value").unwrap_unchecked().parse::<i32>().unwrap_unchecked() };
|
||||
loading_element.set_attribute("value", &(add + value).to_string()).unwrap();
|
||||
}
|
||||
|
||||
const COMPRESSED_NAME: &str = const_format::formatc!("YTBN-{}-DECOMPRESSED", COMMIT);
|
||||
const DATA_NAME: &str = "YTBN-DECOMPRESSED";
|
||||
fn get_storage_decompressed() -> Option<Vec<u8>> {
|
||||
if let Ok(Some(data)) = web_sys::window().expect("Could not get web_sys window").local_storage().unwrap().unwrap().get_item(COMPRESSED_NAME) {
|
||||
tracing::info!("Read cached data");
|
||||
Some(base64::decode(data).expect("unable to read data"))
|
||||
if let Ok(Some(data)) = web_sys::window().expect("Could not get web_sys window").local_storage().unwrap().unwrap().get_item("YTBN-DECOMPRESSED") {
|
||||
let (commit, cached_data) = crate::misc::storage_read(data);
|
||||
|
||||
if commit == build::SHORT_COMMIT {
|
||||
tracing::info!("Read cached data");
|
||||
return Some(cached_data.to_vec());
|
||||
} else {
|
||||
// is invalid
|
||||
None
|
||||
}
|
||||
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -151,8 +157,9 @@ impl MathApp {
|
||||
fn set_storage_decompressed(data: &Vec<u8>) {
|
||||
if let Ok(Some(local_storage)) = web_sys::window().expect("Could not get web_sys window").local_storage() {
|
||||
tracing::info!("Setting cached data");
|
||||
|
||||
local_storage.set_item(COMPRESSED_NAME, &base64::encode(data));
|
||||
let saved_data = &crate::misc::storage_create(&build::SHORT_COMMIT.chars().map(|c| c as u8).collect::<Vec<u8>>(), data.as_slice());
|
||||
tracing::info!("Data has length of {}", saved_data.len());
|
||||
local_storage.set_item("YTBN-DECOMPRESSED", saved_data).expect("failed to set local storage cache");
|
||||
} else {
|
||||
panic!("unable to get local storage")
|
||||
}
|
||||
|
||||
21
src/misc.rs
21
src/misc.rs
@@ -283,3 +283,24 @@ pub fn resolution_helper(max_i: usize, min_x: &f64, resolution: &f64) -> Vec<f64
|
||||
pub fn step_helper(max_i: usize, min_x: &f64, step: &f64) -> Vec<f64> {
|
||||
(0..max_i).map(|x| (x as f64 * step) + min_x).collect()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn storage_create(commit: &[u8], data: &[u8]) -> String {
|
||||
assert_eq!(commit.len(), 8);
|
||||
|
||||
let mut new_data = commit.to_vec();
|
||||
// new_data.push(20); // push 'space'
|
||||
let mut data = data.to_vec();
|
||||
new_data.append(&mut data);
|
||||
base64::encode(new_data)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn storage_read(data: String) -> (String, Vec<u8>) {
|
||||
let decoded_1 = base64::decode(data).expect("unable to read data");
|
||||
let (commit, cached_data) = decoded_1.split_at(8);
|
||||
(
|
||||
commit.iter().map(|c| *c as char).collect::<String>(),
|
||||
cached_data.to_vec(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user