Skip to content

Add ability to tap into acquireVsCodeApi[get|set]State mechanism from the extension host side #183657

Description

@eamodio

VS Code provide some state management for Webviews, but extensions from the extension host side can't really interact with it, other than via deserializeWebviewPanel, where we get access to the persisted state, but then need to manually provide that to the webview by either embedding it into the HTML or sending it later via postMessage. WebviewViews do have a WebviewViewResolveContext that the extension gets on resolve, but again you can't update it and have it serialized with the webview.

VS Code already serializes state to the webviews automatically, but we can't tap into it.

It would be very helpful to be able to pass in intial/bootstrap state when creating a webview (pane or view), and could remove a lot of complexity with getting initial state into a webview

Here are a couple of ideas of a possible API:

  • Add a new resolveState callback function to WebviewOptions since that is common to both WebviewPanels and WebviewViews

    export interface WebviewOptions {
       ...
       readonly resolveState?<T>(savedState: T): Thenable<T>;
    }
    • VS Code would then call that function (if provided) during construction of the webview providing the persisted state in savedState and then pass along the updated state
  • Alternatively,

    • for WebviewPanels:
      • Add a resolveState callback parameter on createWebviewPanel
    • for WebviewViews:
      • Add a setState function provided to the resolveWebviewView callback
      • Or, a setState method to the WebviewViewResolveContext object provided in resolveWebviewView
  • Finally, there could be a getState/setState pair on the Webview itself, but that would "require" (or at least seemingly so) the state to be dynamically updateable -- which isn't really in scope with what I'm asking here.

/cc @mjbvz

Metadata

Metadata

Assignees

Labels

apifeature-requestRequest for new features or functionalitywebviewWebview issues

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions