diff --git a/CHANGELOG.md b/CHANGELOG.md index d3db03687a..486261d9ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,9 @@ ); ``` +- Add `screenshotCacheDuration` to `FlutterOptions` ([#2387](https://github.com/getsentry/sentry-dart/pull/2387)) + - Screenshots will be cached for 100ms per default. + ### Enhancements - Avoid sending too many empty client reports when Http Transport is used ([#2380](https://github.com/getsentry/sentry-dart/pull/2380)) diff --git a/flutter/lib/src/event_processor/screenshot_event_processor.dart b/flutter/lib/src/event_processor/screenshot_event_processor.dart index 912d92382d..42bbc4cdd8 100644 --- a/flutter/lib/src/event_processor/screenshot_event_processor.dart +++ b/flutter/lib/src/event_processor/screenshot_event_processor.dart @@ -14,6 +14,9 @@ import 'package:flutter/widgets.dart' as widget; class ScreenshotEventProcessor implements EventProcessor { final SentryFlutterOptions _options; + Uint8List? _cachedScreenshotData; + DateTime? _lastScreenshotTime; + ScreenshotEventProcessor(this._options); @override @@ -77,13 +80,26 @@ class ScreenshotEventProcessor implements EventProcessor { return event; } - final bytes = await createScreenshot(); + final bytes = await _getOrCreateScreenshot(); if (bytes != null) { hint.screenshot = SentryAttachment.fromScreenshotData(bytes); } return event; } + Future _getOrCreateScreenshot() async { + if (_options.screenshotCacheDuration.inMilliseconds > 0 && + _cachedScreenshotData != null && + _lastScreenshotTime != null && + DateTime.now().difference(_lastScreenshotTime!) < + _options.screenshotCacheDuration) { + return _cachedScreenshotData; + } + _cachedScreenshotData = await createScreenshot(); + _lastScreenshotTime = DateTime.now(); + return _cachedScreenshotData; + } + @internal Future createScreenshot() async { try { diff --git a/flutter/lib/src/sentry_flutter_options.dart b/flutter/lib/src/sentry_flutter_options.dart index e400aa3536..7bced05600 100644 --- a/flutter/lib/src/sentry_flutter_options.dart +++ b/flutter/lib/src/sentry_flutter_options.dart @@ -195,6 +195,10 @@ class SentryFlutterOptions extends SentryOptions { /// Only attach a screenshot when the app is resumed. bool attachScreenshotOnlyWhenResumed = false; + /// Screenshots will be cached for 100ms per default. Change this to change + /// the duration, or set to zero if you want to disable screenshot caching. + Duration screenshotCacheDuration = Duration(milliseconds: 100); + /// Sets a callback which is executed before capturing screenshots. Only /// relevant if `attachScreenshot` is set to true. When false is returned /// from the function, no screenshot will be attached.