1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use js_sys::JSON;
use wasm_bindgen::prelude::*;
extern crate serde;
extern crate serde_json;
pub fn get_storage() -> Option<web_sys::Storage> {
let window = web_sys::window().unwrap();
match window.local_storage() {
Ok(Some(local_storage)) => {
Some(local_storage)
},
Err(E) => None,
Ok(None) => None
}
}
pub fn store_data<T>(storage: &web_sys::Storage, name: &str, data: &T)
where T: serde::Serialize
{
let serialized = serde_json::to_string(&data).unwrap();
storage.set_item(name, &serialized).unwrap();
}