File size: 2,633 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<script>
  import { appDataDir, resolve } from '@tauri-apps/api/path'
  import { LazyStore } from '@tauri-apps/plugin-store'
  import { onMount } from 'svelte'

  let { onMessage } = $props()

  let key = $state()
  let value = $state()

  const storeName = 'cache.json'

  let store = new LazyStore(storeName)
  let path = $state('')
  let cache = $state({})

  async function refreshEntries() {
    try {
      const values = await store.entries()
      cache = {}
      for (const [key, value] of values) {
        cache[key] = value
      }
    } catch (error) {
      onMessage(error)
    }
  }

  onMount(async () => {
    path = await resolve(await appDataDir(), storeName)
    await refreshEntries()
  })

  async function write(key, value) {
    try {
      if (value) {
        await store.set(key, value)
      } else {
        await store.delete(key)
      }
      const v = await store.get(key)
      if (v === undefined) {
        delete cache[key]
        cache = cache
      } else {
        cache[key] = v
      }
    } catch (error) {
      onMessage(error)
    }
  }

  async function reset() {
    try {
      await store.reset()
    } catch (error) {
      onMessage(error)
    }
    await refreshEntries()
  }

  async function reload() {
    try {
      await store.reload({ overrideDefaults: true })
    } catch (error) {
      onMessage(error)
    }
    await refreshEntries()
  }

  async function close() {
    try {
      await store.close()
      onMessage('Store is now closed, any new operations will error out')
    } catch (error) {
      onMessage(error)
    }
  }

  function reopen() {
    store = new LazyStore(storeName)
    onMessage('We made a new `LazyStore` instance, operations will now work')
  }
</script>

<div class="flex flex-col childre:grow gap-1">
  <div class="flex flex-col flex-row-md gap-4">
    <div class="flex items-center gap-1">
      Key:
      <input class="grow input" bind:value={key} />
    </div>

    <div class="flex items-center gap-1">
      Value:
      <input class="grow input" bind:value />
    </div>

    <div>
      <button class="btn" onclick={() => write(key, value)}>Write</button>
      <button class="btn" onclick={() => reset()}>Reset</button>
      <button class="btn" onclick={() => reload()}>Reload</button>
      <button class="btn" onclick={() => close()}>Close</button>
      <button class="btn" onclick={() => reopen()}>Re-open</button>
    </div>
    <div>Store at <code>{path}</code> on disk</div>
  </div>

  <div>
    <h2>Store Values</h2>
    {#each Object.entries(cache) as [k, v]}
      <div>{k} = {v}</div>
    {/each}
  </div>
</div>