Store

Key-value store.

When to use

  • Create a cache

  • Share values or functions between hooks

  • Share values or functions between hooks and UI context

The values aren't persisted when your plugin is reloaded.

How to use

$store makes state sharing between runtimes possible.

$store.set("foo", "bar")
$store.get("foo")
$store.watch("foo", (value) => {})
$store.getAll() // { "foo": "bar" }
$store.remove("foo")
$store.removeAll()
$store.has("foo") // false
$store.getOrSet("foo", () => { return "bar" })
$store.values() // ["bar"]

Example

my-plugin.ts
// 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 now = new Date().toISOString().replaceall(".", "_")
            $storage.set("scan-duration-history."+now, value)
            ctx.toast.info(`Scanning took ${value/1000} seconds!`)
        })
    })
}

Last updated