threads.js
                                
                                 threads.js copied to clipboard
                                
                                    threads.js copied to clipboard
                            
                            
                            
                        Support for Shared Web Workers
Shared Web Workers are a relatively new feature that allow multiple scripts communicate with a single worker. A good use case would be for example sharing computation between multiple browser tabs.
When it comes to browser support, the feature is well supported apart from Safari. It used to have the feature for a while but then it was dropped. The background for the decision is explained at https://bugs.webkit.org/show_bug.cgi?id=149850 .
As we would have a direct use case for the feature in our current project, would it be possible for you to consider inclusion of the feature to threads.js? I am happy to contribute the code if we can agree on the steps. I imagine we would have to expose SharedWorker from threads.js itself and then also make sure the addition works with the webpack plugin. Safari might require some kind of a strategy as well.
Reference: https://github.com/GoogleChromeLabs/worker-plugin/pull/42 (shared worker support in Google's worker-plugin)
Hey @bebraw! Sure, I had my eye on shared workers for years, but didn't really consider it as a priority. Makes perfect sense, though.
Regarding the steps… I think you pretty much wrapped it up already. Just to have an overview:
- Update threads-plugin
- Export SharedWorkerfromthreads, so the threads-plugin can recognize it
- Likely requires small hacks similar to the exported normal Workerto make it work withts-node
If you can contribute something, I would be happy to review and release it 🙂
I set up #402 . It's good for an early look and I'll refine based on your feedback.
FYI: Safari added support again. I can already enable it on Mac with Safari 15.6 as an experimental feature.
Well, I gave it another go. See #458.
I'll admit, it's not as simple as one might have thought it would be. Not a complaint, I see why the code works like it does.
Thanks @bebraw for your attempt, I hopefully learned from it.
Well, see the pull request for some hints why adding SharedWorkers to threads.js is somewhat difficult.
But it was not for nothing. I made this in the meantime, only for WebWorkers (dedicated, shared, maybe also service). Similar to the threads.js api, but not quite the same. The pool is a little basic, but I'm sure it can be improved in the future:
https://github.com/852Kerfunkle/threads-es