Skip to content

Commit

Permalink
handle canceling of streamSubscriptions in websocket_client for Conne…
Browse files Browse the repository at this point in the history
…ctionState stream
  • Loading branch information
frnandu committed Dec 28, 2024
1 parent 6d808d4 commit d560d59
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import '../../data_sources/websocket_client.dart';
class WebSocketClientNostrTransport implements NostrTransport {
/// The underlying WebSocket data source.
final WebsocketDSClient _websocketDS;
late StreamSubscription<ConnectionState> _stateStreamSubscription;

/// Creates a new WebSocketNostrTransport instance.
///
/// [_websocketDS] is the WebSocket data source to be used for communication.
WebSocketClientNostrTransport(this._websocketDS, [Function? onReconnect]) {
Completer completer = Completer();
ready = completer.future;
_websocketDS.ws.connection.listen((state) {
_stateStreamSubscription = _websocketDS.ws.connection.listen((state) {
Logger.log.t("${_websocketDS.url} connection state changed to $state");
switch (state) {
case Connected() || Reconnected():
Expand All @@ -45,6 +46,7 @@ class WebSocketClientNostrTransport implements NostrTransport {
/// Returns a Future that completes when the connection has been closed.
@override
Future<void> close() async {
await _stateStreamSubscription.cancel();
return _websocketDS.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class RelayConnectivity<T> {
/// cancels stream subscription and closes relay transport
Future<void> close() async {
if (_streamSubscription != null) {
_streamSubscription!.cancel();
await _streamSubscription!.cancel();
}
if (relayTransport != null && relayTransport!.isOpen()) {
Logger.log.t("closing $url webSocket");
relayTransport!
await relayTransport!
.close()
.timeout(const Duration(seconds: 3), onTimeout: () {
Logger.log.w("timeout while trying to close socket $url");

Check warning on line 45 in packages/ndk/lib/domain_layer/entities/relay_connectivity.dart

View check run for this annotation

Codecov / codecov/patch

packages/ndk/lib/domain_layer/entities/relay_connectivity.dart#L45

Added line #L45 was not covered by tests
Expand Down

0 comments on commit d560d59

Please sign in to comment.