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 44bb3b3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 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
5 changes: 2 additions & 3 deletions lib/src/helpers/request_events.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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].
///
Expand All @@ -20,8 +19,8 @@ Stream<EventMessage> requestEvents(
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);
} else 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

0 comments on commit 44bb3b3

Please sign in to comment.