-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use globalThis instead of window #28
base: main
Are you sure you want to change the base?
Conversation
Using window is not compatible with Web/Shared Workers (where window is undefined). Using globalThis is a better alternative, as it should work in all environments.
Hello @PVNT, Thank you for your PR! Before I proceed with merging, I want to ensure I fully understand how this change will benefit your use case. Beside there might be other areas in the code where similar updates are necessary. I'm aware that support for web workers isn't ideal at the moment: While we could improve this by avoiding reliance on a global variable and instead replacing the variables throughout the code, this approach would require significantly more effort than the current implementation. I'm having difficulty understanding how using |
Hi @garronej, you are correct that it is not enough... I was trying to replace my own internal plugin by an open source plugin, and yours appeared to fit my needs, except for workers support. The issue with this plugin is that there are two different syntaxes depending on whether we are in a worker ( In my current plugin, I ended up with the following to support workers (it's a bit convoluted, but it works well):
const worker = import.meta.env.DEV
? new SharedWorker(new URL("./instance", import.meta.url), { type: "module" })
: new SharedWorker(new URL("./instance", import.meta.url), { type: "classic" });
|
@pvcnt You're absolutely right about everything. I've realized that the WebWorker support offered doesn't work in dev mode. Since I haven't been using web workers in my recent projects, this issue slipped through the cracks—it's a classic case of not using your own tools. Fixing this isn't trivial, but I can work on this. Alternatively, I'm open to reviewing a PR, though I should mention that the code for this module is quite convoluted. If you're still interested in using it, would you submit a small PR to the starter repo? It would be helpful if you could set up a few examples involving web workers, along with any common gotchas. On my end, I'll work on finding a solution based on your examples. |
Using window is not compatible with Web/Shared Workers (where window is undefined). Using globalThis is a better alternative, as it should work in all environments.