call method

Future<Object?> call(
  1. String method, [
  2. Map<String, Object?> params = const {},
  3. Duration? timeout
])

Calls method with params and returns the result of the response.

Throws a JsonRpcException for error responses, a TimeoutException when no response arrives within timeout (or the client default), and a JsonRpcConnectionClosed when the connection closes first.

Implementation

Future<Object?> call(
  String method, [
  Map<String, Object?> params = const {},
  Duration? timeout,
]) {
  if (_closed) return Future.error(JsonRpcConnectionClosed(method));

  final String id = '${_nextId++}';
  final Completer<Object?> completer = Completer<Object?>();
  _pending[id] = (method: method, completer: completer);
  try {
    _socket.add(
      jsonEncode({
        'jsonrpc': '2.0',
        'id': id,
        'method': method,
        'params': params,
      }),
    );
  } on StateError {
    _pending.remove(id);
    return Future.error(JsonRpcConnectionClosed(method));
  }

  final Duration limit = timeout ?? this.timeout;
  return completer.future.timeout(
    limit,
    onTimeout: () {
      _pending.remove(id);
      throw TimeoutException('No response to $method', limit);
    },
  );
}