startEventsSubscriptionAsync method

  1. @override
Future<List<NostrEvent>> startEventsSubscriptionAsync({
  1. required NostrRequest request,
  2. required Duration timeout,
  3. void onEose(
    1. NostrRequestEoseCommand ease
    )?,
  4. bool useConsistentSubscriptionIdBasedOnRequestData = false,
  5. bool shouldThrowErrorOnTimeoutWithoutEose = true,
})
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(NostrRequestEoseCommand ease)? onEose,
  bool useConsistentSubscriptionIdBasedOnRequestData = false,
  bool shouldThrowErrorOnTimeoutWithoutEose = true,
}) {
  final completer = Completer<List<NostrEvent>>();

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

  final subId = subscription.subscriptionId;

  final events = <NostrEvent>[];

  _registerOnEoselCallBack(subId, (eose) {
    if (!completer.isCompleted) {
      subscription.close();
      completer.complete(events);
    }
  });

  subscription.stream.listen(events.add);

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

  return completer.future;
}