|
1 | 1 | @JS() |
2 | 2 | library dart_webrtc; |
3 | 3 |
|
4 | | -import 'dart:collection'; |
| 4 | +import 'dart:convert'; |
5 | 5 |
|
6 | 6 | import 'package:js/js.dart'; |
7 | 7 |
|
8 | | -@JS('RTCStats') |
9 | | -abstract class RTCStats extends Object { |
10 | | - external dynamic get timestamp; |
11 | | - external String get type; |
12 | | - external String get id; |
| 8 | +class RTCStats { |
| 9 | + RTCStats({this.type, this.id, this.timestamp, this.values}); |
| 10 | + factory RTCStats.fromMap(Map<String, dynamic> map) => RTCStats( |
| 11 | + id: map['id'], |
| 12 | + type: map['type'], |
| 13 | + timestamp: map['timestamp'], |
| 14 | + values: map); |
| 15 | + String type; |
| 16 | + |
| 17 | + String id; |
| 18 | + |
| 19 | + dynamic timestamp; |
| 20 | + |
| 21 | + List<String> names() => values.entries.map((e) => e.key).toList(); |
| 22 | + |
| 23 | + Map<String, dynamic> values; |
| 24 | +} |
| 25 | + |
| 26 | +class RTCStatsReport { |
| 27 | + RTCStatsReport(this._jsStats) { |
| 28 | + _jsStats.forEach(allowInterop((dynamic value, String key, dynamic array) { |
| 29 | + _stats[key] = RTCStats.fromMap(statsToMap(value)); |
| 30 | + })); |
| 31 | + } |
| 32 | + final RTCStatsReportJs _jsStats; |
| 33 | + Map<String, RTCStats> _stats = {}; |
| 34 | + |
| 35 | + int get size => _jsStats.size; |
| 36 | + |
| 37 | + RTCStats operator [](String key) => _stats[key]; |
| 38 | + |
| 39 | + List<String> keys() => _stats.entries.map((e) => e.key).toList(); |
| 40 | + |
| 41 | + Map<String, RTCStats> get values => _stats; |
13 | 42 | } |
14 | 43 |
|
15 | 44 | @JS('RTCStatsReport') |
16 | | -abstract class RTCStatsReport extends ListBase<RTCStats> { |
| 45 | +abstract class RTCStatsReportJs { |
17 | 46 | external int get size; |
| 47 | + external void forEach( |
| 48 | + void Function(dynamic stats, String key, dynamic _) func); |
18 | 49 | } |
| 50 | + |
| 51 | +// Calls invoke JavaScript `JSON.stringify(obj)`. |
| 52 | +@JS('JSON.stringify') |
| 53 | +external String stringify(Object obj); |
| 54 | +final JsonDecoder decoder = JsonDecoder(); |
| 55 | + |
| 56 | +Map<String, dynamic> statsToMap(dynamic stats) => |
| 57 | + decoder.convert(stringify(stats)); |
0 commit comments