compressInQueue function Null safety

Future<ImageFile> compressInQueue(
  1. ImageFileConfiguration param
)

Compress image file input asynchronously

Implementation

Future<ImageFile> compressInQueue(ImageFileConfiguration param) async {
  final input = param.input;
  final config = param.config;
  final completer = Completer<SendPort>();
  final completerResult = Completer<ImageFile>();

  final response = ReceivePort();
  await Isolate.spawn(_compressQueue, response.sendPort);
  response.listen((message) {
    if (message is SendPort) {
      completer.complete(message);
      response.close();
    }
  });

  final sendPort = await completer.future;
  final answer = ReceivePort();
  sendPort.send([
    answer.sendPort,
    ImageFileConfiguration(input: input, config: config),
  ]);
  answer.listen((message) {
    if (message is ImageFile) {
      completerResult.complete(message);
      answer.close();
    }
  });

  return await completerResult.future;
}