closeSubscription method

Future<void> closeSubscription(
  1. String subId, {
  2. String debugLabel = "",
})

Closes a Nostr network subscription

Implementation

Future<void> closeSubscription(String subId, {String debugLabel = ""}) async {
  final state = _globalState.inFlightRequests[subId];

  if (state == null) {
    Logger.log.w(
      () =>
          "no relay urls found for subscription $subId, cannot close :: debug: $debugLabel",
    );
    return;
  }

  Iterable<RelayConnectivity> relays = _relayManager.connectedRelays
      .whereType<RelayConnectivity>()
      .where((relay) => state.requests.containsKey(relay.key));

  for (final relay in relays) {
    final request = state.requests[relay.key]!;
    // a request the relay ended itself, with a CLOSED or with the EOSE of a
    // query, is already closed on its side
    final endedOnRelay = request.receivedClosed ||
        (state.request.closeOnEOSE && request.receivedEOSE);
    if (endedOnRelay) {
      continue;
    }
    _relayManager.sendCloseToConnection(relay.key, subId);
  }

  await state.close();
  _globalState.inFlightRequests.remove(subId);
}