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

web3 -- Web Worker environment still is not supported in 1.2.4 #3377

Closed
keirongulrajani opened this issue Feb 18, 2020 · 1 comment · Fixed by #3381
Closed

web3 -- Web Worker environment still is not supported in 1.2.4 #3377

keirongulrajani opened this issue Feb 18, 2020 · 1 comment · Fixed by #3381
Labels
1.x 1.0 related issues Bug Addressing a bug

Comments

@keirongulrajani
Copy link

Hi all,

1.2.4 (and/or its dependencies, e.g. any-promise) seems to have dependencies on "window" instead of "self" which prevents it working in a Web Worker context.

When I've tried importScripts in a Web Worker using web3.js instead of web3.min.js from the node module i can see the window is not defined error being thrown when register-shim is being loaded; this is a file inside any-promise, and the any-promise library is used by web3-core-promievent in the current version.

The loadImplementation() inside register-shim.js is attempting to grab window.Promise when there is no window:

/**
 * Browser specific loadImplementation.  Always uses `window.Promise`
 *
 * To register a custom implementation, must register with `Promise` option.
 */
function loadImplementation(){
  if(typeof window.Promise === 'undefined'){
    throw new Error("any-promise browser requires a polyfill or explicit registration"+
      " e.g: require('any-promise/register/bluebird')")
  }
  return {
    Promise: window.Promise,
    implementation: 'window.Promise'
  }
}

It is possible to work around this by setting window as self first before the importScripts statements so that it has the correct environment (effectively tricking any-promise under the hood while loading to treat the worker as window:

var window = self;
importScripts(/*path to web 3 goes here*/);

however, there is still an issue of passing/accessing the current provider details to the web worker environment, as there's no way to access this information from the browser instance of web3. It is also not ideal having to create an entirely new instance of web3 inside the web worker instead of using an existing one.

Related: #2211 #1774
Related: https://ethereum.stackexchange.com/questions/56405/integrating-web3-from-metamask-in-react

@nivida
Copy link
Contributor

nivida commented Feb 20, 2020

Thanks for opening this issue! We will fix this asap.

@nivida nivida added 1.x 1.0 related issues Bug Addressing a bug labels Feb 20, 2020
@ryanio ryanio mentioned this issue Apr 15, 2020
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants