reorganization of data loading logic

This commit is contained in:
Simon Gardling 2022-05-16 10:13:29 -04:00
parent 4bb7fea2de
commit 54c0703946
2 changed files with 26 additions and 24 deletions

View File

@ -104,6 +104,11 @@ pub struct MathApp {
since_last_save: Instant, since_last_save: Instant,
} }
#[cfg(target_arch = "wasm32")]
const DATA_NAME: &str = "YTBN-DECOMPRESSED";
#[cfg(target_arch = "wasm32")]
const FUNC_NAME: &str = "YTBN-FUNCTIONS";
impl MathApp { impl MathApp {
#[allow(dead_code)] // This is used lol #[allow(dead_code)] // This is used lol
/// Create new instance of [`MathApp`] and return it /// Create new instance of [`MathApp`] and return it
@ -133,13 +138,10 @@ impl MathApp {
get_window().local_storage().expect("failed to get localstorage1").expect("failed to get localstorage2") get_window().local_storage().expect("failed to get localstorage1").expect("failed to get localstorage2")
} }
const DATA_NAME: &str = "YTBN-DECOMPRESSED";
fn get_storage_decompressed() -> Option<Vec<u8>> { fn get_storage_decompressed() -> Option<Vec<u8>> {
let data = get_localstorage().get_item(DATA_NAME).ok()??; let data = get_localstorage().get_item(DATA_NAME).ok()??;
if crate::misc::HASH_LENGTH >= data.len() {
debug_assert!(!data.is_empty()); return None;
unsafe {
assume(!data.is_empty());
} }
let (commit, cached_data) = crate::misc::hashed_storage_read(data); let (commit, cached_data) = crate::misc::hashed_storage_read(data);
@ -177,7 +179,11 @@ impl MathApp {
} }
fn get_functions() -> Option<FunctionManager> { fn get_functions() -> Option<FunctionManager> {
if let Ok(Some(data)) = get_localstorage().get_item("YTBN-FUNCTIONS") { let data = get_localstorage().get_item(FUNC_NAME).ok()??;
if crate::misc::HASH_LENGTH >= data.len() {
return None;
}
let (commit, func_data) = crate::misc::hashed_storage_read(data); let (commit, func_data) = crate::misc::hashed_storage_read(data);
debug_assert!(!commit.is_empty()); debug_assert!(!commit.is_empty());
@ -197,10 +203,6 @@ impl MathApp {
// is invalid // is invalid
None None
} }
} else {
None
}
} }
} else { } else {
@ -620,7 +622,7 @@ impl App for MathApp {
); );
// tracing::info!("Bytes: {}", saved_data.len()); // tracing::info!("Bytes: {}", saved_data.len());
local_storage local_storage
.set_item("YTBN-FUNCTIONS", saved_data) .set_item(FUNC_NAME, saved_data)
.expect("failed to set local function storage"); .expect("failed to set local function storage");
} else { } else {
panic!("unable to get local storage") panic!("unable to get local storage")

View File

@ -311,7 +311,7 @@ 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() (0..max_i).map(|x| (x as f64 * step) + min_x).collect()
} }
const HASH_LENGTH: usize = 8; pub const HASH_LENGTH: usize = 8;
#[allow(dead_code)] #[allow(dead_code)]
pub fn hashed_storage_create(hash: &[u8], data: &[u8]) -> String { pub fn hashed_storage_create(hash: &[u8], data: &[u8]) -> String {