Skip to content

Commit

Permalink
Merge pull request #33 from relaystr/feat-requests-close-sub
Browse files Browse the repository at this point in the history
close in requests usecase
  • Loading branch information
leo-lox authored Nov 6, 2024
2 parents a927250 + 42455f2 commit cee0003
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
6 changes: 2 additions & 4 deletions lib/domain_layer/usecases/engines/network_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ abstract class NetworkEngine {
Iterable<String>? specificRelays,
});

// todo:
// response obj?
// implement in both engines
// Future<void> handleCloseSubscription(String subId);
/// closes the nostr network subscription
Future<void> closeSubscription(String subId);
}
17 changes: 12 additions & 5 deletions lib/domain_layer/usecases/jit_engine.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';

import 'package:ndk/shared/nips/nip01/client_msg.dart';

import '../../shared/helpers/relay_helper.dart';
import '../../shared/logger/logger.dart';
import '../entities/broadcast_response.dart';
Expand Down Expand Up @@ -176,11 +178,16 @@ class JitEngine with Logger implements NetworkEngine {
return NdkBroadcastResponse(publishedEvent: nostrEvent);
}

// close a relay subscription, the relay connection will be kept open and closed automatically (garbage collected)
//todo: this could be moved to the request object
handleCloseSubscription(String id) async {
await seedRelaysConnected;
throw UnimplementedError();
/// close a relay subscription, the relay connection will be kept open and closed automatically (garbage collected)
@override
closeSubscription(String id) async {
//await seedRelaysConnected;
for (var relay in globalState.connectedRelays) {
if (relay.activeSubscriptions.containsKey(id)) {
await relay.closeSubscription(id);
relay.activeSubscriptions.remove(id);
}
}
}

static doesRelayCoverPubkey(
Expand Down
2 changes: 1 addition & 1 deletion lib/domain_layer/usecases/relay_jit_manager/relay_jit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class RelayJit extends Relay with Logger {
return;
}
activeSubscriptions.remove(id);
ClientMsg closeMsg = ClientMsg("CLOSE", id: id, filters: []);
ClientMsg closeMsg = ClientMsg("CLOSE", id: id);
await send(closeMsg);
}

Expand Down
5 changes: 5 additions & 0 deletions lib/domain_layer/usecases/relay_sets_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,9 @@ class RelaySetsEngine implements NetworkEngine {

return NdkBroadcastResponse(publishedEvent: nostrEvent);
}

@override
Future<void> closeSubscription(String subId) async {
_relayManager.closeSubscription(subId);
}
}
5 changes: 5 additions & 0 deletions lib/domain_layer/usecases/requests/requests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ class Requests {
));
}

/// Closes a Nostr network subscription
Future<void> closeSubscription(String subId) {
return _engine.closeSubscription(subId);
}

/// Performs a low-level Nostr event request
///
/// This method should be used only if the prebuilt use cases and
Expand Down

0 comments on commit cee0003

Please sign in to comment.