requestRelays method

Future<NdkResponse> requestRelays(
  1. String name,
  2. Iterable<String> urls,
  3. Filter filter, {
  4. int timeout = DEFAULT_STREAM_IDLE_TIMEOUT,
  5. bool closeOnEOSE = true,
  6. dynamic onTimeout()?,
})

Implementation

Future<NdkResponse> requestRelays(
    String name, Iterable<String> urls, Filter filter,
    {int timeout = DEFAULT_STREAM_IDLE_TIMEOUT,
    bool closeOnEOSE = true,
    Function()? onTimeout}) async {
  String id = Helpers.getRandomString(10);
  RequestState state = RequestState(closeOnEOSE
      ? NdkRequest.query(id, name: name, filters: [filter])
      : NdkRequest.subscription(
          id,
          name: name,
          filters: [],
        ));

  for (var url in urls) {
    state.addRequest(url, RelaySet.sliceFilterAuthors(filter));
  }
  _globalState.inFlightRequests[state.id] = state;

  List<String> notSent = [];
  for (MapEntry<String, RelayRequestState> entry in state.requests.entries) {
    if (!doRelayRequest(state.id, entry.value)) {
      notSent.add(entry.key);
    }
  }
  for (var url in notSent) {
    state.requests.remove(url);
  }

  return NdkResponse(state.id, state.stream);
}