You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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 usingweb3.js
instead ofweb3.min.js
from the node module i can see thewindow is not defined
error being thrown whenregister-shim
is being loaded; this is a file insideany-promise
, and theany-promise
library is used byweb3-core-promievent
in the current version.The
loadImplementation()
insideregister-shim.js
is attempting to grabwindow.Promise
when there is nowindow
:It is possible to work around this by setting
window
asself
first before theimportScripts
statements so that it has the correct environment (effectively trickingany-promise
under the hood while loading to treat the worker aswindow
: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
The text was updated successfully, but these errors were encountered: