startEventsSubscriptionAsync method
Future<List<NostrEvent> >
startEventsSubscriptionAsync({
- required NostrRequest request,
- required Duration timeout,
- void onEose(
- String relay,
- NostrRequestEoseCommand ease
- bool useConsistentSubscriptionIdBasedOnRequestData = false,
- bool shouldThrowErrorOnTimeoutWithoutEose = true,
- 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));
}