Skip to content

Commit

Permalink
feat: unsubscribe to relay upon notice message, expose closed message…
Browse files Browse the repository at this point in the history
…, add optional send close message to unsubscribe
  • Loading branch information
ice-ajax committed Dec 16, 2024
1 parent 4f8687c commit 3fb361c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/nostr_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
14 changes: 6 additions & 8 deletions lib/src/helpers/request_events.dart
Original file line number Diff line number Diff line change
@@ -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<EventMessage> requestEvents(
Stream<RelayMessage> 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);
}
}
}
4 changes: 2 additions & 2 deletions lib/src/relay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/helper_request_events_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
events.add(event);
}

expect(events.length, equals(2));
expect(events.length, equals(3));
});
});
}

0 comments on commit 3fb361c

Please sign in to comment.