IsolateClient constructor

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

Implementation

IsolateClient(String? clientId, this.serverSendPort) {
  _clientId = clientId;
  receivePort.listen((data) {
    if (data is Map && data['request_id'] is String) {
      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)) {
          c.completeError(PubSubException(data['error_message']?.toString() ??
              'The server sent a failure response, but did not provide an error message.'));
        } else if (data['result'] is! Map) {
          c.completeError(FormatException(
              'The server sent a success response, but did not include a result.'));
        } else {
          c.complete(data['result'] as Map?);
        }
      }
    } else if (data is Map && data['id'] is String && _id == null) {
      _id = data['id'] as String?;

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

      _onConnect.clear();
    } else if (data is List && data.length == 2 && data[0] is String) {
      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);
}