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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/core/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const detectPassiveEvents = () => {
};

const ua = (typeof navigator !== 'undefined') ? navigator.userAgent : '';
const environment = (typeof window !== 'undefined') ? 'browser' : 'node';
const environment = typeof window !== 'undefined' ? 'browser' :
typeof global !== 'undefined' ? 'node' : 'worker';

// detect platform
const platformName =
Expand Down Expand Up @@ -62,19 +63,22 @@ const platform = {
name: platformName,

/**
* String identifying the current runtime environment. Either 'browser' or 'node'.
* String identifying the current runtime environment. Either 'browser', 'node' or 'worker'.
*
* @type {'browser' | 'node'}
* @type {'browser' | 'node' | 'worker'}
*/
environment: environment,

/**
* The global object. This will be the window object when running in a browser and the global
* object when running in nodejs.
* object when running in nodejs and self when running in a worker.
*
* @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

(environment === 'browser' && window) ??
(environment === 'node' && global) ??
(environment === 'worker' && self),

/**
* Convenience boolean indicating whether we're running in the browser.
Expand Down