Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debounce to ScreenshotWidget #2368

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements

- Cache parsed DSN ([#2365](https:/getsentry/sentry-dart/pull/2365))
- Add debounce to `ScreenshotWidget` ([#2368](https:/getsentry/sentry-dart/pull/2368))

## 8.10.0-beta.2

Expand Down
22 changes: 22 additions & 0 deletions flutter/lib/src/event_processor/screenshot_event_processor.dart
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@denrase I have already created a debouncer in utils/debouncer.dart, because I needed it for a different issue. I think it would make sense, to use this existing debouncer or adapt it if it doesn`t fit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to use it, but due to the callback nature it didn't fit with the event processors. Will take a look if it can be adapted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also they work a bit different, the first call always goes through in this PR, whereas the Debouncer would not call it. I also don't delay, but rather bail out on consecutive calls.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay I understand. Would it make sense, to extend the current debouncer with some parameters, to behave it that way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't feel that it is worth the effort, as it is different behaviour and we only use it in this one case.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class ScreenshotEventProcessor implements EventProcessor {
bool get _hasSentryScreenshotWidget =>
sentryScreenshotWidgetGlobalKey.currentContext != null;

final _debounceDuration = Duration(milliseconds: 100);

/// Only apply this event processor every [debounceDuration] duration.
DateTime? _lastApplyCall;

/// The debounce duration for applying this event processor.
Duration get debounceDuration => _debounceDuration;

@override
Future<SentryEvent?> apply(SentryEvent event, Hint hint) async {
if (event is SentryTransaction) {
Expand All @@ -30,6 +38,20 @@ class ScreenshotEventProcessor implements EventProcessor {
_hasSentryScreenshotWidget) {
return event;
}

// ignore: invalid_use_of_internal_member
final now = _options.clock();
final difference = _lastApplyCall?.difference(now).abs();
_lastApplyCall = now;

if (difference != null && difference < debounceDuration) {
_options.logger(
SentryLevel.warning,
'Skipping screenshot due to too many calls within a short time frame.',
);
return event; // Debounce
}

final beforeScreenshot = _options.beforeScreenshot;
if (beforeScreenshot != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,71 @@ void main() {
expect(beforeScreenshotHint, hint);
});
});

group("debounce", () {
testWidgets("limits added screenshots within debounce timeframe",
(tester) async {
// Run with real async https://stackoverflow.com/a/54021863
await tester.runAsync(() async {
final sut = fixture.getSut(FlutterRenderer.canvasKit, false);

await tester.pumpWidget(SentryScreenshotWidget(
child: Text('Catching Pokémon is a snap!',
textDirection: TextDirection.ltr)));

final throwable = Exception();

final firstEvent = SentryEvent(throwable: throwable);
final firstHint = Hint();

final secondEvent = SentryEvent(throwable: throwable);
final secondHint = Hint();

// ignore: invalid_use_of_internal_member
fixture.options.clock = () => DateTime.fromMillisecondsSinceEpoch(0);
await sut.apply(firstEvent, firstHint);

// ignore: invalid_use_of_internal_member
fixture.options.clock = () => DateTime.fromMillisecondsSinceEpoch(
sut.debounceDuration.inMilliseconds - 1);
await sut.apply(secondEvent, secondHint);

expect(firstHint.screenshot, isNotNull);
expect(secondHint.screenshot, isNull);
});
});

testWidgets("adds screenshots after debounce timeframe", (tester) async {
// Run with real async https://stackoverflow.com/a/54021863
await tester.runAsync(() async {
final sut = fixture.getSut(FlutterRenderer.canvasKit, false);

await tester.pumpWidget(SentryScreenshotWidget(
child: Text('Catching Pokémon is a snap!',
textDirection: TextDirection.ltr)));

final throwable = Exception();

final firstEvent = SentryEvent(throwable: throwable);
final firstHint = Hint();

final secondEvent = SentryEvent(throwable: throwable);
final secondHint = Hint();

// ignore: invalid_use_of_internal_member
fixture.options.clock = () => DateTime.fromMillisecondsSinceEpoch(0);
await sut.apply(firstEvent, firstHint);

// ignore: invalid_use_of_internal_member
fixture.options.clock = () => DateTime.fromMillisecondsSinceEpoch(
sut.debounceDuration.inMilliseconds);
await sut.apply(secondEvent, secondHint);

expect(firstHint.screenshot, isNotNull);
expect(secondHint.screenshot, isNotNull);
});
});
});
}

class Fixture {
Expand Down
Loading