diff --git a/.vscode/settings.json b/.vscode/settings.json index 3f7a96d..4ea1ad7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "files.insertFinalNewline": true, "editor.formatOnSave": true, - "files.trimTrailingWhitespace": true + "files.trimTrailingWhitespace": true, } diff --git a/Cargo.lock b/Cargo.lock index 10ba6f5..ee5f700 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,6 +143,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base64" +version = "0.20.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149ea5dc24cb11513350770afebba32b68e3d2e356f9221351a2a1ee89112a82" + [[package]] name = "benchmarks" version = "0.1.0" @@ -2760,6 +2766,7 @@ name = "ytbn_graphing_software" version = "0.1.0" dependencies = [ "async-lock", + "base64", "benchmarks", "bincode", "cfg-if 1.0.0", diff --git a/Cargo.toml b/Cargo.toml index 5c9c98f..7add5ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ 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 diff --git a/src/consts.rs b/src/consts.rs index b72eea0..343ae67 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -6,10 +6,12 @@ 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: {}", - &build::SHORT_COMMIT, + &COMMIT, &build::BRANCH, &build::BUILD_TIME, &build::PKG_VERSION, diff --git a/src/math_app.rs b/src/math_app.rs index d891586..3375e5f 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -110,18 +110,21 @@ impl MathApp { #[cfg(not(threading))] tracing::info!("Threading: Disabled"); + tracing::info!("Initializing..."); + let start = instant::Instant::now(); + cfg_if::cfg_if! { if #[cfg(target_arch = "wasm32")] { use wasm_bindgen::JsCast; - use web_sys::HtmlElement; + use web_sys::{HtmlElement, Window}; if let Some(web_info) = &cc.integration_info.web_info { tracing::info!("Web Info: {:?}", web_info); } - let document = web_sys::window() - .expect("Could not get web_sys window") - .document() - .expect("Could not get web_sys document"); + + let window = web_sys::window().expect("Could not get web_sys window"); + + let document = window.document().expect("Could not get web_sys document"); let loading_element = document .get_element_by_id("loading") @@ -134,23 +137,53 @@ impl MathApp { unsafe { loading_element.get_attribute("value").unwrap_unchecked().parse::().unwrap_unchecked() }; loading_element.set_attribute("value", &(add + value).to_string()).unwrap(); } + + const COMPRESSED_NAME: &str = const_format::formatc!("YTBN-{}-DECOMPRESSED", COMMIT); + fn get_storage_decompressed() -> Option> { + 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")) + } else { + None + } + } + + fn set_storage_decompressed(data: &Vec) { + 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)); + } else { + panic!("unable to get local storage") + } + } + } else { + const fn get_storage_decompressed() -> Option> { + None + } + + const fn set_storage_decompressed(_: &Vec) {} } } - tracing::info!("Initializing..."); - let start = instant::Instant::now(); - - let mut data = Vec::new(); - let _ = unsafe { - ruzstd::StreamingDecoder::new( - &mut include_bytes!(concat!(env!("OUT_DIR"), "/compressed_data")).as_slice(), - ) - .unwrap_unchecked() - .read_to_end(&mut data) - .unwrap_unchecked() + let data_decompressed: Vec = if let Some(cached_data) = get_storage_decompressed() { + cached_data + } else { + let mut data = Vec::new(); + let _ = unsafe { + ruzstd::StreamingDecoder::new( + &mut include_bytes!(concat!(env!("OUT_DIR"), "/compressed_data")).as_slice(), + ) + .unwrap_unchecked() + .read_to_end(&mut data) + .unwrap_unchecked() + }; + set_storage_decompressed(&data); + data }; - let data: crate::data::TotalData = bincode::deserialize(data.as_slice()).unwrap(); + let data: crate::data::TotalData = + bincode::deserialize(data_decompressed.as_slice()).unwrap(); #[cfg(target_arch = "wasm32")] update_loading(&loading_element, 30);