Skip to content

Commit 4bfeb6c

Browse files
committed
feat(docs): add context.storage documentation and usage examples
1 parent c4ccb05 commit 4bfeb6c

3 files changed

Lines changed: 401 additions & 9 deletions

File tree

‎.vitepress/sidebar.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function sidebar(): DefaultTheme.SidebarItem[] {
3838
{ text: 'context.rcp', link: '/runtime-api/rcp' },
3939
{ text: 'context.socket', link: '/runtime-api/socket' },
4040
{ text: 'context.fs (PluginFS)', link: '/runtime-api/fs' },
41+
{ text: 'context.storage', link: '/runtime-api/storage' },
4142
{
4243
text: 'Modules',
4344
// collapsed: false,

‎docs/runtime-api/data-store.md‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ browsers. This namespace helps you to store user data on disk.
66
The whole store is read from disk **before any plugin runs**, and kept in memory
77
for the rest of the session. Reads (`get`, `has`) are therefore synchronous and
88
safe to call from your plugin's `init`. Writes update memory immediately and
9-
commit to disk on a short debounce, so a burst of `set` calls (a settings slider,
10-
say) collapses into a single write.
9+
reach disk a moment later; repeated `set` calls on the same key (a settings
10+
slider, say) collapse into a single write.
11+
12+
::: tip Storing more than a few settings?
13+
14+
Every plugin shares this one store, and it's all held in memory. For datasets,
15+
caches, or anything that grows, use [`context.storage`](./storage) — it's
16+
per-plugin, and the cost of a write doesn't grow with how much you've stored.
17+
18+
:::
1119

1220
## DataStore.set()
1321

@@ -34,11 +42,14 @@ functions and runtime objects will be ignored.
3442

3543
#### Returns
3644

37-
`true` if the value was accepted, `false` if `key` was not a string.
45+
`true` if the value was accepted.
3846

39-
The return value is **not** a write confirmation — it means "stored in memory,
40-
and it will reach disk shortly". Use [`flush()`](#datastore-flush) if you need
41-
to know the data is durable.
47+
`false` if `key` wasn't a string, if the value is something JSON can't represent,
48+
or if the store is [full](#storage-limits).
49+
50+
A `true` is **not** a write confirmation — it means "stored in memory, and it
51+
will reach disk shortly". Use [`flush()`](#datastore-flush) if you need to know
52+
the data is durable.
4253

4354
#### Example
4455

@@ -172,9 +183,9 @@ function flush(): Promise<void>
172183
Writes any pending changes out immediately and resolves once they are durable
173184
on disk.
174185

175-
Most plugins never need this — the debounced commit already handles normal use.
176-
Reach for it when you are about to do something that could end the session
177-
before the debounce fires, such as calling `restartClient()`.
186+
Most plugins never need this — normal writes reach disk on their own. Reach for
187+
it when you're about to do something that could end the session first, such as
188+
calling `restartClient()`.
178189

179190
#### Example
180191

@@ -183,3 +194,39 @@ DataStore.set('my-config', config)
183194
await DataStore.flush()
184195
window.restartClient()
185196
```
197+
198+
## DataStore.usage()
199+
200+
<Badge type="info" text="function" />
201+
<Badge type="tip" text="since v1.2.0" />
202+
203+
```ts
204+
function usage(): Promise<{ used: number, quota: number }>
205+
```
206+
207+
How much of the store is in use, and the cap — both in bytes.
208+
209+
#### Example
210+
211+
```js
212+
const { used, quota } = await DataStore.usage()
213+
console.log(`${Math.round(used / quota * 100)}% of DataStore used`)
214+
```
215+
216+
## Storage limits
217+
218+
DataStore is capped at **128 MB**, and that budget is **shared by every
219+
installed plugin** — it's one file. One plugin filling it stops every other
220+
plugin from saving.
221+
222+
When it's full, `set` returns `false` and nothing is written; a warning appears
223+
in the console once. Existing data is untouched and still readable. Removing
224+
keys frees space, and writes start working again shortly after.
225+
226+
::: tip
227+
228+
If you're storing anything that grows — cached data, per-match records, a
229+
dataset — use [`context.storage`](./storage) instead. It's per-plugin, gets
230+
256 MB of its own, and nothing another plugin does can use it up.
231+
232+
:::

0 commit comments

Comments
 (0)