use crate::i18n::js_service_unavailable_error;
use lxapp::{LxApp, lx};
use rong::{JSContext, JSContextService, JSFunc, JSResult};
use rong_storage::{Storage as RongStorage, StorageOptions};
const STORAGE_MAX_KEY_BYTES: u32 = 1024; const STORAGE_MAX_VALUE_BYTES: u32 = 5 * 1024 * 1024;
const STORAGE_MAX_DATA_BYTES: u32 = 20 * 1024 * 1024;
fn storage_options() -> StorageOptions {
StorageOptions {
max_key_size: Some(STORAGE_MAX_KEY_BYTES),
max_value_size: Some(STORAGE_MAX_VALUE_BYTES),
max_data_size: Some(STORAGE_MAX_DATA_BYTES),
}
}
#[derive(Clone)]
struct LxStorageService {
storage: RongStorage,
}
impl JSContextService for LxStorageService {
fn on_shutdown(&self) {
self.storage.close();
}
}
fn get_storage(ctx: JSContext) -> JSResult<RongStorage> {
if let Some(existing) = ctx.get_service::<LxStorageService>() {
return Ok(existing.storage.clone());
}
let lxapp = LxApp::from_ctx(&ctx)?;
if lxapp.storage_file_path.as_os_str().is_empty() {
return Err(js_service_unavailable_error(
"Storage path is not configured for this app",
));
}
let options = storage_options();
let storage = RongStorage::new(lxapp.storage_file_path.clone(), options)?;
ctx.set_service::<LxStorageService>(LxStorageService {
storage: storage.clone(),
});
Ok(storage)
}
pub(crate) fn init(ctx: &JSContext) -> JSResult<()> {
let get_storage_fn = JSFunc::new(ctx, get_storage)?.name("getStorage")?;
lx::register_js_api(ctx, "getStorage", get_storage_fn)?;
Ok(())
}