From a66f33a0c3e20bac2f084d1b037f0282d47c143e Mon Sep 17 00:00:00 2001 From: ice-ajax <190340101+ice-ajax@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:06:31 +0100 Subject: [PATCH] feat: unsubscribe to relay upon notice message, expose closed message, add optional send close message to unsubscribe --- lib/nostr_dart.dart | 1 + lib/src/helpers/request_events.dart | 14 ++++++-------- lib/src/relay.dart | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/nostr_dart.dart b/lib/nostr_dart.dart index 43628a6..f5c9967 100644 --- a/lib/nostr_dart.dart +++ b/lib/nostr_dart.dart @@ -10,6 +10,7 @@ export 'src/helpers/collect_stored_events.dart'; export 'src/helpers/request_events.dart'; export 'src/logging.dart' show NostrLogLevel; export 'src/model/close_message.dart'; +export 'src/model/closed_message.dart'; export 'src/model/eose_message.dart'; export 'src/model/event_message.dart'; export 'src/model/notice_message.dart'; diff --git a/lib/src/helpers/request_events.dart b/lib/src/helpers/request_events.dart index fdf0ca8..02f4af7 100644 --- a/lib/src/helpers/request_events.dart +++ b/lib/src/helpers/request_events.dart @@ -1,27 +1,25 @@ import 'dart:async'; import 'package:nostr_dart/nostr_dart.dart'; -import 'package:nostr_dart/src/model/closed_message.dart'; -/// Requests stored [EventMessage]s from the provided [NostrRelay] using [RequestMessage]. +/// Requests stored [RelayMessage]s from the provided [NostrRelay] using [RequestMessage]. /// /// Stored events are those controlled by the `limit` property of a filter /// and are returned before the [EoseMessage]. /// /// Upon receiving the stored events, the subscription is closed. If you -/// need to keep the subscription to receive real-time [EventMessage]s, consider +/// need to keep the subscription to receive real-time [RelayMessage]s, consider /// using [collectStoredEvents] instead. -Stream requestEvents( +Stream requestEvents( RequestMessage requestMessage, NostrRelay relay, ) async* { final NostrSubscription subscription = relay.subscribe(requestMessage); await for (final message in subscription.messages) { - if (message is EventMessage) { - yield message; - } else if (message is EoseMessage || message is ClosedMessage) { - relay.unsubscribe(subscription.id); + yield message; + if (message is EoseMessage || message is ClosedMessage || message is NoticeMessage) { + relay.unsubscribe(subscription.id, sendCloseMessage: message is EoseMessage); } } } diff --git a/lib/src/relay.dart b/lib/src/relay.dart index fee4eb5..70eed90 100644 --- a/lib/src/relay.dart +++ b/lib/src/relay.dart @@ -116,13 +116,13 @@ class NostrRelay { } /// Closes the previously created [NostrSubscription] identified by the provided [subscriptionId]. - void unsubscribe(String subscriptionId) { + void unsubscribe(String subscriptionId, {bool sendCloseMessage = true}) { try { final NostrSubscription? subscription = _subscriptions[subscriptionId]; if (subscription == null) { throw SubscriptionNotFoundException(subscriptionId); } - sendMessage(CloseMessage(subscriptionId: subscriptionId)); + if (sendCloseMessage) sendMessage(CloseMessage(subscriptionId: subscriptionId)); subscription.dispose(); _subscriptions.remove(subscriptionId); _subscriptionsCountController.add(_subscriptions.keys.length);