Web Worker Settings for Chrome

Web worker settings for chrome

You have to close all the windows of Chrome before opening it with --allow-file-access-from-files flag on.

To open it in Ubuntu simply write google-chrome --allow-file-access-from-files in your terminal.

Nested Web Worker in Chrome

The answer to my question is currently found in the Spawning Subworkers subsection of the MDN documentation on Using Web Workers

Workers may spawn more workers if they wish. So-called sub-workers must be hosted within the same origin as the parent page. Also, the URIs for subworkers are resolved relative to the parent worker's location rather than that of the owning page. This makes it easier for workers to keep track of where their dependencies are.

Ultimately, the problem that I experienced was rooted in the fact that when my minion worker attempted to spawn its peon, it attempted to make reference to a file that was relative to document root rather than from the context of the worker but nothing was output to the console indicating that the file did not exist.

To tie this simple example out:

Slavedriver

<script type="application/javascript">
var slave_driver = (
function(){
let minion = new Worker('/Features/Work/minion.js');
let crackTheWhip = ()=>
{
minion.postMessage(
JSON.stringify({SERVICE: "get"})
);
};
let take_the_credit = (message)=>
{
console.log("It's about time");
alert(message.data);
};
minion.onmessage = take_the_credit;
return {
GetJaniceHerCoffee: crackTheWhip
}
}
)();

slave_driver.GetJaniceHerCoffee();
</script>

Minion ./Features/Work/Work.Minion.js

(
function(){
var self = this;
let home = location;
let peon = new Worker('/peon.js');
let receiveWorkOrder = (message)=>
{
console.log("Delegating to a Peon");
peon.postMessage(
message.data
);
}
let take_the_credit = (message)=>
{
console.log("It's about time!");
console.log("Minion Taking the Credit");
let work = JSON.parse(message.data);
self.postMessage(JSON.stringify(work));
};

peon.onmessage = take_the_credit;
self.onmessage = receiveWorkOrder;
}
)();

Peon ./Features/Work/Work.Peon.js

(
function(){
var self = this;
let receiveWorkOrder = (message)=>
{
console.log("Peon surfing stackoverflow");
console.log("Peon surfing ebay for beanie babies, cuz they're coming back, yo");
console.log("...wth is this? *sigh*");

let work_result = {Value: "Coffee"};
self.postMessage(JSON.stringify(
work_result
));
};
self.onmessage = receiveWorkOrder;
})();

Notice the small change to the file reference made when the minion spawns the peon.
If you drop the "slavedriver" script into the body of an index.html file and the relevant modules into their identified locations, you should get an alert that displays {"Value":"Coffee"} when you open index.

Using web workers with chrome extension

To send data to a worker, simply add the following to the top of worker.js:

self.onmessage = function(event) {
// Do something with event.data
};

and use az_worker.postMessage(somestring); to send data to it.


But your code is unnecessarily complex. If your goal is "to speedup my extension", then you should not be using Web workers to make synchronous XHR, but use asynchronous XHR on the main thread. Web Workers are great for CPU-bound tasks, but don't provide any advantage for I/O tasks such as network requests.

See https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest for tutorials on using XMLHttpRequest.

Chrome can't load web worker

Chrome doesn't let you load web workers when running scripts from a local file.

Chrome developer tool how to profile webworkers?

In the Memory panel, you have to select which JavaScript VM you want to profile:

Sample Image



Related Topics



Leave a reply



Submit