mirror of
https://github.com/gradio-app/gradio.git
synced 2024-12-15 02:11:15 +08:00
fc06fe41f0
* localstate * add changeset * changes * changes * changes * add changeset * changes * add changeset * format * notebook * some changes * add changeset * format * fix * changes * fix js lint and ts * add changeset * fix pytest * component demo * rename * rename * notebooks * revert * changes * revert * revert * revert * changes * changes * format * fix * notebook * docstring * guide * types * cleanup --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
52 lines
1.2 KiB
Svelte
52 lines
1.2 KiB
Svelte
<svelte:options accessors={true} />
|
|
|
|
<script lang="ts">
|
|
import { beforeUpdate } from "svelte";
|
|
import { encrypt, decrypt } from "./crypto";
|
|
import { dequal } from "dequal/lite";
|
|
|
|
export let storage_key: string;
|
|
export let secret: string;
|
|
export let default_value: any;
|
|
export let value = default_value;
|
|
let initialized = false;
|
|
let old_value = value;
|
|
|
|
function load_value(): void {
|
|
const stored = localStorage.getItem(storage_key);
|
|
if (!stored) {
|
|
old_value = default_value;
|
|
value = old_value;
|
|
return;
|
|
}
|
|
try {
|
|
const decrypted = decrypt(stored, secret);
|
|
old_value = JSON.parse(decrypted);
|
|
value = old_value;
|
|
} catch (e) {
|
|
console.error("Error reading from localStorage:", e);
|
|
old_value = default_value;
|
|
value = old_value;
|
|
}
|
|
}
|
|
|
|
function save_value(): void {
|
|
try {
|
|
const encrypted = encrypt(JSON.stringify(value), secret);
|
|
localStorage.setItem(storage_key, encrypted);
|
|
old_value = value;
|
|
} catch (e) {
|
|
console.error("Error writing to localStorage:", e);
|
|
}
|
|
}
|
|
|
|
$: value && !dequal(value, old_value) && save_value();
|
|
|
|
beforeUpdate(() => {
|
|
if (!initialized) {
|
|
initialized = true;
|
|
load_value();
|
|
}
|
|
});
|
|
</script>
|