Open corwinsheahan-wf opened 5 years ago
Hi @corwinsheahan-wf I have has the same problem, I am not sure what the official word on this is, yet I realized very quickly I did not actually want this to work with DDC. Let me explain, in my particular use case, the code I wanted to run on the web workers, was agnostic to web workers. The code is only dependent on a typed Stream & Stink for input and output.
I abstracted the coms and serialization (where needed) in a wrapper/decorator around the code that was going to run in the worker, the abstraction is almost like a container for the code the worker needs to execute. The upshot was a cleaner code and simplify testing. Code that runs in the web worker is tested and built independently of workers and can be hence the testing advantage. Implementing this for a couple or few workers which just pass around maps and always passes data back to the main thread is probably not worth the investment in time and energy.
@yuan-kuan wrote about this and I think there is a related dart package kick around about it. If you are still having problems, I am happy to help on StackOverflow(https://stackoverflow.com/questions/54264262/transfering-a-messageport-via-webworker-postmessage) where there is already an issue.
Any update on this? This really needs to work. I'm having to jump through hoops to abstract things out so it can work on DDC (without workers) and dart2js
(with workers).
Quick update on this (specifically with the example in the original comment): it does work if the worker code is compiled with dart2js. The main app can be either ddc or dart2js.
To get the worker itself running with ddc, we'd need some plumbing. When ddc compiles a Dart file to JS, it doesn't invoke main as it doesn't know what the entrypoint is. A wrapper (typically package:build_web_compilers) does this for UI code. There is no corresponding logic for workers.
If the ddc generated entry file is worker.js
, than a handwritten trampoline file worker.dart.js
:
importScripts('../dart/sdk/xcodebuild/ReleaseX64/dart-sdk/lib/dev_compiler/kernel/amd/require.js');
require({
paths: {
'dart_sdk': '../dart/sdk/xcodebuild/ReleaseX64/dart-sdk/lib/dev_compiler/kernel/amd/dart_sdk'
},
baseUrl: "./"
},
["require", "dart_sdk", "worker"],
function(require, dart_sdk, worker) {
worker.worker.main();
}
);
will produce code that loads and runs within a worker. The timing here is a bit different though since require
is asynchronous.
A better solution might involve concatenating and/or using a different module system.
fyi @sigmundch @jakemac53
I actually found an easy way (but not automated way) to run worker.
In a web project such as angulardart project, just call
dart2js <source file location from the top level> -o web/<destination file name>.dart.js
For instance, say the source file is under <project>lib/src/worker/worker.dart
dart2js lib/src/worker/worker.dart -o web/worker.dart.js
Then I can reference worker.dart.js from the main program (doesn't have to be in the main function.
I found this to be easier than relying on DDC until DDC fixes the problems with -O0.
@kevmoo Is there any solution to use dart isolates (dart:isolate) on the web now?
@insinfo – nope. When it was "supported" it was very broken.
Hi there!
It would appear that web workers don't work with DDC. We're following this example: https://github.com/dart-lang/sdk/issues/35730#issuecomment-457449337
Everything works as expected when compiled via dart2js. However, when compiled via DDC, there is no apparent communication between the main thread and the worker. Is this a known issue?
This is with
Dart 2.2.0