Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

New sample demonstrating how to use Web Workers #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions demos/computingSquareRoots.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Workers : table </title>
</head>
<body>
<article>
<p><span id="log"></span></p>
</article>

<script>

/*
This sample shows ho to perform an operation (computing the square roots) on a table using Web Workers
Worker #i is assigned the task of finding the square roots of the numbers in line #i of the table.
A global object then recombines the results received from the workers into the result table.

Sample code contributed by : Francois Role [email protected]
*/





var log=document.querySelector('#log');



function logging(txt) { log.innerHTML+="<p>" + txt ; }




///////////////////////////////////////////////////////
// Dispatcher object
//////////////////////////////////////////////////////

function Dispatcher() {

logging("<p>Creating the Dispatcher") ;

this.availableArray=new Array();
this.workerArray=new Array();

this.nbWorkers=0;
this.resultTable=new Array();

this.handle=function(m) {
this.nbWorkers=m.length;
that=this;
for (var i =0 ; i < this.nbWorkers ; i++) {
this.workerArray[i]=new Worker('../js/computingSquareRoots.js');
this.availableArray[i]=true;
this.workerArray[i].onmessage = function(event) {
respObj=JSON.parse(event.data);
numLine= respObj.id;
line=respObj.line
that.resultTable[numLine]=line;
that.nbWorkers--;
if ( that.nbWorkers ==0 )
logging(formatTable(that.resultTable, "Final table"));
} ;



this.workerArray[i].postMessage(JSON.stringify( { id : i , line : m[i] } ) );
}

logging("<p>Number of created workers:" + this.workerArray.length) ;

}
} // end of constructor Dispatcher



///////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////


function formatTable(a,caption) {
var res ="";
res+="<table border=1><caption><b><u>" + caption + "</u></b></caption>";
for (var i in a) {
res+= "<tr>" + "<th>#" + i + "</th>";
for (var j in a[i] )
res+="<td align='center'>" + a[i][j] + "</td>" ;
res+= "</tr>" ;
}
res+= "</table>";
return res;
}


function randomLine(nbElems, borne) {
l=[];
for (var i=0 ; i < nbElems ; i++) {
l[i]=Math.floor( Math.random()*borne ) ;
}
return l;
}

a= [ randomLine(10, 66) , randomLine(10, 155) ,randomLine(10, 166) ,randomLine(10,28) ,randomLine(10, 89) , randomLine(10, 89) ]


logging(formatTable(a,"Initial table"));


d=new Dispatcher();

d.handle(a);


</script>

</body>
</html>
11 changes: 11 additions & 0 deletions js/computingSquareRoots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
onmessage=function(event) {
if ( event.data =="hello") {
postMessage("I'm here !");
}
else {
obj=JSON.parse(event.data);
rep={ id : obj.id , line : obj.line.map(Math.sqrt) }
postMessage(JSON.stringify(rep) );
close();
}
}