IsolateClient constructor

IsolateClient(
  1. String? clientId,
  2. SendPort serverSendPort
)

Implementation

IsolateClient(String? clientId, this.serverSendPort) {
  _clientId = clientId;
  receivePort.listen((data) {
    if (data is Map<String, Object?>) {
      var (status, id, requestId, result, errorMessage) =
          MessageHandler().decodeResponseMessage(data);

      if (requestId != null) {
        //var requestId = data['request_id'] as String?;
        var c = _requests.remove(requestId);

        if (c != null && !c.isCompleted) {
          //if (data['status'] is! bool) {
          //  c.completeError(
          //      FormatException('The server sent an invalid response.'));
          //} else if (!(data['status'] as bool)) {
          if (!status) {
            c.completeError(PubSubException(errorMessage ??
                'The server sent a failure response, but did not provide an error message.'));
          } else if (result is! Map) {
            c.completeError(FormatException(
                'The server sent a success response, but did not include a result.'));
          } else {
            c.complete(result);
          }
        }
      } else if (id != null && _id == null) {
        _id = id;

        for (var c in _onConnect) {
          if (!c.isCompleted) c.complete(_id);
        }

        _onConnect.clear();
      }
    } else if (data is List) {
      var eventName = data[0] as String;
      var event = data[1];
      for (var s in _subscriptions.where((s) => s.eventName == eventName)) {
        if (!s._stream.isClosed) s._stream.add(event);
      }
    }
  });
  serverSendPort.send(receivePort.sendPort);
}