Skip to content

Commit

Permalink
Update connected and disconnected callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Jul 28, 2023
1 parent a1fa907 commit b7bdcf1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
34 changes: 29 additions & 5 deletions lib/src/client/centrifuge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion lib/src/subscription/client_subscription_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b7bdcf1

Please sign in to comment.