centrifugal / centrifuge-dart

Dart (Flutter) client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
https://pub.dartlang.org/packages/centrifuge
MIT License
102 stars 29 forks source link

Subscriptions should have a dispose method to close all EventSink #65

Closed Amir-P closed 1 year ago

Amir-P commented 1 year ago

I think Centrifuge.Subscription should have a dispose method to be called when it's removed from Centrifuge.Client to free up resources and close all EvenSink's.

FZambia commented 1 year ago

@Amir-P hello, thanks! Probably you can provide more information and example of what you mean? Do you mean closing various stream controllers Subscription object has internally on removing Subscription from Client's registry? Or sth else? Definitely, clear example would help me understand your idea much better.

Amir-P commented 1 year ago

Right now when we're removing a subscription from our client, subscription.unsubscribe is called and then it is removed from _subscriptions in ClientImpl. But Subscription object has various StreamController instances which is left open after subscription is removed. Hence if there is a listener on one of the streams exposed by Subscription class (e.g. publication), it will never get done event and get closed.

void main() async {
  final controller = StreamController();
  controller.stream.listen((_) {}, onDone: () => print('done'));
  print(controller.hasListener); // prints out true
  await controller.close(); // onDone gets called and 'done' will be printed
  print(controller.hasListener); // prints out false
}

Let me know if I couldn't explain clearly again or got something wrong. @FZambia

FZambia commented 1 year ago

@Amir-P could you look at #67 - do you mean this?

FZambia commented 1 year ago

Released in v0.9.2