shutdown method
Ends every open subscription before closing the connection, so each of their requests gets the response the specification asks a server tearing a subscription down to send.
Implementation
@override
Future<void> shutdown() async {
if (_shutdown case final pending?) return pending.future;
final shutdown = _shutdown = Completer<void>();
// A later caller awaits this and sees the error; on the first call the
// error also travels up the `rethrow` below, so nothing has to listen.
shutdown.future.ignore();
try {
final open = _subscriptions.values.toList();
_subscriptions.clear();
for (final subscription in open) {
if (!subscription.isCompleted) subscription.complete();
}
await _cancellationSubscription?.cancel();
if (open.isNotEmpty && isActive) {
// `package:json_rpc_2` writes each response in a microtask once its
// handler returns, and drops it when the connection is already
// closed. An event loop turn runs every pending microtask, so the
// responses are out before `super.shutdown()` closes the connection.
await Future<void>.delayed(Duration.zero);
}
await super.shutdown();
shutdown.complete();
} catch (error, stackTrace) {
shutdown.completeError(error, stackTrace);
rethrow;
}
}