Skip to content
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

Added worker environment #6108

Merged
merged 1 commit into from
Mar 5, 2024
Merged

Added worker environment #6108

merged 1 commit into from
Mar 5, 2024

Conversation

marklundin
Copy link
Member

When a user script is parsed in the editor, it's executed in a Worker which doesn't have window or global.

This means that when the following ESM Script is parsed in the editor, it will import the engine and immediately throw because neither window nor global exists.

import { ScriptType } from 'playcanvas';
class MyRotator extends ScriptType {}

This PR adds environment: 'worker' and global: self as additional values which prevents this error.

@marklundin marklundin self-assigned this Mar 5, 2024
@marklundin marklundin requested review from slimbuck, willeastcott and mvaligursky and removed request for slimbuck March 5, 2024 13:56
Copy link
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine. What's the globalThis?

@marklundin
Copy link
Member Author

globalThis is global which works across everything, but it's not available pre Chrome-71 era, so falls back to using environment. I considered a polyfill, but it's so small

@marklundin marklundin merged commit 7d56fe9 into main Mar 5, 2024
7 checks passed
@marklundin marklundin deleted the platform-worker-fix branch March 5, 2024 16:03
*
* @type {object}
*/
global: (environment === 'browser') ? window : global,
global: globalThis ??
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this throw on browsers that didn't support globalThis? You might need to do typeof globalThis !== undefined before attempting to reference it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're 100% right. Good spot. Will patch this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants