Search code examples
dartweb-workerdart-isolates

Recent documentation about Dart Isolates


I recently started with Dart (www.dartlang.org) and really like it so far. A very promising feature are isolates, but I am not sure on how to start.

The documentation I found so far is from before a breaking change (BREAKING CHANGE: dart:isolate) in October 2013. The information in this "Breaking change" email is quite complicated and it looks like the new api is more complicated than the old.

I've got some questions:

  • Is the dart:isolate api stable?
  • Is there any up-to-date documentation?
  • Are there any working examples?

Solution

  • I tried this example and it works https://gist.github.com/olostan/7883315

    import "dart:isolate";
    
    void main() {
      print("Starting");
      var sPort = new ReceivePort();
      SendPort rPort;
      sPort.listen((msg) {
        if (msg is SendPort) {
          print("Host got port. sending back");
          rPort = msg;
          rPort.send("Hello!");
        }
        else print("Host got $msg");
        rPort.send(null);
        sPort.close();
      });
      Isolate.spawn(test,sPort.sendPort);
    }
    void test(sender) {
      var rPort = new ReceivePort();
      sender.send(rPort.sendPort);
      rPort.listen((msg){
        print("Worker got $msg");
        if (msg!=null)
          sender.send("I am worker");
        else rPort.close();
      });
    }
    

    Isolates seem not to be used too much yet so there may still be some bugs.
    The latest problems I remember reading about was debugging code running in isolates. I don't know if this is solved yet.
    It also depends if you want to use isolates on the server or in the browser.
    AFAIK it's more stable in the VM.