sendRequestWithId<T extends Result?> method
- @protected
inherited
Sends methodName and hands back the JSON-RPC ID it goes out under.
Relies on json_rpc_2 writing the request to its sink before sendRequest returns. Captures the ID before installing the subscription handle, deferring the transport write until that handle can receive acknowledgements. Throws StateError if capture fails.
Implementation
@protected
({RequestId id, Future<T> result, Future<void> sent})
sendRequestWithId<T extends Result?>(String methodName, {Request? request}) {
final captured = _CapturedRequest();
_capturedRequest = captured;
late final Future<Object?> result;
try {
result = _peer.sendRequest(methodName, request);
} finally {
_capturedRequest = null;
}
final id = captured.id;
final forward = captured.forward;
if (id == null || forward == null) {
result.ignore();
unawaited(shutdown());
throw StateError('Encoding "$methodName" recorded no JSON-RPC ID.');
}
final sent = Completer<void>();
sent.future.ignore();
scheduleMicrotask(() => sent.complete(Future.sync(forward)));
return (
id: id,
result: result.then(
(value) => (value as Map?)?.cast<String, Object?>() as T,
),
sent: sent.future,
);
}