Skip to content

Commit e1f7c36

Browse files
authored
feat: add ability to emit/watch incoming and outgoing payload (#328)
1 parent dbe9e79 commit e1f7c36

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

lib/services/connection/call_manager/call_history.dart

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
import 'dart:async';
2+
13
import 'package:flutter_deriv_api/services/connection/call_manager/call_history_entry.dart';
4+
import 'package:flutter_deriv_api/services/interfaces/call_history_provider.dart';
25

36
/// Provides storage for messages sent/received via the web socket connection
4-
class CallHistory {
7+
class CallHistory implements CallHistoryProvider {
8+
/// It initializes [CallHistory] instance.
9+
CallHistory() {
10+
_callHistoryBroadcaster = StreamController<NetworkPayload>.broadcast();
11+
}
12+
13+
late final StreamController<NetworkPayload> _callHistoryBroadcaster;
14+
515
/// Messages that were sent to the remote endpoint
616
final List<CallHistoryEntry> outgoing = <CallHistoryEntry>[];
717

@@ -29,7 +39,15 @@ class CallHistory {
2939
incoming.add(
3040
CallHistoryEntry(timeStamp: timestamp, method: method, message: message),
3141
);
32-
42+
if (!method.contains('ping')) {
43+
_callHistoryBroadcaster.add(
44+
NetworkPayload(
45+
method: method,
46+
body: message,
47+
direction: NetworkDirections.received,
48+
timeStamp: timestamp),
49+
);
50+
}
3351
_trimHistory(incoming);
3452
}
3553

@@ -42,6 +60,15 @@ class CallHistory {
4260
outgoing.add(
4361
CallHistoryEntry(timeStamp: timestamp, method: method, message: message),
4462
);
63+
if (!method.contains('ping')) {
64+
_callHistoryBroadcaster.add(
65+
NetworkPayload(
66+
method: method,
67+
body: message,
68+
direction: NetworkDirections.sent,
69+
timeStamp: timestamp),
70+
);
71+
}
4572

4673
_trimHistory(outgoing);
4774
}
@@ -52,4 +79,7 @@ class CallHistory {
5279
callHistory.removeRange(0, callHistory.length - limit);
5380
}
5481
}
82+
83+
@override
84+
Stream<NetworkPayload> get stream => _callHistoryBroadcaster.stream;
5585
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// This interface provides stream of network payload that is going out(SENT)
2+
/// and coming in(RECEIVED) to the application.
3+
abstract class CallHistoryProvider {
4+
/// Stream of network payload that is going out(SENT) and coming in(RECEIVED)
5+
Stream<NetworkPayload> get stream;
6+
}
7+
8+
/// Network payload that is going out and coming in from the web socket.
9+
class NetworkPayload {
10+
/// Initializes [NetworkPayload] instance.
11+
NetworkPayload({
12+
required this.method,
13+
required this.body,
14+
required this.direction,
15+
required this.timeStamp,
16+
});
17+
18+
/// name of the api.
19+
final String method;
20+
21+
/// content of the api.
22+
final Object body;
23+
24+
/// direction of the api i.e SENT or RECEIVED.
25+
final NetworkDirections direction;
26+
27+
/// time of the api.
28+
final int timeStamp;
29+
}
30+
31+
enum NetworkDirections {
32+
/// Going out from the web socket.
33+
sent,
34+
35+
/// Coming in from the web socket.
36+
received
37+
}

lib/state/connection/connection_cubit.dart

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ConnectionCubit extends Cubit<ConnectionState> {
4646

4747
final String _key = '${UniqueKey()}';
4848

49-
late final BaseAPI? _api;
49+
late final BaseAPI _api;
5050

5151
/// Enables debug mode.
5252
///
@@ -86,6 +86,9 @@ class ConnectionCubit extends Cubit<ConnectionState> {
8686
/// Stream subscription for connectivity.
8787
StreamSubscription<ConnectivityResult>? connectivitySubscription;
8888

89+
/// Getter for [BaseAPI] implementation class. By default, it will be [BinaryAPI].
90+
BaseAPI get api => _api;
91+
8992
/// Reconnect to Websocket.
9093
Future<void> reconnect({
9194
ConnectionInformation? connectionInformation,
@@ -109,7 +112,7 @@ class ConnectionCubit extends Cubit<ConnectionState> {
109112
emit(const ConnectionConnectingState());
110113

111114
try {
112-
await _api!.disconnect().timeout(_pingTimeout);
115+
await _api.disconnect().timeout(_pingTimeout);
113116
} on Exception catch (e) {
114117
dev.log('$runtimeType disconnect exception: $e', error: e);
115118

@@ -118,7 +121,7 @@ class ConnectionCubit extends Cubit<ConnectionState> {
118121
return;
119122
}
120123

121-
await _api!.connect(
124+
await _api.connect(
122125
_connectionInformation,
123126
printResponse: enableDebug && printResponse,
124127
onOpen: (String key) {

0 commit comments

Comments
 (0)