mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-29 12:06:01 +02:00
Add lazy store
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script>
|
||||
import { Store } from "@tauri-apps/plugin-store";
|
||||
import { LazyStore } from "@tauri-apps/plugin-store";
|
||||
import { onMount } from "svelte";
|
||||
|
||||
export let onMessage;
|
||||
@@ -7,11 +7,10 @@
|
||||
let key;
|
||||
let value;
|
||||
|
||||
const store = new Store("cache.json");
|
||||
const store = new LazyStore("cache.json");
|
||||
let cache = {};
|
||||
|
||||
onMount(async () => {
|
||||
await store.load();
|
||||
const values = await store.entries();
|
||||
for (const [key, value] of values) {
|
||||
cache[key] = value;
|
||||
@@ -19,16 +18,14 @@
|
||||
cache = cache;
|
||||
});
|
||||
|
||||
function write(key, value) {
|
||||
store
|
||||
.set(key, value)
|
||||
.then(() => store.get(key))
|
||||
.then((v) => {
|
||||
cache[key] = v;
|
||||
cache = cache;
|
||||
})
|
||||
.then(() => store.save())
|
||||
.catch(onMessage);
|
||||
async function write(key, value) {
|
||||
try {
|
||||
await store.set(key, value);
|
||||
const v = await store.get(key);
|
||||
cache[key] = v;
|
||||
} catch (error) {
|
||||
onMessage(error);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user