mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-05-29 13:31:27 +02:00
Use pretty json by default
This commit is contained in:
@@ -37,11 +37,7 @@ pub fn run() {
|
||||
.plugin(tauri_plugin_os::init())
|
||||
.plugin(tauri_plugin_process::init())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.plugin(
|
||||
tauri_plugin_store::Builder::default()
|
||||
.register_serialize_fn("pretty-json".to_owned(), pretty_json)
|
||||
.build(),
|
||||
)
|
||||
.plugin(tauri_plugin_store::Builder::default().build())
|
||||
.setup(move |app| {
|
||||
#[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 value;
|
||||
|
||||
let store;
|
||||
let store = new LazyStore("cache.json");
|
||||
let cache = {};
|
||||
|
||||
function newStore() {
|
||||
store = new LazyStore("cache.json", { serializeFnName: "pretty-json" });
|
||||
}
|
||||
newStore()
|
||||
|
||||
async function refreshEntries() {
|
||||
try {
|
||||
const values = await store.entries();
|
||||
@@ -33,7 +28,11 @@
|
||||
|
||||
async function write(key, value) {
|
||||
try {
|
||||
await store.set(key, value);
|
||||
if (value) {
|
||||
await store.set(key, value);
|
||||
} else {
|
||||
await store.delete(key);
|
||||
}
|
||||
const v = await store.get(key);
|
||||
cache[key] = v;
|
||||
} catch (error) {
|
||||
@@ -60,7 +59,7 @@
|
||||
}
|
||||
|
||||
function reopen() {
|
||||
newStore()
|
||||
store = new LazyStore("cache.json");
|
||||
onMessage("We made a new `LazyStore` instance, operations will now work");
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -238,7 +238,7 @@ impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
|
||||
fn default_serialize(
|
||||
cache: &HashMap<String, JsonValue>,
|
||||
) -> 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(
|
||||
@@ -277,16 +277,16 @@ impl<R: Runtime> Builder<R> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// fn pretty_json(
|
||||
/// 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_pretty(&cache)?)
|
||||
/// Ok(serde_json::to_vec(&cache)?)
|
||||
/// }
|
||||
///
|
||||
/// tauri::Builder::default()
|
||||
/// .plugin(
|
||||
/// 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(),
|
||||
/// )
|
||||
/// ```
|
||||
@@ -302,6 +302,23 @@ impl<R: Runtime> Builder<R> {
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
self.default_serialize = serialize_fn;
|
||||
self
|
||||
|
||||
Reference in New Issue
Block a user