make caching system more robust
This commit is contained in:
parent
cd930966b9
commit
8d4a315a96
@ -54,7 +54,7 @@ static_assertions = "1.1"
|
||||
uuid = { version = "1", features = ["v4", "fast-rng", "js"] }
|
||||
bincode = "1.3"
|
||||
serde = "1"
|
||||
|
||||
base64 = "0.20.0-alpha.1"
|
||||
|
||||
[dev-dependencies]
|
||||
benchmarks = { path = "./benchmarks" }
|
||||
@ -83,7 +83,6 @@ wee_alloc = "0.4"
|
||||
wasm-bindgen = { version = "0.2", default-features = false, features = ["std"] }
|
||||
web-sys = "0.3"
|
||||
tracing-wasm = "0.2"
|
||||
base64 = "0.20.0-alpha.1"
|
||||
|
||||
[package.metadata.cargo-all-features]
|
||||
skip_optional_dependencies = true #don't test optional dependencies, only features
|
||||
|
||||
2
build.sh
2
build.sh
@ -13,7 +13,7 @@ if test "$1" == "" || test "$1" == "release"; then
|
||||
llvm-strip -s target/wasm32-unknown-unknown/release/ytbn_graphing_software.wasm
|
||||
export TYPE="release"
|
||||
elif test "$1" == "debug"; then
|
||||
cargo build --dev --target wasm32-unknown-unknown -Z build-std=std,panic_unwind -Z build-std-features=panic-abort --lib
|
||||
cargo build --target wasm32-unknown-unknown -Z build-std=std,panic_unwind,panic_abort -Z build-std-features=panic-unwind --lib
|
||||
export TYPE="debug"
|
||||
else
|
||||
echo "ERROR: build.sh, argument invalid"
|
||||
|
||||
@ -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(),
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
use ytbn_graphing_software::{storage_create, storage_read};
|
||||
|
||||
/// Tests [`SteppedVector`] to ensure everything works properly (helped me find a bunch of issues)
|
||||
#[test]
|
||||
fn stepped_vector() {
|
||||
@ -93,3 +95,17 @@ fn option_vec_printer() {
|
||||
assert_eq!(option_vec_printer(&key), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage() {
|
||||
let commit = "abcdefeg".chars().map(|c| c as u8).collect::<Vec<u8>>();
|
||||
let data = "really cool data"
|
||||
.chars()
|
||||
.map(|c| c as u8)
|
||||
.collect::<Vec<u8>>();
|
||||
let storage = storage_create(commit.as_slice(), data.as_slice());
|
||||
|
||||
let read = storage_read(storage);
|
||||
assert_eq!(read.0.chars().map(|c| c as u8).collect::<Vec<u8>>(), commit);
|
||||
assert_eq!(read.1, data);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user