forked from deriv-com/flutter-deriv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_history.dart
85 lines (71 loc) · 2.33 KB
/
call_history.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import 'dart:async';
import 'package:flutter_deriv_api/services/connection/call_manager/call_history_entry.dart';
import 'package:flutter_deriv_api/services/interfaces/call_history_provider.dart';
/// Provides storage for messages sent/received via the web socket connection
class CallHistory implements CallHistoryProvider {
/// It initializes [CallHistory] instance.
CallHistory() {
_callHistoryBroadcaster = StreamController<NetworkPayload>.broadcast();
}
late final StreamController<NetworkPayload> _callHistoryBroadcaster;
/// Messages that were sent to the remote endpoint
final List<CallHistoryEntry> outgoing = <CallHistoryEntry>[];
/// Messages that were received from the remote endpoint
final List<CallHistoryEntry> incoming = <CallHistoryEntry>[];
int _limit = 1024;
/// Messages limit
int get limit => _limit;
set limit(int limit) {
_limit = limit;
_trimHistory(incoming);
_trimHistory(outgoing);
}
/// Record a message that was received from the remote endpoint
void pushIncoming({
required int timestamp,
required String method,
required Object message,
}) {
incoming.add(
CallHistoryEntry(timeStamp: timestamp, method: method, message: message),
);
if (!method.contains('ping')) {
_callHistoryBroadcaster.add(
NetworkPayload(
method: method,
body: message,
direction: 'RECEIVED',
timeStamp: timestamp),
);
}
_trimHistory(incoming);
}
/// Record a message being sent to the remote endpoint
void pushOutgoing({
required int timestamp,
required String method,
required Object message,
}) {
outgoing.add(
CallHistoryEntry(timeStamp: timestamp, method: method, message: message),
);
if (!method.contains('ping')) {
_callHistoryBroadcaster.add(
NetworkPayload(
method: method,
body: message,
direction: 'SENT',
timeStamp: timestamp),
);
}
_trimHistory(outgoing);
}
/// Trim early entries from [CallHistory] if we exceed the current limit
void _trimHistory(List<CallHistoryEntry> callHistory) {
if (callHistory.length > limit) {
callHistory.removeRange(0, callHistory.length - limit);
}
}
@override
Stream<NetworkPayload> get stream => _callHistoryBroadcaster.stream;
}