Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do VSCode webviews support Web Workers?

I'm in the middle of making a vscode extension, noticed some strange interactions when I tried to implement a Dedicated Worker to my extensions webview.

I'm already using the vscode API to pass messages around the extension, however when using a dedicated worker(in the webview) & passing messages around else where in the extension, the web worker somehow gets called.

If I let my web worker send a call back an infinite loop occurs and VSCode crashes. This leaves me to believe that VSCode is actually utilising web workers already? But shouldn't a new dedicated worker be spawned when using new Worker(), regardless of VSCode?

I tried implementing a shared worker, but didn't have any success (no response at all from the SharedWorker), which leaves me to believe that webviews don't support this? Am I right in saying that? Has anyone implemented this before?

I tried finding more information online and on their GitHub issues, but wasn't able to :/

TLDR: Do Webviews in VSCode support web workers?

like image 246
RockiRider Avatar asked Oct 29 '25 03:10

RockiRider


1 Answers

Yes, you can. I don't know about seperate files, but you can use URL.createObjectUrl() to create a worker. Here is some code that worked for me (inside the webview html).

const blob = new Blob(['self.postMessage("Message from worker")'])
const uri = URL.createObjectURL(blob)
new Worker(uri).addEventListener('message', e => {
  console.log(e)
})
like image 120
programmerRaj Avatar answered Oct 31 '25 02:10

programmerRaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!