dart2js isolates?

322 views
Skip to first unread message

Alec Henninger

unread,
Jan 1, 2016, 5:50:15 PM1/1/16
to mi...@dartlang.org
Admittedly I have yet to try it out, but I'm curious if anyone here successfully uses additional isolates in their web app. I have been reading competing things about isolates WRT dart2js. Seth has said they compile to web workers, but https://www.dartlang.org/articles/event-loop/ says "Dart web apps can't currently create additional isolates, but they can create additional workers."

Do they currently fully map to workers in JS? If not, is it planned?

Thanks!

Alec

Günter Zöchbauer

unread,
Jan 2, 2016, 3:20:10 PM1/2/16
to Dart Misc
Curious about this as well. Never saw a statement that clearly says isolate should work in the browser, and how.

Andreas Reiter

unread,
Jan 4, 2016, 4:02:56 AM1/4/16
to mi...@dartlang.org
Isolate.spawn(...) does not work in the browser, but spawnUri does.

A nice example is provided here: http://stackoverflow.com/a/25104337/4370360

Regards,
Andreas

Günter Zöchbauer

unread,
Jan 4, 2016, 6:21:58 AM1/4/16
to Dart Misc
That loads a Dart script. I doubt this example works in Chrome after `pub build`. 
I guess there is a way to make it work (building the isolate source to JS as well and change the uri to *.js). 
I saw it mentioned that some use isolates in the browser, but no details how.
To me it looks like this is just experimental with no official support.

Edouard Tavinor

unread,
Sep 13, 2016, 6:21:59 PM9/13/16
to Dart Misc
Im interested in this too. Has anybody got it to work? Code that runs in Dartium bugs out when compiled to Javascript. Indeed, Isolate.spawnUri isn't even trying to fetch the file, as far as I can see.

Peter StJ

unread,
Sep 17, 2016, 8:36:45 AM9/17/16
to mi...@dartlang.org
I have successfully used isolates in dart and js from the same source code.

It works fine, the only limitation is that you have to have your url for worker in web dir because once built the urls for lib change, other than that it works as expected, use the same URL (for example 'worker.dart') and it is compiled to a separate js executable. I have not tried to call the worker unit from JS from main thread, I am not sure it this will work, but calling it from dart from main thread works.

Here is an example:



/// Initializes the worker (url is passes static - worker.dart).
 
Future _initWorker() async {
   
var uri = Uri.parse(url);
    _worker
= await Isolate.spawnUri(uri, [], _reciever.sendPort);
    _reciever
.listen(_handleData);
   
return null;
 
}


Interestingly the dart file for worker is not requested, instead the js file is fetched: 

Edouard Tavinor

unread,
Sep 19, 2016, 8:49:29 AM9/19/16
to Dart Misc
Hi!

Can you tell me what's in your pubspec.yaml and how you're compiling the code?

I'm compiling using dart2js without options and my pubspec.yaml is as follows:
name: 'isolatetest'
version: 0.0.1
description: An absolute bare-bones web app.
#author: Your Name <em...@example.com>

environment:
  sdk: '>=1.0.0 <2.0.0'

dependencies:
  browser: "^0.10.0"
  dart_to_js_script_rewriter: '^0.1.0'

transformers:
- dart_to_js_script_rewriter

Peter StJ

unread,
Sep 20, 2016, 7:09:36 AM9/20/16
to Dart Misc
Sure:


name
: swagger_tools
description
: Collection of instruments to work with REST API describing documents.
version
: 0.0.1
author
: PeterStJ <regardingscot@gmail.com>



environment
:
  sdk
: '>=1.0.0 <2.0.0'


dependencies
:

  semver
: "^0.2.1"


dev_dependencies
:
  test
: '>=0.12.0 <0.13.0'
  args
: '^0.13.4'
  browser
: '>=0.10.0 <0.11.0'
  dart_to_js_script_rewriter
: '^0.1.0'


# Include transformers for the web test to run
transformers
:
- dart_to_js_script_rewriter
- $dart2js:
    csp
: false
    sourceMaps
: true
   
checked: false
    minify
: true
    commandLineOptions
:
   
- --trust-type-annotations
   
- --trust-primitives


pub serve / pub build is used to serve/compile

Doug Reese

unread,
Oct 3, 2016, 8:36:57 AM10/3/16
to Dart Misc
I am curious about your swagger_tools package.  I don't see it on pub (yet). I have just started looking into Swagger with respect to Dart.

Doug

Peter StJ

unread,
Oct 4, 2016, 8:07:52 AM10/4/16
to Dart Misc
Its nothing special, it ingests swagger format API description and produces dart, closure compatible JS, angular services etc, similar to the code gen utility used in dart to produce code for discovery document format APIs. I used it as a demo project while teaching a short term course on dart to demonstrate capabilities like type system, analyzer, isolates, running the same code on server, on client and as CLI app etc. The tools is not ready for public consumption and this is why you are not finding it on pub. 
Reply all
Reply to author
Forward
0 new messages