Skip to content

Commit

Permalink
Adds functionality to emit hub connection state events once changed (#32
Browse files Browse the repository at this point in the history
)

* Adds functionality to emit hub connection state events once changed

* Update CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: svsk417 <svoskuhl@slashwhy.de>
Co-authored-by: Tobias Busch <tobi@minimalme.de>
  • Loading branch information
3 people committed Aug 30, 2023
1 parent 8c93dac commit 98a309c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.4.0]

* Emit events once the HubConnectionState changes

## [1.3.5]

* Upgrade packages and remove warnings
Expand Down
38 changes: 35 additions & 3 deletions lib/hub_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@ import 'dart:async';
import 'package:logging/logging.dart';
import 'package:tuple/tuple.dart';

import 'default_reconnect_policy.dart';
import 'errors.dart';
import 'handshake_protocol.dart';
import 'iconnection.dart';
import 'ihub_protocol.dart';
import 'iretry_policy.dart';
import 'utils.dart';
import 'default_reconnect_policy.dart';

const int DEFAULT_TIMEOUT_IN_MS = 30 * 1000;
const int DEFAULT_PING_INTERVAL_IN_MS = 15 * 1000;

/// internal class to wrap emitting events once the {@link HubConnectionState} changes.
class _HubConnectionStateMaintainer {
late StreamController<HubConnectionState> _hubConnectionStateStreamController;
late HubConnectionState _connectionState;

_HubConnectionStateMaintainer(HubConnectionState initialConnectionState) {
_hubConnectionStateStreamController =
StreamController<HubConnectionState>.broadcast();
_connectionState = initialConnectionState;
}

set hubConnectionState(HubConnectionState hubConnectionState) {
_connectionState = hubConnectionState;
_hubConnectionStateStreamController.add(_connectionState);
}

HubConnectionState get hubConnectionState => _connectionState;

Stream<HubConnectionState> get hubConnectionStateStream =>
_hubConnectionStateStreamController.stream;
}

/// Describes the current state of the {@link HubConnection} to the server.
enum HubConnectionState {
/// The hub connection is disconnected.
Expand Down Expand Up @@ -61,7 +83,12 @@ class HubConnection {
Completer? _handshakeCompleter;
Exception? _stopDuringStartError;

HubConnectionState? _connectionState;
late final _HubConnectionStateMaintainer _hubConnectionStateMaintainer;
HubConnectionState get _connectionState =>
_hubConnectionStateMaintainer.hubConnectionState;
set _connectionState(HubConnectionState hubConnectionState) {
_hubConnectionStateMaintainer.hubConnectionState = hubConnectionState;
}

// connectionStarted is tracked independently from connectionState, so we can check if the
// connection ever did successfully transition from connecting to connected before disconnecting.
Expand Down Expand Up @@ -93,6 +120,10 @@ class HubConnection {
/// Indicates the state of the {@link HubConnection} to the server.
HubConnectionState? get state => _connectionState;

/// Emits upon changes of the {@link HubConnectionState}.
Stream<HubConnectionState> get stateStream =>
_hubConnectionStateMaintainer.hubConnectionStateStream;

/// Represents the connection id of the {@link HubConnection} on the server. The connection id will be null when the connection is either
/// in the disconnected state or if the negotiation step was skipped.
String? get connectionId => _connection.connectionId ?? null;
Expand Down Expand Up @@ -144,7 +175,8 @@ class HubConnection {
_reconnectedCallbacks = [];
_invocationId = 0;
_receivedHandshakeResponse = false;
_connectionState = HubConnectionState.Disconnected;
_hubConnectionStateMaintainer =
_HubConnectionStateMaintainer(HubConnectionState.Disconnected);
_connectionStarted = false;

_cachedPingMessage = _protocol.writeMessage(PingMessage());
Expand Down

0 comments on commit 98a309c

Please sign in to comment.