GenericWebWorker enables the execution of javascript WebWorkers without using external js files, this WebWorkers allow the user to:
- run javascript blocking code without stoping the main thread
- pass a generic function to be executed.
- pass functions to be executed in the WebWorker.
Visit Web Worker MDN for more information about WebWorkers
Its main features are:
The user does not have to create a /file.js:
- Dynamic code execution inside web worker.
- there is no need to request js files.
The code executed in the WebWorker have error catch.
If there is the need to execute a piece of code that will block the main thread for a long time, for example a huge for loop, math operations or javascript bitcoin minning, you can execute it in a GenericWebWOrker
Basic usage code:
This will create a WebWorker that will not use any external function o read external data, the code inside the callback execute as it
var worker = new GenericWebWorker()
worker.exec(function() {
//This will block the code for a long time, but it is not in the main thread, so the web page does not freeze.
var a = 0
for (var i = 0; i < 100000000000; i++) //blocking code
a += i
return a
})
.then(res => console.log(res)) //Getitng the result back in the main thread.
.catch(()=>{})
var gw = new GenericWebWorker({foo: 23, bar: "ii"}, test)
gw.exec((data, fun1)=>{
var a = 0
for (var i = 0; i < 100000000000; i++) //blocking code
a += i
console.log(data) //{foo: 23, bar: "ii"}
return fun1()
})
.then(data => console.log(data)) //print Hello there
.catch(e=> {})
function test () {
return "Hello there"
}
To create a GenericWebWorker you have three options
This will create a WebWorker that will not use any external function o read external data, the code inside the callback execute as it
var worker = new GenericWebWorker()
worker.exec(function() {
function mult (x, y) {
return x*y
}
var a = 123
var b = 90
var c = mult(a, b)
return c
})
.then(res => console.log(res)) //Print 11070
.catch(()=>{})
function fn_print() {
console.log(00098)
}
var w2 = new GenericWebWorker(1, 2, fn_print)
w2.exec(function (data1, data2, printer){
console.log(data1, data2) //1 2
printer() // 00098
return "bla"
}).then(d => console.log(d)) // bla
.catch(e=> console.log(e))
var foo = {
blockCpu: function (ms) { //Blocking CPU function
var now = new Date().getTime();
var result = 0
while(true) {
result += Math.random() * Math.random();
if (new Date().getTime() > now +ms)
return;
}
}
}
var data = {
bla: "asdasd",
ble: 12312312312,
blu: [12,32,12312]
}
var worker = new GenericWebWorker(data, foo.blockCpu)
worker.exec(function (data, blockCpu) {
blockCpu(20000) // Stop execution for 20s, main thread not affected
console.log(data) // {bla: "asdasd"...}
return "Anakin Skywalker"
})
.then(data => console.log(data)) // Anakin Skywalker
.catch(e => console.log(e))