Skip to content

Commit

Permalink
refactor: rename ConnectionState to DerivConnectionState (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahani-deriv authored Jan 23, 2024
1 parent f17d85d commit 0252a31
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library web_socket_handler;

export 'src/connection_controller/connection_state.dart'
show
ConnectionState,
DerivConnectionState,
ConnectingState,
ConnectedState,
ReconnectingState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'connection_state.dart';

/// Base class for connection controllers.
abstract class BaseConnectionController extends Stream<ConnectionState> {
abstract class BaseConnectionController extends Stream<DerivConnectionState> {
/// Gets the current connection state.
ConnectionState get state;
DerivConnectionState get state;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ class ConnectionController extends BaseConnectionController {
/// Initializes [ConnectionController].
ConnectionController()
: _state = const ConnectingState(),
_controller = StreamController<ConnectionState>.broadcast() {
_controller = StreamController<DerivConnectionState>.broadcast() {
_controller.add(_state);
}

ConnectionState _state;
final StreamController<ConnectionState> _controller;
DerivConnectionState _state;
final StreamController<DerivConnectionState> _controller;

@override
ConnectionState get state => _state;
DerivConnectionState get state => _state;

@override
StreamSubscription<ConnectionState> listen(
void Function(ConnectionState event)? onData, {
StreamSubscription<DerivConnectionState> listen(
void Function(DerivConnectionState event)? onData, {
Function? onError,
void Function()? onDone,
bool? cancelOnError,
Expand All @@ -32,13 +32,13 @@ class ConnectionController extends BaseConnectionController {
cancelOnError: cancelOnError,
);

Stream<ConnectionState> _getStream() async* {
Stream<DerivConnectionState> _getStream() async* {
yield _state;
yield* _controller.stream;
}

/// Sets the connection state to [state] and notifies listeners.
void add(ConnectionState state) {
void add(DerivConnectionState state) {
_state = state;
_controller.add(state);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import 'package:equatable/equatable.dart';

/// Base class for connection states.
abstract class ConnectionState with EquatableMixin {
/// Initializes [ConnectionState].
const ConnectionState();
abstract class DerivConnectionState with EquatableMixin {
/// Initializes [DerivConnectionState].
const DerivConnectionState();

@override
List<Object?> get props => <Object?>[];
}

/// Connectiong state, used when the connection is being established.
class ConnectingState extends ConnectionState {
class ConnectingState extends DerivConnectionState {
/// Initializes [ConnectingState].
const ConnectingState();
}

/// Connected state, used when the connection is established.
class ConnectedState extends ConnectionState {
class ConnectedState extends DerivConnectionState {
/// Initializes [ConnectedState].
const ConnectedState();
}

/// Reconnecting state, used when the connection is being established after a disconnection.
class ReconnectingState extends ConnectionState {
class ReconnectingState extends DerivConnectionState {
/// Initializes [ReconnectingState].
const ReconnectingState();
}

/// Reconnected state, used when the connection is established after a disconnection.
class ReconnectedState extends ConnectionState {
class ReconnectedState extends DerivConnectionState {
/// Initializes [ReconnectedState].
const ReconnectedState();
}

/// Disconnecting state, used when the connection is being closed.
class DisconnectingState extends ConnectionState {
class DisconnectingState extends DerivConnectionState {
/// Initializes [DisconnectingState].
const DisconnectingState();
}

/// Disconnected state, used when the connection is closed.
class DisconnectedState extends ConnectionState {
class DisconnectedState extends DerivConnectionState {
/// Initializes [DisconnectedState].
const DisconnectedState({
this.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class WebSocket {

/// Closes the connection.
Future<void> close([int? code, String? reason]) async {
final ConnectionState state = _connectionController.state;
final DerivConnectionState state = _connectionController.state;

if (state is DisconnectedState) {
return;
Expand Down Expand Up @@ -96,7 +96,7 @@ class WebSocket {
pingInterval: pingInterval,
).timeout(timeout);

final ConnectionState connectionState = _connectionController.state;
final DerivConnectionState connectionState = _connectionController.state;

if (connectionState is ReconnectingState) {
_connectionController.add(const ReconnectedState());
Expand Down Expand Up @@ -169,19 +169,19 @@ class WebSocket {
}

bool _isConnected() {
final ConnectionState state = _connectionController.state;
final DerivConnectionState state = _connectionController.state;

return state is ConnectedState || state is ReconnectedState;
}

bool _isReconnecting() {
final ConnectionState state = _connectionController.state;
final DerivConnectionState state = _connectionController.state;

return state is ReconnectingState;
}

bool _isDisconnecting() {
final ConnectionState state = _connectionController.state;
final DerivConnectionState state = _connectionController.state;

return state is DisconnectingState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void main() {

await expectLater(
socket.connection,
emitsInOrder(<ConnectionState>[
emitsInOrder(<DerivConnectionState>[
const ConnectingState(),
const ConnectedState(),
]),
Expand Down Expand Up @@ -98,14 +98,15 @@ void main() {
onConnection: (WebSocketChannel c) => channel = c,
);

final List<ConnectionState> connectionStates = <ConnectionState>[];
final List<DerivConnectionState> connectionStates =
<DerivConnectionState>[];

final WebSocket socket = WebSocket(Uri.parse('ws://localhost:$port'))
..connection.listen(connectionStates.add);

await expectLater(
socket.connection,
emitsInOrder(<ConnectionState>[
emitsInOrder(<DerivConnectionState>[
const ConnectingState(),
const ConnectedState(),
]),
Expand All @@ -118,7 +119,7 @@ void main() {

expect(
connectionStates,
equals(<ConnectionState>[
equals(<DerivConnectionState>[
const ConnectingState(),
const ConnectedState(),
const DisconnectedState(code: closeCode, reason: closeReason),
Expand All @@ -135,7 +136,7 @@ void main() {

await expectLater(
socket.connection,
emitsInOrder(<ConnectionState>[
emitsInOrder(<DerivConnectionState>[
const ReconnectingState(),
const ReconnectedState(),
]),
Expand All @@ -153,14 +154,15 @@ void main() {
() async {
server = await createWebSocketServer();

final List<ConnectionState> connectionStates = <ConnectionState>[];
final List<DerivConnectionState> connectionStates =
<DerivConnectionState>[];
final WebSocket socket = WebSocket(
Uri.parse('ws://localhost:${server!.port}'),
)..connection.listen(connectionStates.add);

await expectLater(
socket.connection,
emitsInOrder(<ConnectionState>[
emitsInOrder(<DerivConnectionState>[
const ConnectingState(),
const ConnectedState(),
]),
Expand All @@ -172,7 +174,7 @@ void main() {

await expectLater(
connectionStates,
equals(<ConnectionState>[
equals(<DerivConnectionState>[
const ConnectingState(),
const ConnectedState(),
const DisconnectingState(),
Expand Down Expand Up @@ -204,7 +206,7 @@ void main() {

await expectLater(
socket.connection,
emitsInOrder(<ConnectionState>[
emitsInOrder(<DerivConnectionState>[
const ConnectingState(),
const ConnectedState(),
]),
Expand Down Expand Up @@ -252,7 +254,7 @@ void main() {

await expectLater(
socket.connection,
emitsInOrder(<ConnectionState>[
emitsInOrder(<DerivConnectionState>[
const ConnectingState(),
const ConnectedState(),
]),
Expand Down Expand Up @@ -291,7 +293,7 @@ void main() {

await expectLater(
socket.connection,
emitsInOrder(<ConnectionState>[const DisconnectedState()]),
emitsInOrder(<DerivConnectionState>[const DisconnectedState()]),
);

expect(socket.connection.state, equals(const DisconnectedState()));
Expand Down

0 comments on commit 0252a31

Please sign in to comment.