spawnFile method

Future<void> spawnFile(
  1. File dangerFile,
  2. dynamic message,
  3. bool isDebug
)

Implementation

Future<void> spawnFile(File dangerFile, dynamic message, bool isDebug) async {
  final exitPort = ReceivePort();
  final errorPort = ReceivePort();

  final isolateExitCompleter = Completer();
  final isolateErrorCompleter = Completer();

  exitPort.listen((message) {
    isolateExitCompleter.complete();
  });

  errorPort.listen((message) {
    isolateErrorCompleter.completeError(message);
  });

  final tempDangerFile = _createTempDangerFile(dangerFile, isDebug);

  final currentIsolate = await Isolate.spawnUri(
      tempDangerFile.uri, [], message,
      automaticPackageResolution: true,
      paused: true,
      onExit: exitPort.sendPort);
  if (currentIsolate.pauseCapability != null) {
    currentIsolate.resume(currentIsolate.pauseCapability!);
  }
  return Future.any(
          [isolateExitCompleter.future, isolateErrorCompleter.future])
      .whenComplete(() {
    exitPort.close();
    errorPort.close();
    currentIsolate.kill(priority: Isolate.immediate);
    if (tempDangerFile.existsSync()) {
      tempDangerFile.deleteSync();
    }
  });
}