sendCountEventToRelaysAsync method

  1. @override
Future<NostrCountResponse> sendCountEventToRelaysAsync(
  1. NostrCountEvent countEvent, {
  2. required Duration timeout,
  3. List<String>? relays,
})
override

Implementation

@override
Future<NostrCountResponse> sendCountEventToRelaysAsync(
  NostrCountEvent countEvent, {
  required Duration timeout,
  List<String>? relays,
}) async {
  await _registerNewRelays(relays ?? relaysList!);

  var isSomeOkTriggered = false;

  final completers = <Completer<NostrCountResponse>>[];

  _runFunctionOverRelationIteration((relay) {
    final relayUrl = relay.url;
    if (relays?.containsRelay(relayUrl) ?? true) {
      _registerOnCountCallBack(
        relay: relayUrl,
        subscriptionId: countEvent.subscriptionId,
        onCountResponse: (relay, countRes) {
          final completer = Completer<NostrCountResponse>();

          Future.delayed(timeout, () {
            if (!isSomeOkTriggered) {
              throw TimeoutException(
                'the count event with subscription id: ${countEvent.subscriptionId} has timed out after: ${timeout.inSeconds} seconds',
              );
            }
          });

          isSomeOkTriggered = true;
          completer.complete(countRes);
        },
      );

      final serialized = countEvent.serialized();
      relay.socket.sink.add(serialized);
      utils.log(
        'count event with subscription id: ${countEvent.subscriptionId} is sent to relay with url: $relayUrl',
      );
    }
  });

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