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) {
  RequestState 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,
  )();

  /// 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;
    }

    // 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,
      );
    }

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

  asyncStuff();

  // Return the response immediately
  return response;
}