Answers for "Dart Isolate 2-Way Communication"

0

Dart Isolate 2-Way Communication

import 'dart:io'; // for exit();
import 'dart:async';
import 'dart:isolate';

Future<dynamic> initIsolate() async {
  Completer completer = Completer<SendPort>();
  var isolateToMainStream = ReceivePort();

  isolateToMainStream.listen((data) {
    if (data is SendPort) {
      var mainToIsolateStream = data;
      completer.complete(mainToIsolateStream);
    } else {
      print('[isolateToMainStream] $data');
    }
  });

  await Isolate.spawn(myIsolate, isolateToMainStream.sendPort);
  return completer.future;
}

void myIsolate(SendPort isolateToMainStream) {
  var mainToIsolateStream = ReceivePort();
  isolateToMainStream.send(mainToIsolateStream.sendPort);

  mainToIsolateStream.listen((data) {
    print('[mainToIsolateStream] $data');
    exit(0);
  });

  isolateToMainStream.send('This is from myIsolate()');
}

void main() async {
  var mainToIsolateStream = await initIsolate();
  mainToIsolateStream.send('This is from main()');
}
Posted by: Guest on August-08-2021

Code answers related to "Dart Isolate 2-Way Communication"

Code answers related to "Dart"

Browse Popular Code Answers by Language