Skip to content

Events, Data and Commands

This page is a cheat sheet, not a tutorial. It collects every data, event, and command operation in one place so you can look up the syntax without hunting through other pages. For the concepts behind grants, see Access Grants. For the backend context, see Backend.

Accessing the bridge

  • React: call useBridge() from any component inside a plugin surface subtree (hooks reference).
  • JsComponent: use the ctx argument passed to your factory (JsComponentContext interface).
  • Backend: use ctx directly in activate() / deactivate().

Dormant-state behaviour

React and JsComponent components have identical capability when the plugin is active. Only the dormant failure mechanism differs: useBridge() throws synchronously (the error boundary catches it and retries), while a JsComponent's ctx methods degrade individually.

Method Dormant behaviour Silent?
useBridge() Throws synchronously because there is no PluginBridge in context. The error boundary shows "[Plugin failed]" until activation. usePluginData degrades to undefined. No (throws)
ctx.data.get(key) Plugin/own keys: sends a data-get to main, which wakes the dormant plugin, and resolves the value exposed during activation (or undefined if the key is never exposed). Host keys: waits for the bridge with a timeout, then resolves undefined. Yes
ctx.data.on(key, cb) Plugin/own keys: delivers undefined once, then subscribes when the store arrives (replay-1 semantics preserved). Host keys: delivers undefined once, then attaches to the plugin's bridge when it is published (and again after a plugin reload). Yes
ctx.commands.call(key) Rejects with a descriptive error ("plugin is dormant"). No (rejects)
ctx.commands.stream(key) Returns a handle that errors immediately, and calls cbs.error if provided. No (errors)
ctx.events.on(key, cb) Attaches to the plugin's bridge when it is published, and again after a plugin reload. Events fired before then are not buffered or replayed. Yes
ctx.events.emit(key, payload) Silently dropped. Yes

If you need state at activation, use ctx.data with own keys. The callback fires immediately with the current value and again on every change, even if the store arrives after the subscription was created.


Data

Expose (main side only)

const handle = ctx.data.expose("myKey", initialValue);
handle.set(newValue);
const current = handle.get();

The exposed key is auto-prefixed to plugin:<self>/myKey.

Read and subscribe

Scope Main React JsComponent
Own key await ctx.data.get("myKey") const [val, set] = usePluginData("myKey") await ctx.data.get("myKey")
Host key ctx.data.on("repository.current", cb) useCurrentRepository() ctx.data.on("repository.current", cb)
Foreign key await ctx.data.get("plugin:@other/id/key") const [val] = usePluginData("plugin:@other/id/key") await ctx.data.get("plugin:@other/id/key")
  • data.on replays the current value on subscribe (replay-1). data.get is a one-shot read.
  • Cross-process reads work: the renderer reads what main exposed, gate-checked.
  • Ownership rule: the exposing side gets [value, setter], and a non-owner gets [value] only. A non-owner write is a silent no-op.

Host data keys

Declared in the SDK's HostDataContract: the repository.* keys, app.language, and workspace.view. Each read needs a matching data: grant.

Key Shape Notes
repository.* see RepositoryData Main-side. repository.revisions takes a { branchId } argument.
app.language { language: string; locale: string } Host UI language and locale, served renderer-side from the host's i18next instance. It replays once on subscribe and does not change today, because the host has no language switcher.
workspace.view { kind: "whole" \| "named" \| "custom"; name: string \| null; rules: string[]; revision: string \| null } The repository's current view, read-only. Grant: data:workspace.view. Today kind is "custom" when the repository has an active view and "whole" otherwise; name is always null and rules is always empty.

Host events

Key Payload Notes
workspace.viewChanged { kind: "whole" \| "named" \| "custom"; name: string \| null } Grant: events:workspace.viewChanged. Not emitted yet. The key is declared so you can subscribe ahead of time, but a subscriber receives nothing today.

Host commands

Key Arguments Notes
workspace.requestView { viewFile: string } Grant: call:workspace.requestView. Opens the host's repository view dialog prefilled with viewFile, which must be a bare file name, not a path, and requires a loaded repository. A confirmation banner names the requesting plugin. The command never applies the view itself: the user still has to confirm in the dialog.

Events

Emit (bare key; the host auto-prefixes plugin:<self>/)

// Main
ctx.events.emit("branchCreated", { branch: "main" });

// JsComponent
ctx.events.emit("refreshed", { ts: Date.now() });

Passing an already-prefixed key to emit throws a TypeError.

Subscribe (fully qualified key)

Scope Main React JsComponent
Own event ctx.events.on("plugin:@acme/my-plugin/x", cb) useEvents("plugin:@acme/my-plugin/x", cb) ctx.events.on("plugin:@acme/my-plugin/x", cb)
Host event ctx.events.on("branches.updated", cb) useEvents("branches.updated", cb) ctx.events.on("branches.updated", cb)
Foreign event ctx.events.on("plugin:@other/id/x", cb) useEvents("plugin:@other/id/x", cb) ctx.events.on("plugin:@other/id/x", cb)
  • Every handler receives an array of payloads. Events of the same type fired within about 16 ms coalesce into one delivery, so iterate the array.
  • No replay: events.on never replays past events (contrast with data.on).
  • The emit/subscribe key asymmetry (bare vs. fully qualified) is explained in Access Grants.

Commands

Call

Scope Main React JsComponent
Own command await ctx.commands.call("plugin:@acme/my-plugin/refresh", {}) await bridge.commands.call("plugin:@acme/my-plugin/refresh", {}) await ctx.commands.call("plugin:@acme/my-plugin/refresh", {})
Host command await ctx.commands.call("repository.users", {}) await bridge.commands.call("repository.users", {}) await ctx.commands.call("repository.users", {})
Foreign command await ctx.commands.call("plugin:@other/id/doThing", { x: 1 }) await bridge.commands.call("plugin:@other/id/doThing", { x: 1 }) await ctx.commands.call("plugin:@other/id/doThing", { x: 1 })

Stream

const handle = ctx.commands.stream(
  "plugin:@other/id/streamData",
  { query: "..." },
  {
    next(item) { /* ... */ },
    error(err) { /* ... */ },
    complete() { /* ... */ },
  }
);
// handle.cancel() to abort

Error handling

A command call or data read sent while its plugin is shutting down does not wait for the 30 s call timeout. The call rejects at once with an error such as [<plugin id>] is not accepting inbound messages (phase=deactivating) — call dropped, and the data read resolves undefined.

Call errors serialize as strings across the plugin channel. Match errors by name or message, not by instanceof:

try {
  await ctx.commands.call("plugin:@other/id/cmd", args);
} catch (err) {
  if ((err as Error).message.includes("GrantDenied")) {
    // missing grant
  }
}

Identity is sealed

Every bridge instance has pluginId and parsed access grants frozen at construction. You cannot pass a different pluginId to a bridge call, because the bridge does not read it from arguments. A mutated manifest object cannot escalate grants at runtime.

Next

With the runtime API covered, the last piece is getting your plugin to compile. The next page covers the build pipeline and tsconfig setup.

Build and tsconfig