@kkdev92/vscode-ext-kit - v4.1.1
    Preparing search index...

    Function createWebviewRpc

    • Creates a typed request/response + event RPC channel over a vscode.Webview. VS Code's own postMessage/onDidReceiveMessage are fire-and-forget with no correlation, timeout, or cancellation built in — this fills that gap.

      Works with both WebviewPanel.webview and WebviewView.webview (they share the same Webview shape), and ManagedWebviewPanel/ ManagedWebviewView both expose this automatically as .rpc.

      The webview-side counterpart ships as @kkdev92/vscode-ext-kit/webview-client — vscode-free, so it bundles into webview code — and is written against the same WebviewRpcSchema, which keeps method names and TypeScript payload shapes aligned. It does not make values crossing postMessage trustworthy: validate them in handlers.

      Type Parameters

      Parameters

      • webview: WebviewLike

        The webview to layer the RPC channel over

      Returns WebviewRpc<S>

      interface MyRpcSchema extends WebviewRpcSchema {
      webviewRequests: { getSelection: { params: void; result: { text: string } } };
      hostRequests: { save: { params: { content: string }; result: { ok: boolean } } };
      hostEvents: { theme: { kind: 'light' | 'dark' } };
      webviewEvents: { dirty: { isDirty: boolean } };
      }

      const rpc = createWebviewRpc<MyRpcSchema>(panel.webview);
      rpc.onRequest('save', async ({ content }) => ({ ok: await writeFile(content) }));
      rpc.onEvent('dirty', ({ isDirty }) => updateTabTitle(isDirty));
      rpc.emit('theme', { kind: 'dark' });
      const { text } = await rpc.request('getSelection', undefined, { timeoutMs: 5000 });