callAsync method

Future<Uint8List> callAsync(
  1. String name,
  2. Uint8List payload
)

Implementation

Future<Uint8List> callAsync(String name, Uint8List payload) async {
  final port = ReceivePort('${_libraryName}_port');
  final args = IsolateArguments(name, payload, port.sendPort);
  final completer = new Completer<Uint8List>();

  final isolate = await Isolate.spawn(
    callBridge,
    args,
    errorsAreFatal: false,
    debugName: '${_libraryName}_isolate',
    onError: port.sendPort,
  );

  port.listen(
    (message) async {
      if (message is Uint8List) {
        completer.complete(message);
      } else if (message is List) {
        completer.completeError(message.firstOrNull ?? "internal error");
      } else {
        completer.completeError(message ?? "spawn error");
      }
      port.close();
    },
    onDone: () {
      isolate.kill(priority: Isolate.beforeNextEvent);
    },
  );

  return completer.future;
}