@@ -14,10 +14,18 @@ import '../sentry_flutter_measurement.dart';
14
14
class TimeToInitialDisplayTracker {
15
15
static final TimeToInitialDisplayTracker _instance =
16
16
TimeToInitialDisplayTracker ._internal ();
17
- factory TimeToInitialDisplayTracker () => _instance;
17
+
18
+ factory TimeToInitialDisplayTracker (
19
+ {FrameCallbackHandler ? frameCallbackHandler}) {
20
+ if (frameCallbackHandler != null ) {
21
+ _instance._frameCallbackHandler = frameCallbackHandler;
22
+ }
23
+ return _instance;
24
+ }
25
+
18
26
TimeToInitialDisplayTracker ._internal ();
19
27
20
- IFrameCallbackHandler frameCallbackHandler = DefaultFrameCallbackHandler ();
28
+ FrameCallbackHandler _frameCallbackHandler = DefaultFrameCallbackHandler ();
21
29
bool _isManual = false ;
22
30
Completer <DateTime >? _trackingCompleter;
23
31
DateTime ? _endTimestamp;
@@ -26,7 +34,8 @@ class TimeToInitialDisplayTracker {
26
34
@internal
27
35
DateTime ? get endTimestamp => _endTimestamp;
28
36
29
- Future <void > trackRegularRoute (ISentrySpan transaction, DateTime startTimestamp, String routeName) async {
37
+ Future <void > trackRegularRoute (ISentrySpan transaction,
38
+ DateTime startTimestamp, String routeName) async {
30
39
final endTimestamp = await determineEndTime ();
31
40
if (endTimestamp == null ) return ;
32
41
@@ -41,19 +50,20 @@ class TimeToInitialDisplayTracker {
41
50
ttidSpan.origin = SentryTraceOrigins .autoUiTimeToDisplay;
42
51
}
43
52
44
- // Reset after completion
45
- _isManual = false ;
46
-
47
53
final ttidMeasurement = SentryFlutterMeasurement .timeToInitialDisplay (
48
54
Duration (
49
55
milliseconds:
50
- endTimestamp.difference (startTimestamp).inMilliseconds));
56
+ endTimestamp.difference (startTimestamp).inMilliseconds));
51
57
transaction.setMeasurement (ttidMeasurement.name, ttidMeasurement.value,
52
58
unit: ttidMeasurement.unit);
53
- return ttidSpan.finish (endTimestamp: endTimestamp);
59
+ await ttidSpan.finish (endTimestamp: endTimestamp);
60
+
61
+ // We can clear the state after creating and finishing the ttid span has finished
62
+ clear ();
54
63
}
55
64
56
- Future <void > trackAppStart (ISentrySpan transaction, AppStartInfo appStartInfo, String routeName) async {
65
+ Future <void > trackAppStart (ISentrySpan transaction, AppStartInfo appStartInfo,
66
+ String routeName) async {
57
67
final ttidSpan = transaction.startChild (
58
68
SentrySpanOperations .uiTimeToInitialDisplay,
59
69
description: '$routeName initial display' ,
@@ -68,7 +78,8 @@ class TimeToInitialDisplayTracker {
68
78
final ttidMeasurement = SentryFlutterMeasurement .timeToInitialDisplay (
69
79
Duration (milliseconds: appStartInfo.measurement.value.toInt ()),
70
80
);
71
- transaction.setMeasurement (ttidMeasurement.name, ttidMeasurement.value, unit: ttidMeasurement.unit);
81
+ transaction.setMeasurement (ttidMeasurement.name, ttidMeasurement.value,
82
+ unit: ttidMeasurement.unit);
72
83
73
84
// Since app start measurement is immediate, finish the TTID span with the app start's end timestamp
74
85
await ttidSpan.finish (endTimestamp: appStartInfo.end);
@@ -80,9 +91,14 @@ class TimeToInitialDisplayTracker {
80
91
Future <DateTime >? determineEndTime () {
81
92
_trackingCompleter = Completer <DateTime >();
82
93
94
+ // If we already know it's manual we can return the future immediately
95
+ if (_isManual) {
96
+ return _trackingCompleter? .future;
97
+ }
98
+
83
99
// Schedules a check at the end of the frame to determine if the tracking
84
100
// should be completed immediately (approximation mode) or deferred (manual mode).
85
- frameCallbackHandler .addPostFrameCallback ((_) {
101
+ _frameCallbackHandler .addPostFrameCallback ((_) {
86
102
if (! _isManual) {
87
103
completeTracking ();
88
104
}
@@ -103,4 +119,11 @@ class TimeToInitialDisplayTracker {
103
119
_trackingCompleter? .complete (endTimestamp);
104
120
}
105
121
}
122
+
123
+ void clear () {
124
+ _isManual = false ;
125
+ _trackingCompleter = null ;
126
+ // We can't clear the ttid end time stamp here, because it might be needed
127
+ // in the [TimeToFullDisplayTracker] class
128
+ }
106
129
}
0 commit comments