send method

  1. @override
void send(
  1. dynamic message
)
override

Send a JSON-RPC message

Implementation

@override
void send(dynamic message) {
  if (_isClosed) return;

  // Check if this is the initialized notification
  if (message is Map &&
      message['method'] == 'notifications/initialized' &&
      !_getStreamActive) {
    _startGetStream();
  }

  // Lifecycle: nothing may overtake `notifications/initialized`, and
  // separate POSTs are not ordered — so later messages wait for it.
  final Future<void> sent;
  if (message is Map && message['method'] == 'notifications/initialized') {
    sent = _initializedSent = _sendRequest(message);
  } else {
    final gate = _initializedSent;
    sent = gate == null
        ? _sendRequest(message)
        : gate.catchError((Object _) {}).then((_) => _sendRequest(message));
  }
  sent.catchError((error) {
    // A long-lived stateless `subscriptions/listen` SSE POST can still be
    // unwinding when the transport is torn down (disconnect force-closes the
    // connection). Guard against reporting the resulting connection error onto
    // an already-closed controller.
    if (!_isClosed && !_messageController.isClosed) {
      _messageController.addError(error);
    }
  });
}