cache data in browser

This commit is contained in:
Simon Gardling
2022-05-11 17:14:06 -04:00
parent 051197bfe2
commit cd930966b9
5 changed files with 62 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
{ {
"files.insertFinalNewline": true, "files.insertFinalNewline": true,
"editor.formatOnSave": true, "editor.formatOnSave": true,
"files.trimTrailingWhitespace": true "files.trimTrailingWhitespace": true,
} }

7
Cargo.lock generated
View File

@@ -143,6 +143,12 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "base64"
version = "0.20.0-alpha.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "149ea5dc24cb11513350770afebba32b68e3d2e356f9221351a2a1ee89112a82"
[[package]] [[package]]
name = "benchmarks" name = "benchmarks"
version = "0.1.0" version = "0.1.0"
@@ -2760,6 +2766,7 @@ name = "ytbn_graphing_software"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-lock", "async-lock",
"base64",
"benchmarks", "benchmarks",
"bincode", "bincode",
"cfg-if 1.0.0", "cfg-if 1.0.0",

View File

@@ -83,6 +83,7 @@ wee_alloc = "0.4"
wasm-bindgen = { version = "0.2", default-features = false, features = ["std"] } wasm-bindgen = { version = "0.2", default-features = false, features = ["std"] }
web-sys = "0.3" web-sys = "0.3"
tracing-wasm = "0.2" tracing-wasm = "0.2"
base64 = "0.20.0-alpha.1"
[package.metadata.cargo-all-features] [package.metadata.cargo-all-features]
skip_optional_dependencies = true #don't test optional dependencies, only features skip_optional_dependencies = true #don't test optional dependencies, only features

View File

@@ -6,10 +6,12 @@ use epaint::Color32;
use shadow_rs::shadow; use shadow_rs::shadow;
shadow!(build); shadow!(build);
pub const COMMIT: &str = build::SHORT_COMMIT;
// Constant string that has a string containing information about the build. // Constant string that has a string containing information about the build.
pub const BUILD_INFO: &str = formatc!( pub const BUILD_INFO: &str = formatc!(
"Commit: {} ({})\nBuild Date: {}\nPackage Version: {}\nRust Channel: {}\nRust Version: {}", "Commit: {} ({})\nBuild Date: {}\nPackage Version: {}\nRust Channel: {}\nRust Version: {}",
&build::SHORT_COMMIT, &COMMIT,
&build::BRANCH, &build::BRANCH,
&build::BUILD_TIME, &build::BUILD_TIME,
&build::PKG_VERSION, &build::PKG_VERSION,

View File

@@ -110,18 +110,21 @@ impl MathApp {
#[cfg(not(threading))] #[cfg(not(threading))]
tracing::info!("Threading: Disabled"); tracing::info!("Threading: Disabled");
tracing::info!("Initializing...");
let start = instant::Instant::now();
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] { if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
use web_sys::HtmlElement; use web_sys::{HtmlElement, Window};
if let Some(web_info) = &cc.integration_info.web_info { if let Some(web_info) = &cc.integration_info.web_info {
tracing::info!("Web Info: {:?}", web_info); tracing::info!("Web Info: {:?}", web_info);
} }
let document = web_sys::window()
.expect("Could not get web_sys window") let window = web_sys::window().expect("Could not get web_sys window");
.document()
.expect("Could not get web_sys document"); let document = window.document().expect("Could not get web_sys document");
let loading_element = document let loading_element = document
.get_element_by_id("loading") .get_element_by_id("loading")
@@ -134,12 +137,38 @@ impl MathApp {
unsafe { loading_element.get_attribute("value").unwrap_unchecked().parse::<i32>().unwrap_unchecked() }; unsafe { loading_element.get_attribute("value").unwrap_unchecked().parse::<i32>().unwrap_unchecked() };
loading_element.set_attribute("value", &(add + value).to_string()).unwrap(); 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<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"))
} else {
None
} }
} }
tracing::info!("Initializing..."); fn set_storage_decompressed(data: &Vec<u8>) {
let start = instant::Instant::now(); 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<Vec<u8>> {
None
}
const fn set_storage_decompressed(_: &Vec<u8>) {}
}
}
let data_decompressed: Vec<u8> = if let Some(cached_data) = get_storage_decompressed() {
cached_data
} else {
let mut data = Vec::new(); let mut data = Vec::new();
let _ = unsafe { let _ = unsafe {
ruzstd::StreamingDecoder::new( ruzstd::StreamingDecoder::new(
@@ -149,8 +178,12 @@ impl MathApp {
.read_to_end(&mut data) .read_to_end(&mut data)
.unwrap_unchecked() .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")] #[cfg(target_arch = "wasm32")]
update_loading(&loading_element, 30); update_loading(&loading_element, 30);