-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add local storage cache (#312)
* feat: add local storage cache * fix: use local storage directly * fix: use database_type in client * fix: rename db type * fix: clippy error
- Loading branch information
Showing
10 changed files
with
148 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
extern crate console_error_panic_hook; | ||
extern crate web_sys; | ||
|
||
use config::Config; | ||
use consensus::database::Database; | ||
use eyre::Result; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[derive(Clone)] | ||
pub struct LocalStorageDB; | ||
|
||
impl Database for LocalStorageDB { | ||
fn new(_config: &Config) -> Result<Self> { | ||
console_error_panic_hook::set_once(); | ||
let window = web_sys::window().unwrap(); | ||
if let Ok(Some(_local_storage)) = window.local_storage() { | ||
return Ok(Self {}); | ||
} | ||
|
||
eyre::bail!("local_storage not available") | ||
} | ||
|
||
fn load_checkpoint(&self) -> Result<Vec<u8>> { | ||
let window = web_sys::window().unwrap(); | ||
if let Ok(Some(local_storage)) = window.local_storage() { | ||
let checkpoint = local_storage.get_item("checkpoint"); | ||
if let Ok(Some(checkpoint)) = checkpoint { | ||
let checkpoint = checkpoint.strip_prefix("0x").unwrap_or(&checkpoint); | ||
return hex::decode(checkpoint) | ||
.map_err(|_| eyre::eyre!("Failed to decode checkpoint")); | ||
} | ||
eyre::bail!("checkpoint not found") | ||
} | ||
|
||
eyre::bail!("local_storage not available") | ||
} | ||
|
||
fn save_checkpoint(&self, checkpoint: &[u8]) -> Result<()> { | ||
let window = web_sys::window().unwrap(); | ||
if let Ok(Some(local_storage)) = window.local_storage() { | ||
local_storage | ||
.set_item("checkpoint", &hex::encode(checkpoint)) | ||
.unwrap_throw(); | ||
return Ok(()); | ||
} | ||
|
||
eyre::bail!("local_storage not available") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters