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
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 viapostMessage. WebviewViews do have aWebviewViewResolveContextthat 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
resolveStatecallback function toWebviewOptionssince that is common to bothWebviewPanels andWebviewViewssavedStateand then pass along the updated stateAlternatively,
WebviewPanels:resolveStatecallback parameter oncreateWebviewPanelWebviewViews:setStatefunction provided to theresolveWebviewViewcallbacksetStatemethod to theWebviewViewResolveContextobject provided inresolveWebviewViewFinally, there could be a
getState/setStatepair on theWebviewitself, 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