Skip to content

store add

Scaffold a reactive store under src/stores/. A store holds one observable value; mutate it and anything watching reacts. See the Reactivity guide for the full picture.

Terminal window
vsceasy store add --name badgeCount --type number --initial 0

Flags

FlagTypeNotes
--nametextRequired. Store id (camelCase, e.g. badgeCount).
--typenumber | string | boolean | jsonValue type. Default number.
--initialtextInitial value expression. Defaults per type: 0 / '' / false / null.

What it generates

src/stores/badgeCount.ts
import { defineStore } from '../shared/vsceasy';
export const badgeCount = defineStore<number>(0);

defineStore gives get(), set(v), update(fn), and subscribe(cb).

Using it

Mutate the store anywhere on the host:

import { badgeCount } from '../stores/badgeCount';
badgeCount.set(3);
badgeCount.update((n) => n + 1);

Push changes to a webview — watch the store in a panel’s rpc() and emit:

import { watch } from '../shared/vsceasy';
import { badgeCount } from '../stores/badgeCount';
rpc: (vscode, ctx, emit) => {
watch(badgeCount, () => emit('badgeCount:changed', badgeCount.get()));
return { /* … */ };
}

React in the webview:

import { listen } from '../shared/vsceasy/client';
listen(api, 'badgeCount:changed', (v) => render(v));

Examples

Terminal window
# a boolean flag, starts false
vsceasy store add --name sidebarOpen --type boolean
# a string with a custom initial value
vsceasy store add --name filter --type string --initial "'all'"
# arbitrary JSON state
vsceasy store add --name selection --type json --initial "{ ids: [] }"

See the Reactivity guide for ORM-entity reactivity (watchEntity) and the host↔webview flow.