requestNostrEvent method

NdkResponse requestNostrEvent(
  1. NdkRequest request
)

Performs a low-level Nostr event request

This method should be used only if the prebuilt use cases and query or subscription methods do not meet your needs

request The NdkRequest object containing request parameters

Returns an NdkResponse containing the request results

Implementation

NdkResponse requestNostrEvent(NdkRequest request) {
  final state = RequestState(request);

  final response = NdkResponse(state.id, state.stream);

  final concurrency = ConcurrencyCheck(_globalState);

  // register event verification - removes invalid events from the stream
  final verifiedNetworkStream = VerifyEventStream(
    unverifiedStreamInput: state.networkController.stream,
    eventVerifier: _eventVerifier,
  )();

  /// register cache new responses
  _cacheWrite.saveNetworkResponse(
    writeToCache: request.cacheWrite,
    inputStream: verifiedNetworkStream,
  );

  // register listener
  StreamResponseCleaner(
    inputStreams: [
      verifiedNetworkStream,
      state.cacheController.stream,
    ],
    trackingSet: state.returnedIds,
    outController: state.controller,
    timeout: request.timeout,
    eventOutFilters: _eventOutFilters,
  )();

  /// avoids sending events to response stream before a listener could be attached
  Future<void> asyncStuff() async {
    /// concurrency check - check if request is inFlight
    final streamWasReplaced = request.cacheRead && concurrency.check(state);
    if (streamWasReplaced) {
      return;
    } else {
      // add to in flight
      _globalState.inFlightRequests[state.id] = state;
    }

    // caching should write to response stream and keep track on what is unresolved to send the split filters to the engine
    if (request.cacheRead) {
      await _cacheRead.resolveUnresolvedFilters(
        requestState: state,
        outController: state.cacheController,
      );
    } else {
      /// close cache controller if not used
      state.cacheController.close();
    }

    /// handle request
    _engine.handleRequest(state);
  }

  asyncStuff();

  // Return the response immediately
  return response;
}