Thanks for the trick, I almost got it work. Here is my Java code:
public class MyWorker {
public static void main(String[] args) {
setOnMessageHandler((EventListener<MessageEvent>) MyWorker::onMessage);
}
@JSBody(params={"handler"}, script="self.onmessage=handler")
@Import(name = "setOnMessageHandler")
public static native void setOnMessageHandler(EventListener handler);
public static void onMessage(MessageEvent messageEvent) {
log("Received message with following data:");
log(messageEvent.getData());
}
@JSBody(params={"msg"}, script="console.log(msg)")
@Import(name = "logString")
private static native void log(String msg);
@JSBody(params={"js"}, script="console.log(js)")
@Import(name = "logJs")
private static native void log(JSObject js);
}
And then the caller code (in JavaScript here):
const worker = new Worker("classes.js");
worker.postMessage({ bar: "Foo" });
I said almost because to make it work, I had to patch classes.js by adding main() at the end the script so the main method is automatically called when loading the worker.
Any idea how to call the main method in a cleaner way?