Storage
Persistent storage.
Permission
storage
permission is required.
{
//...
"plugin": {
"permissions": {
"scopes": ["storage"]
}
}
}
Usage
API
Unlike store
, storage
handles nested values out of the box.
$storage.set("foo.bar", 1)
$storage.set("foo.baz", "2")
$storage.has("foo") // true
$storage.get<number>("foo.bar") // 1
$storage.get<Record<string, any>>("foo") // { "bar": 1, "baz": "2" }
$storage.set("foo", "bar")
$storage.get("foo") // bar
$storage.watch("foo", (value) => {})
Example
// A simple plugin that stores the history of scan durations
function init() {
$app.onScanCompleted((e) => {
// Store the scanning duration (in ms)
$store.set("scan-completed", e.duration)
e.next()
})
$ui.register((ctx) => {
// Callback is triggered when the value is updated
$store.watch<number>("scan-completed", (value) => {
const date = new Date()
const now = date.toISOString().replaceall(".", "_")
// Add the value to the history
$storage.set("scan-duration-history."+now, {
duration: value,
durationInSeconds: value/1000,
addedAt: date,
})
ctx.toast.info(`Scanning took ${value/1000} seconds!`)
})
function deleteHistory() {
$storage.remove("scan-duration-history")
}
})
}
Make sure your storage doesn't grow too big by doing some cleanup.
Good to know
The plugin storage is deleted when the plugin is uninstalled.
Last updated