Skip to content

Commit d0c6c01

Browse files
committed
improve timestamps
1 parent 2b514dd commit d0c6c01

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

flutter/lib/src/event_processor/screenshot_event_processor.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ class ScreenshotEventProcessor implements EventProcessor {
125125
}
126126

127127
@internal
128-
Future<Uint8List?> createScreenshot() => _recorder
129-
.capture((ScreenshotPng screenshot) => Future.value(screenshot.data));
128+
Future<Uint8List?> createScreenshot() =>
129+
_recorder.capture((screenshot) => Future.value(screenshot.data));
130130
}

flutter/lib/src/screenshot/recorder.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ class ScreenshotRecorder {
6060
/// To prevent accidental addition of await before that happens,
6161
///
6262
/// THIS FUNCTION MUST NOT BE ASYNC.
63-
Future<R> capture<R>(Future<R> Function(ScreenshotPng) callback) {
63+
Future<R> capture<R>(Future<R> Function(ScreenshotPng) callback,
64+
[Flow? flow]) {
6465
try {
65-
final flow = Flow.begin();
66+
flow ??= Flow.begin();
6667
Timeline.startSync('Sentry::captureScreenshot', flow: flow);
6768
final context = sentryScreenshotWidgetGlobalKey.currentContext;
6869
final renderObject =
@@ -213,7 +214,7 @@ class _Capture<R> {
213214
final imageData =
214215
await finalImage.toByteData(format: ImageByteFormat.png);
215216
final png = ScreenshotPng(finalImage.width, finalImage.height,
216-
imageData!.buffer.asUint8List(), timestamp);
217+
imageData!.buffer.asUint8List(), timestamp, flow);
217218
Timeline.finishSync(); // Sentry::screenshotToPng
218219

219220
Timeline.startSync('Sentry::screenshotCallback', flow: flow);
@@ -310,8 +311,10 @@ class ScreenshotPng {
310311
final int height;
311312
final Uint8List data;
312313
final DateTime timestamp;
314+
final Flow flow;
313315

314-
const ScreenshotPng(this.width, this.height, this.data, this.timestamp);
316+
const ScreenshotPng(
317+
this.width, this.height, this.data, this.timestamp, this.flow);
315318

316319
bool hasSameImageAs(ScreenshotPng other) {
317320
if (other.width != width || other.height != height) {

flutter/lib/src/screenshot/retrier.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,24 @@ class ScreenshotRetrier<R> {
5252
final prevScreenshot = _previousScreenshot;
5353
_previousScreenshot = screenshot;
5454
if (prevScreenshot != null && prevScreenshot.hasSameImageAs(screenshot)) {
55+
// Sucessfully captured a stable screenshot (repeated at least twice).
5556
_tries = 0;
56-
await _callback(screenshot);
57+
if (prevScreenshot.flow.id == screenshot.flow.id) {
58+
// If it's from the same (retry) flow, use the first screenshot timestamp.
59+
await _callback(prevScreenshot);
60+
} else {
61+
// Otherwise this was called from a scheduler (in a new flow) so use
62+
// the new timestamp.
63+
await _callback(screenshot);
64+
}
5765
} else if (_tries > _options.screenshotRetries) {
5866
throw Exception('Failed to capture a stable screenshot. '
5967
'Giving up after $_tries tries.');
6068
} else {
61-
ensureFrameAndAddCallback(capture);
69+
ensureFrameAndAddCallback((Duration sinceSchedulerEpoch) {
70+
_tries++;
71+
_recorder.capture(_onImageCaptured, screenshot.flow);
72+
});
6273
}
6374
}
6475
}

0 commit comments

Comments
 (0)