user2302011
user2302011

Reputation: 11

Dart to javascript workers

I'm looking to cross compile a dart script to JS, running in a web workers.

My JS script should look like this:

myScript.js

postMessage("I\'m working before postMessage(\'ali\').");

onmessage = function (oEvent) {
  postMessage("Hi " + oEvent.data);
};

How to structure my dart code to do this ?

It's important to have this two functions directly at the top of my javascript, to be able to run in the workers. thx

Upvotes: 1

Views: 263

Answers (2)

user2302011
user2302011

Reputation: 11

I tried this :

dart_worker.dart

void main() {

  window.onMessage.listen( (event) => onData);

}

void onData(){
  var a = 10 + 25;
}

test-worker.html

<script>
        var a = new Worker('dart_worker.dart.js');
        a.postMessage('yeaaaah');
    </script>

Chrome report that i have a script error for my dart script : Uncaught Illegal argument(s): command

Is there something wrong with my code ?

Upvotes: 0

Justin Fagnani
Justin Fagnani

Reputation: 11201

I'm not sure anyone's tried this before. So you should!

dart.js, the script that starts the Dart VM if available and sets up js-interp, won't be of much use because a lot of it accesses objects that aren't available in workers, and because you don't need to start the VM.

So my guess is that all you need to do is compile code with dart2js and directly reference the app.dart.js in your worker call:

new Worker("dart_worker.dart.js");

Then in dart_worker.dart.js register for window.onMessage events and call window.postMessage normally.

Hopefully if you don't use disallowed DOM objects, like document, dart2js doesn't generate code that will fail in a worker.

Let us know if this works!

Upvotes: 1

Related Questions