Reputation: 539
I'm in the process of creating a web application, written in Dart, that requires the creation of independent paths of execution, also written in Dart. (Were we not working in the browser, I would call them "threads.)
Normally, I would utilize Dart's (very robust) isolates to perform this task, but I am restricted in that I need to be able to arbitrarily terminate any of these "threads" at any time, without their cooperation.
If anyone is curious, this is because untrusted, 3rd party code must be run in these "threads," and I need to be able to terminate them if they are not responding, as well as for other reasons.
As I understand, this is impossible using dart:isolate. (Right?)
Anyway, I have had success compiling some Dart code to Javascript with dart2js and executing it in a Worker
, but I don't know how I can properly invoke the postMessage function and respond to the onmessage event, like I would in JS. I messed around a little bit with the internals of dart2js and managed to tweak it (specifically the js_backend stuff) such that I am able to execute the postMessage
function from my code, but responding to the onmessage
event with a Dart handler is beyond my understanding of the workings of dart2js.
I looked into the js.dart library, but that works by creating script tags on the page, and that is obviously not possible in a worker.
I know this is a really weird use case, but does anyone have any idea how I would leverage the internals of dart2js to respond and reply to messages to the worker?
Upvotes: 4
Views: 668
Reputation: 42383
The Worker
class seems to support what you want.
postMessage
to send messages to itonMessage
to receive messagesterminate
to terminate itUpvotes: 3