Use pretty json by default

pull/1860/head
Tony 8 months ago
parent 4037589000
commit 1ef9f6a487
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -37,11 +37,7 @@ pub fn run() {
.plugin(tauri_plugin_os::init()) .plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_shell::init())
.plugin( .plugin(tauri_plugin_store::Builder::default().build())
tauri_plugin_store::Builder::default()
.register_serialize_fn("pretty-json".to_owned(), pretty_json)
.build(),
)
.setup(move |app| { .setup(move |app| {
#[cfg(desktop)] #[cfg(desktop)]
{ {
@ -163,9 +159,3 @@ pub fn run() {
} }
}) })
} }
fn pretty_json(
cache: &std::collections::HashMap<String, serde_json::Value>,
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
Ok(serde_json::to_vec_pretty(&cache)?)
}

@ -7,14 +7,9 @@
let key; let key;
let value; let value;
let store; let store = new LazyStore("cache.json");
let cache = {}; let cache = {};
function newStore() {
store = new LazyStore("cache.json", { serializeFnName: "pretty-json" });
}
newStore()
async function refreshEntries() { async function refreshEntries() {
try { try {
const values = await store.entries(); const values = await store.entries();
@ -33,7 +28,11 @@
async function write(key, value) { async function write(key, value) {
try { try {
await store.set(key, value); if (value) {
await store.set(key, value);
} else {
await store.delete(key);
}
const v = await store.get(key); const v = await store.get(key);
cache[key] = v; cache[key] = v;
} catch (error) { } catch (error) {
@ -60,7 +59,7 @@
} }
function reopen() { function reopen() {
newStore() store = new LazyStore("cache.json");
onMessage("We made a new `LazyStore` instance, operations will now work"); onMessage("We made a new `LazyStore` instance, operations will now work");
} }
</script> </script>

@ -238,7 +238,7 @@ impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
fn default_serialize( fn default_serialize(
cache: &HashMap<String, JsonValue>, cache: &HashMap<String, JsonValue>,
) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> { ) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
Ok(serde_json::to_vec(&cache)?) Ok(serde_json::to_vec_pretty(&cache)?)
} }
fn default_deserialize( fn default_deserialize(
@ -277,16 +277,16 @@ impl<R: Runtime> Builder<R> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// fn pretty_json( /// fn no_pretty_json(
/// cache: &std::collections::HashMap<String, serde_json::Value>, /// cache: &std::collections::HashMap<String, serde_json::Value>,
/// ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> { /// ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
/// Ok(serde_json::to_vec_pretty(&cache)?) /// Ok(serde_json::to_vec(&cache)?)
/// } /// }
/// ///
/// tauri::Builder::default() /// tauri::Builder::default()
/// .plugin( /// .plugin(
/// tauri_plugin_store::Builder::default() /// tauri_plugin_store::Builder::default()
/// .register_serialize_fn("pretty-json".to_owned(), pretty_json) /// .register_serialize_fn("no-pretty-json".to_owned(), no_pretty_json)
/// .build(), /// .build(),
/// ) /// )
/// ``` /// ```
@ -302,6 +302,23 @@ impl<R: Runtime> Builder<R> {
} }
/// Use this serialize function for stores by default /// Use this serialize function for stores by default
///
/// # Examples
///
/// ```
/// fn no_pretty_json(
/// cache: &std::collections::HashMap<String, serde_json::Value>,
/// ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
/// Ok(serde_json::to_vec(&cache)?)
/// }
///
/// tauri::Builder::default()
/// .plugin(
/// tauri_plugin_store::Builder::default()
/// .default_serialize_fn(no_pretty_json)
/// .build(),
/// )
/// ```
pub fn default_serialize_fn(mut self, serialize_fn: SerializeFn) -> Self { pub fn default_serialize_fn(mut self, serialize_fn: SerializeFn) -> Self {
self.default_serialize = serialize_fn; self.default_serialize = serialize_fn;
self self

Loading…
Cancel
Save