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

chore: change screenshots debouncing approach to throttling #131

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Next

## 4.8.0

- chore: change screenshots debouncing approach to throttling ([#131](https://github.com/PostHog/posthog-flutter/pull/131))
- Added `throttleDelay` config and deprecated `debouncerDelay` config.

## 4.7.1

- chore: do not send repeated snapshots ([#126](https://github.com/PostHog/posthog-flutter/pull/126))
Expand Down
3 changes: 1 addition & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Future<void> main() async {
config.sessionReplay = true;
config.sessionReplayConfig.maskAllTexts = false;
config.sessionReplayConfig.maskAllImages = false;
config.sessionReplayConfig.debouncerDelay =
const Duration(milliseconds: 1000);
config.sessionReplayConfig.throttleDelay = const Duration(milliseconds: 1000);
config.flushAt = 1;
await Posthog().setup(config);

Expand Down
16 changes: 14 additions & 2 deletions lib/src/posthog_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,29 @@ class PostHogSessionReplayConfig {
/// Default: true.
var maskAllImages = true;

/// Note: Deprecated in favor of [throttleDelay] from v4.8.0.
Copy link
Member Author

Choose a reason for hiding this comment

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

Not removing this var to avoid compilation issues

/// The value assigned to this var will be forwarded to [throttleDelay]
///
/// Debouncer delay used to reduce the number of snapshots captured and reduce performance impact.
/// This is used for capturing the view as a screenshot.
/// The lower the number, the more snapshots will be captured but higher the performance impact.
/// Defaults to 1s.
var debouncerDelay = const Duration(seconds: 1);
set debouncerDelay(Duration debouncerDelay) {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
throttleDelay = debouncerDelay;
}

/// Debouncer delay used to reduce the number of snapshots captured and reduce performance impact.
/// This is used for capturing the view as a screenshot.
/// The lower the number, the more snapshots will be captured but higher the performance impact.
/// Experimental support.
/// Defaults to 1s.
var throttleDelay = const Duration(seconds: 1);

Map<String, dynamic> toMap() {
return {
'maskAllImages': maskAllImages,
'maskAllTexts': maskAllTexts,
'debouncerDelayMs': debouncerDelay.inMilliseconds,
'throttleDelayMs': throttleDelay.inMilliseconds,
};
}
}
27 changes: 19 additions & 8 deletions lib/src/posthog_widget_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class PostHogWidgetState extends State<PostHogWidget> {
ScreenshotCapturer? _screenshotCapturer;
NativeCommunicator? _nativeCommunicator;

Timer? _debounceTimer;
Duration _debounceDuration = const Duration(milliseconds: 1000);
Timer? _throttleTimer;
bool _isThrottling = false;
Duration _throttleDuration = const Duration(milliseconds: 1000);

@override
void initState() {
Expand All @@ -35,7 +36,7 @@ class PostHogWidgetState extends State<PostHogWidget> {
return;
}

_debounceDuration = config.sessionReplayConfig.debouncerDelay;
_throttleDuration = config.sessionReplayConfig.throttleDelay;

_screenshotCapturer = ScreenshotCapturer(config);
_nativeCommunicator = NativeCommunicator();
Expand All @@ -46,10 +47,20 @@ class PostHogWidgetState extends State<PostHogWidget> {

// This works as onRootViewsChangedListeners
void _onChangeDetected() {
_debounceTimer?.cancel();
if (_isThrottling) {
// If throttling is active, ignore this call
return;
}

// Start throttling
_isThrottling = true;

// Execute the snapshot generation
_generateSnapshot();

_debounceTimer = Timer(_debounceDuration, () {
_generateSnapshot();
// Reset throttling after the duration
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
_throttleTimer = Timer(_throttleDuration, () {
_isThrottling = false;
});
}

Expand Down Expand Up @@ -91,8 +102,8 @@ class PostHogWidgetState extends State<PostHogWidget> {

@override
void dispose() {
_debounceTimer?.cancel();
_debounceTimer = null;
_throttleTimer?.cancel();
_throttleTimer = null;
_changeDetector?.stop();
_changeDetector = null;
_screenshotCapturer = null;
Expand Down
Loading