
Solving importScripts () in Opera
Good day to all! Literally today, when using Web Workers, I encountered a problem in the importScripts () function, which is that Opera (using version 11.61) for some of its internal reasons, when re-creating the Worker object, refuses to execute the importScripts () function inside it (the problem arises only in opera, other browsers behave appropriately).
A small example:
Worker code (suppose we have a global variable test in some.js):
The above code on the first call will faithfully display the contents of the test variable to us, but a repeated call instead of the expected value will display an error that the test variable is not defined. For what exactly reasons importScripts is not executed repeatedly. Having struggled for several hours, I found a solution. It's simple, since the opera does not want to re-import scripts in the newly created object, we will not create a new object, we will create only one and make it global and in the future we will send everything through it. The previous code can be upgraded as follows:
at the same time, the onmessage handler should not be placed inside the condition, since if variables coming into the function are used inside it, a short circuit to the variables of the first call will be created, and subsequent ones will be ignored.
I hope that the bug with importScripts will be fixed soon, after all, the technology is still fresh and sometimes you feel like a beta tester and not a user ...
PS I hope the note will be useful to someone
A small example:
var str = "http://" + document.domain + "/classes/js/workers/worker.js";
var worker = new Worker(str);
worker.onerror = function(e)
{
alert([
'ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message].join(''));
}
worker.onmessage = function (obj)
{
alert(obj['data']);
}
worker.postMessage();
Worker code (suppose we have a global variable test in some.js):
onmessage = function ()
{
importScripts("/classes/js/some.js");
postMessage(test);
}
The above code on the first call will faithfully display the contents of the test variable to us, but a repeated call instead of the expected value will display an error that the test variable is not defined. For what exactly reasons importScripts is not executed repeatedly. Having struggled for several hours, I found a solution. It's simple, since the opera does not want to re-import scripts in the newly created object, we will not create a new object, we will create only one and make it global and in the future we will send everything through it. The previous code can be upgraded as follows:
if(!('worker' in window))
{
var str = "http://" + document.domain + "/classes/js/workers/worker.js";
worker = new Worker(str);
worker.onerror = function(e)
{
alert([
'ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message].join(''));
}
}
worker.onmessage = function (obj)
{
alert(obj['data']);
}
worker.postMessage();
at the same time, the onmessage handler should not be placed inside the condition, since if variables coming into the function are used inside it, a short circuit to the variables of the first call will be created, and subsequent ones will be ignored.
I hope that the bug with importScripts will be fixed soon, after all, the technology is still fresh and sometimes you feel like a beta tester and not a user ...
PS I hope the note will be useful to someone