From b7bdcf1d06ab6e897d7fd09c58f44bf4fbd1fa40 Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Fri, 28 Jul 2023 11:25:57 +0400 Subject: [PATCH] Update connected and disconnected callbacks --- lib/src/client/centrifuge.dart | 34 ++++++++++++++++--- .../client_subscription_manager.dart | 14 +++++++- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/src/client/centrifuge.dart b/lib/src/client/centrifuge.dart index a2b364c..bdfe449 100644 --- a/lib/src/client/centrifuge.dart +++ b/lib/src/client/centrifuge.dart @@ -66,12 +66,21 @@ abstract base class CentrifugeBase implements ICentrifuge { @mustCallSuper void _initCentrifuge() {} + /// Called when connection established. + /// Right before [CentrifugeState$Connected] state. + /// {@nodoc} + @protected + @mustCallSuper + void _onConnected(CentrifugeState$Connected state) { + logger.fine('Connection established'); + } + /// Called when connection lost. /// Right before [CentrifugeState$Disconnected] state. /// {@nodoc} @protected @mustCallSuper - void _onDisconnect() { + void _onDisconnected(CentrifugeState$Disconnected state) { logger.fine('Connection lost'); } @@ -107,8 +116,17 @@ base mixin CentrifugeStateMixin on CentrifugeBase { @nonVirtual void _onStateChange(CentrifugeState newState) { logger.info('State changed: ${_state.type} -> ${state.type}'); + switch (newState) { + case CentrifugeState$Disconnected state: + _onDisconnected(state); + case CentrifugeState$Connecting _: + break; + case CentrifugeState$Connected state: + _onConnected(state); + case CentrifugeState$Closed _: + break; + } _statesController.add(_state = newState); - if (newState is CentrifugeState$Disconnected) _onDisconnect(); } @protected @@ -289,9 +307,15 @@ base mixin CentrifugeClientSubscriptionMixin } @override - void _onDisconnect() { - super._onDisconnect(); - _clientSubscriptionManager.disconnectAll(); + void _onConnected(CentrifugeState$Connected state) { + super._onConnected(state); + _clientSubscriptionManager.subscribeAll(); + } + + @override + void _onDisconnected(CentrifugeState$Disconnected state) { + super._onDisconnected(state); + _clientSubscriptionManager.unsubscribeAll(); } @override diff --git a/lib/src/subscription/client_subscription_manager.dart b/lib/src/subscription/client_subscription_manager.dart index 65a961d..afcf4c4 100644 --- a/lib/src/subscription/client_subscription_manager.dart +++ b/lib/src/subscription/client_subscription_manager.dart @@ -90,10 +90,22 @@ final class ClientSubscriptionManager { _channelSubscriptions.remove(subscription.channel); } + /// Establish all subscriptions for the specific client. + /// {@nodoc} + void subscribeAll() { + for (final entry in _channelSubscriptions.values) { + try { + // TODO(plugfox): moveToSubscribing if subscribed now + } on Object { + /* ignore */ + } + } + } + /// Disconnect all subscriptions for the specific client /// from internal registry. /// {@nodoc} - void disconnectAll() { + void unsubscribeAll() { for (final entry in _channelSubscriptions.values) { try { // TODO(plugfox): moveToSubscribing if subscribed now