How do I create the code for a web worker using Scala.js? Looking at the documentation everything seems to produce JavaScript functions rather than top level JavaScript. The new Worker constructor seems to require top level Javascript, rather than a function within a file. So in my Scala.js code I have:
var myWorker = new Worker("worker.js");
myWorker.onmessage = (e: scala.scalajs.js.Any) =>
{
val e1 = e.asInstanceOf[MessageEvent]
println("Worker message recieved:" -- e1.data.toString)
}
myWorker.postMessage("")
And in the file worker.js I have
self.postMessage("from webworker");
The message is picked up and displayed in the web console as intended.