ParentPortClient constructor

ParentPortClient()

Implementation

ParentPortClient() {
  final initCompleter = Completer<SendPort>.sync();
  sendPortFuture = initCompleter.future;
  sendPortFuture.then((value) {
    sendPort = value;
  });
  _receivePort.listen((message) {
    if (message is _InitMessage) {
      assert(!initCompleter.isCompleted);
      initCompleter.complete(message.port);
    } else if (message is _PortChannelResult) {
      final handler = handlers.remove(message.requestId);
      assert(handler != null);
      if (message.success) {
        handler!.complete(message.result);
      } else {
        handler!.completeError(message.error, message.stackTrace);
      }
    } else if (message == _closeMessage) {
      close();
    }
  }, onError: (e) {
    if (!initCompleter.isCompleted) {
      initCompleter.completeError(e);
    }

    close();
  }, onDone: () {
    if (!initCompleter.isCompleted) {
      initCompleter.completeError(ClosedException());
    }
    close();
  });
  _errorPort.listen((message) {
    final [error, stackTraceString] = message;
    final stackTrace = stackTraceString == null
        ? null
        : StackTrace.fromString(stackTraceString);
    if (!initCompleter.isCompleted) {
      initCompleter.completeError(error, stackTrace);
    }
    _close(IsolateError(cause: error, isolateDebugName: _isolateDebugName),
        stackTrace);
  });
}