Issue with using comlink for ext host ↔︎ webview communication (postMessage missing "transfers" parameter) #2392
-
I'm trying to use the popular Comlink IPC abstraction to simplify my webview ↔︎ custom editor communication. import { expose } from "comlink";
// when setting up custom editor…
{
const myAPI = {
hello() { return "world"); }
};
expose(myAPI, extensionHostEndpoint(this.webviewPanel.webview));
}
```ts
// in webview
import { wrap } from "comlink";
const editorApi = wrap<EditorAPI>(webviewEndpont(vscode));
await editorApi(); // should resolve to "world"; It allows you to make abstractions for the (addList…/removeList…/postMess…) interface const webviewEndpont = (vscode) => {
addEventListener: window.addEventListener,
removeEventListener: window.removeEventListener,
postMessage(message, transfers) {
vscode.postMessage(message, transfers);
}
} On the Extension Host side, I use This all works great until I want to go the other way, sending a message from the extension host → webview via Unfortunately, Comlink likes to create additional // in webview
import { wrap, proxy } from "comlink";
const editorApi = wrap<EditorAPI>(webviewEndpont(vscode));
const myListener = () => console.log("hello");
// Fails with Uncaught DOMException: Failed to execute 'postMessage' on 'MessagePort': A MessagePort could not be cloned because it was not transferred.
await editorApi.addListener(proxy(myListener)); VSCode bugThis runs up against what appears to be a bug in vscode, where you can't pass a I made a minimal repro so you can see that issue
![]() Why is this broken? The support code in the rough call chain is
![]() Could this be intentional somehow? Either way the code looks like it intends to pass transfers but doesn't so maybe it could be clarified if this is supposed to work on not. perhaps related: microsoft/vscode#206884 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This sounds like a bug in VS Code. And I suggest to open a new issue in the vscode repo please https://github.com/microsoft/vscode/issues |
Beta Was this translation helpful? Give feedback.
This sounds like a bug in VS Code. And I suggest to open a new issue in the vscode repo please https://github.com/microsoft/vscode/issues