cleanup web stuff
This commit is contained in:
@@ -101,6 +101,17 @@ pub struct MathApp {
|
|||||||
settings: AppSettings,
|
settings: AppSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
fn get_window() -> web_sys::Window { web_sys::window().expect("Could not get web_sys window") }
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
fn get_localstorage() -> web_sys::Storage {
|
||||||
|
get_window()
|
||||||
|
.local_storage()
|
||||||
|
.expect("failed to get localstorage1")
|
||||||
|
.expect("failed to get localstorage2")
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
const DATA_NAME: &str = "YTBN-DECOMPRESSED";
|
const DATA_NAME: &str = "YTBN-DECOMPRESSED";
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
@@ -129,14 +140,6 @@ impl MathApp {
|
|||||||
tracing::info!("Web Info: {:?}", web_info);
|
tracing::info!("Web Info: {:?}", web_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_window() -> web_sys::Window {
|
|
||||||
web_sys::window().expect("Could not get web_sys window")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_localstorage() -> web_sys::Storage {
|
|
||||||
get_window().local_storage().expect("failed to get localstorage1").expect("failed to get localstorage2")
|
|
||||||
}
|
|
||||||
|
|
||||||
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()??;
|
||||||
let (commit, cached_data) = crate::misc::hashed_storage_read(&data)?;
|
let (commit, cached_data) = crate::misc::hashed_storage_read(&data)?;
|
||||||
@@ -157,20 +160,6 @@ impl MathApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_storage_decompressed(data: &[u8]) {
|
|
||||||
debug_assert!(!data.is_empty());
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
assume(!data.is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
tracing::info!("Setting decompression cache");
|
|
||||||
let commit: crate::misc::HashBytes = const { unsafe { std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT) } };
|
|
||||||
let saved_data = &crate::misc::hashed_storage_create(commit, data);
|
|
||||||
tracing::info!("Bytes: {}", saved_data.len());
|
|
||||||
get_localstorage().set_item(DATA_NAME, saved_data).expect("failed to set local storage cache");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_functions() -> Option<FunctionManager> {
|
fn load_functions() -> Option<FunctionManager> {
|
||||||
let data = get_localstorage().get_item(FUNC_NAME).ok()??;
|
let data = get_localstorage().get_item(FUNC_NAME).ok()??;
|
||||||
if crate::misc::HASH_LENGTH >= data.len() {
|
if crate::misc::HASH_LENGTH >= data.len() {
|
||||||
@@ -212,7 +201,25 @@ impl MathApp {
|
|||||||
.unwrap_unchecked()
|
.unwrap_unchecked()
|
||||||
};
|
};
|
||||||
#[cfg(target = "wasm32")]
|
#[cfg(target = "wasm32")]
|
||||||
set_storage_decompressed(&data);
|
{
|
||||||
|
debug_assert!(!data.is_empty());
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
assume(!data.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
tracing::info!("Setting decompression cache");
|
||||||
|
let commit: crate::misc::HashBytes = const {
|
||||||
|
unsafe {
|
||||||
|
std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let saved_data = &crate::misc::hashed_storage_create(commit, data);
|
||||||
|
tracing::info!("Bytes: {}", saved_data.len());
|
||||||
|
get_localstorage()
|
||||||
|
.set_item(DATA_NAME, saved_data)
|
||||||
|
.expect("failed to set local storage cache");
|
||||||
|
}
|
||||||
|
|
||||||
unsafe { bincode::deserialize(data.as_slice()).unwrap_unchecked() }
|
unsafe { bincode::deserialize(data.as_slice()).unwrap_unchecked() }
|
||||||
}
|
}
|
||||||
@@ -612,10 +619,6 @@ impl App for MathApp {
|
|||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
fn save(&mut self, _: &mut dyn eframe::Storage) {
|
fn save(&mut self, _: &mut dyn eframe::Storage) {
|
||||||
if let Ok(Some(local_storage)) = web_sys::window()
|
|
||||||
.expect("Could not get web_sys window")
|
|
||||||
.local_storage()
|
|
||||||
{
|
|
||||||
tracing::info!("Saving function data");
|
tracing::info!("Saving function data");
|
||||||
let hash: crate::misc::HashBytes =
|
let hash: crate::misc::HashBytes =
|
||||||
unsafe { std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT) };
|
unsafe { std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT) };
|
||||||
@@ -624,12 +627,9 @@ impl App for MathApp {
|
|||||||
bincode::serialize(&self.functions).unwrap().as_slice(),
|
bincode::serialize(&self.functions).unwrap().as_slice(),
|
||||||
);
|
);
|
||||||
// tracing::info!("Bytes: {}", saved_data.len());
|
// tracing::info!("Bytes: {}", saved_data.len());
|
||||||
local_storage
|
get_localstorage()
|
||||||
.set_item(FUNC_NAME, saved_data)
|
.set_item(FUNC_NAME, saved_data)
|
||||||
.expect("failed to set local function storage");
|
.expect("failed to set local function storage");
|
||||||
} else {
|
|
||||||
panic!("unable to get local storage")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear_color(&self, _visuals: &egui::Visuals) -> egui::Rgba {
|
fn clear_color(&self, _visuals: &egui::Visuals) -> egui::Rgba {
|
||||||
|
|||||||
Reference in New Issue
Block a user