sendCountEventToRelaysAsync method

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

Implementation

@override
Future<NostrCountResponse> sendCountEventToRelaysAsync(
  NostrCountEvent countEvent, {
  required Duration timeout,
}) {
  var isSomeOkTriggered = false;

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

  _runFunctionOverRelationIteration((relay) {
    _registerOnCountCallBack(
      relay: relay.url,
      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: ${relay.url}',
    );
  });

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