startEventsSubscriptionAsync method

  1. @override
Future<List<NostrEvent>> startEventsSubscriptionAsync({
  1. required NostrRequest request,
  2. required Duration timeout,
  3. void onEose(
    1. String relay,
    2. NostrRequestEoseCommand ease
    )?,
  4. bool useConsistentSubscriptionIdBasedOnRequestData = false,
  5. bool shouldThrowErrorOnTimeoutWithoutEose = true,
  6. List<String>? relays,
})
override

Retuens a Future of List<NostrEvent> that will be triggered when the onEose callback is triggered of the subscription created by your request.

Implementation

@override
Future<List<NostrEvent>> startEventsSubscriptionAsync({
  required NostrRequest request,
  required Duration timeout,
  void Function(String relay, NostrRequestEoseCommand ease)? onEose,
  bool useConsistentSubscriptionIdBasedOnRequestData = false,
  bool shouldThrowErrorOnTimeoutWithoutEose = true,
  List<String>? relays,
}) async {
  await _registerNewRelays(relays ?? relaysList!);

  final subscription = startEventsSubscription(
    request: request,
    onEose: onEose,
    useConsistentSubscriptionIdBasedOnRequestData:
        useConsistentSubscriptionIdBasedOnRequestData,
  );

  final subId = subscription.subscriptionId;

  var isSomeEoseTriggered = false;

  final events = <NostrEvent>[];

  final completers = <Completer<List<NostrEvent>>>[];

  _runFunctionOverRelationIteration((relay) {
    final relayUrl = relay.url;

    if (relays?.containsRelay(relayUrl) ?? true) {
      final completer = Completer<List<NostrEvent>>();
      completers.add(completer);

      _registerOnEoselCallBack(
        subscriptionId: subId,
        relay: relayUrl,
        onEose: (relay, eose) {
          if (!isSomeEoseTriggered) {
            // subscription.close();
            completer.complete(events);
            isSomeEoseTriggered = true;
          }
        },
      );
    }
  });

  subscription.stream.listen(events.add);

  Future.delayed(
    timeout,
    () {
      if (!isSomeEoseTriggered) {
        if (shouldThrowErrorOnTimeoutWithoutEose) {
          throw TimeoutException(
            'the subscription with id: $subId has timed out after: ${timeout.inSeconds} seconds',
          );
        } else {
          for (final completer in completers) {
            completer.complete(events);
          }
        }
      }
    },
  );

  return Future.any(completers.map((e) => e.future));
}