Implement local storage for todos
example in Wasm
This commit is contained in:
parent
679d758627
commit
36e617ae70
@ -14,6 +14,10 @@ serde_json = "1.0"
|
|||||||
async-std = "1.0"
|
async-std = "1.0"
|
||||||
directories = "2.0"
|
directories = "2.0"
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
web-sys = { version = "0.3", features = ["Window", "Storage"] }
|
||||||
|
wasm-timer = "0.2"
|
||||||
|
|
||||||
[package.metadata.deb]
|
[package.metadata.deb]
|
||||||
assets = [
|
assets = [
|
||||||
["target/release/todos", "usr/bin/iced-todos", "755"],
|
["target/release/todos", "usr/bin/iced-todos", "755"],
|
||||||
|
@ -557,15 +557,38 @@ impl SavedState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
impl SavedState {
|
impl SavedState {
|
||||||
|
fn storage() -> Option<web_sys::Storage> {
|
||||||
|
let window = web_sys::window()?;
|
||||||
|
|
||||||
|
window.local_storage().ok()?
|
||||||
|
}
|
||||||
|
|
||||||
async fn load() -> Result<SavedState, LoadError> {
|
async fn load() -> Result<SavedState, LoadError> {
|
||||||
Err(LoadError::FileError)
|
let storage = Self::storage().ok_or(LoadError::FileError)?;
|
||||||
|
|
||||||
|
let contents = storage
|
||||||
|
.get_item("state")
|
||||||
|
.map_err(|_| LoadError::FileError)?
|
||||||
|
.ok_or(LoadError::FileError)?;
|
||||||
|
|
||||||
|
serde_json::from_str(&contents).map_err(|_| LoadError::FormatError)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save(self) -> Result<(), SaveError> {
|
async fn save(self) -> Result<(), SaveError> {
|
||||||
Err(SaveError::FileError)
|
let storage = Self::storage().ok_or(SaveError::FileError)?;
|
||||||
|
|
||||||
|
let json = serde_json::to_string_pretty(&self)
|
||||||
|
.map_err(|_| SaveError::FormatError)?;
|
||||||
|
|
||||||
|
storage
|
||||||
|
.set_item("state", &json)
|
||||||
|
.map_err(|_| SaveError::WriteError)?;
|
||||||
|
|
||||||
|
let _ = wasm_timer::Delay::new(std::time::Duration::from_secs(2)).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user