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

Wrapped global breaks JS running in service worker. #28

Open
Pantura opened this issue Feb 10, 2022 · 0 comments
Open

Wrapped global breaks JS running in service worker. #28

Pantura opened this issue Feb 10, 2022 · 0 comments

Comments

@Pantura
Copy link

Pantura commented Feb 10, 2022

https://github.com/mbullington/node_preamble.dart/blob/master/lib/preamble.dart#L9-L11

Is there a reason why the global/window is wrapped in an object? 327ac2e introduced the change but I do not get the reason for encapsulation. Couldn't find an issue this was related to.

Webworker context won't allow calling for example self.location once going through Object.create. Edit: not only Webworker, you can paste Object.create(window).location to browser console to get the illegal invocation error as well.

I suggest with help of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#search_for_the_global_across_environments
to change this to (globalThis would wrap the first function but can that be support requirement?)

var getGlobal = function () {
  // webworker
  if (typeof self !== 'undefined') { return self; }
  // browser
  if (typeof window !== 'undefined') { return window; }
  // node
  if (typeof global !== 'undefined') { return global; }
  throw new Error('unable to locate global object');
};

var self = getGlobal();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant