Add simple persistent cache API

This commit is contained in:
NGnius (Graham) 2022-08-09 21:12:43 -04:00
parent a8644d2729
commit 6fcc4c9884
3 changed files with 25 additions and 3 deletions

4
Cargo.lock generated
View file

@ -1047,7 +1047,7 @@ dependencies = [
[[package]]
name = "usdpl-front"
version = "0.5.0"
version = "0.6.0"
dependencies = [
"console_error_panic_hook",
"hex",
@ -1063,7 +1063,7 @@ dependencies = [
[[package]]
name = "usdpl-rs"
version = "0.1.0"
version = "0.6.0"
[[package]]
name = "utf-8"

View file

@ -1,6 +1,6 @@
[package]
name = "usdpl-front"
version = "0.6.0"
version = "0.6.1"
authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
edition = "2021"
license = "GPL-3.0-only"

View file

@ -19,6 +19,8 @@ use usdpl_core::{socket::Packet, RemoteCall};
static mut CTX: UsdplContext = UsdplContext { port: 31337, id: 1,
#[cfg(feature = "encrypt")] key: Vec::new() };
static mut CACHE: Option<std::collections::HashMap<String, JsValue>> = None;
#[cfg(feature = "encrypt")]
fn encryption_key() -> Vec<u8> {
hex::decode(obfstr::obfstr!(env!("USDPL_ENCRYPTION_KEY"))).unwrap()
@ -69,6 +71,10 @@ pub fn init_usdpl(port: u16) {
key: encryption_key(),
};
}
unsafe {
CACHE = Some(std::collections::HashMap::new());
}
}
/// Get the targeted plugin framework, or "any" if unknown
@ -77,6 +83,22 @@ pub fn target() -> String {
usdpl_core::api::Platform::current().to_string()
}
/// Get the targeted plugin framework, or "any" if unknown
#[wasm_bindgen]
pub fn set_value(key: String, value: JsValue) -> JsValue {
unsafe {
CACHE.as_mut().unwrap().insert(key, value).unwrap_or(JsValue::NULL)
}
}
/// Get the targeted plugin framework, or "any" if unknown
#[wasm_bindgen]
pub fn get_value(key: String) -> JsValue {
unsafe {
CACHE.as_ref().unwrap().get(&key).map(|x| x.to_owned()).unwrap_or(JsValue::UNDEFINED)
}
}
/// Call a function on the back-end.
/// Returns null (None) if this fails for any reason.
#[wasm_bindgen]