-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Debounce lifecycle, network, and setMode signals #281
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
Open
tanderson-ld
wants to merge
3
commits into
ta/SDK-2187/connection-mode-and-resolution-flutter
Choose a base branch
from
ta/SDK-2333/state-change-debouncer
base: ta/SDK-2187/connection-mode-and-resolution-flutter
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
packages/common_client/lib/src/data_sources/fdv2/state_debounce_manager.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:launchdarkly_dart_common/launchdarkly_dart_common.dart' | ||
| show LDLogger; | ||
|
|
||
| import '../../fdv2_connection_mode.dart'; | ||
|
|
||
| /// Snapshot of the desired state accumulated within a debounce window. | ||
| /// | ||
| /// Each field is one of the axes that participate in debouncing per the | ||
| /// FDv2 connection-mode resolution spec: network availability, application | ||
| /// lifecycle, and the user-requested mode (when set via the public | ||
| /// `setMode` API). `identify` calls intentionally do not participate. | ||
| final class DebouncedState { | ||
| final bool networkAvailable; | ||
| final bool inForeground; | ||
| final FDv2ConnectionMode? requestedMode; | ||
|
|
||
| static const _unset = Object(); | ||
|
|
||
| const DebouncedState({ | ||
| required this.networkAvailable, | ||
| required this.inForeground, | ||
| required this.requestedMode, | ||
| }); | ||
|
|
||
| DebouncedState _copyWith({ | ||
| bool? networkAvailable, | ||
| bool? inForeground, | ||
| Object? requestedMode = _unset, | ||
| }) { | ||
| return DebouncedState( | ||
| networkAvailable: networkAvailable ?? this.networkAvailable, | ||
| inForeground: inForeground ?? this.inForeground, | ||
| requestedMode: identical(requestedMode, _unset) | ||
| ? this.requestedMode | ||
| : requestedMode as FDv2ConnectionMode?, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Callback fired when the debounce window closes with the final | ||
| /// accumulated [DebouncedState]. | ||
| typedef OnDebounceReconcile = void Function(DebouncedState state); | ||
|
|
||
| /// Factory that produces a one-shot timer used to schedule the debounce | ||
| /// fire. Exists primarily so tests can substitute a controllable | ||
| /// implementation (e.g. via `fake_async`). | ||
| typedef DebounceTimerFactory = Timer Function( | ||
| Duration duration, void Function() callback); | ||
|
|
||
| Timer _defaultTimerFactory(Duration d, void Function() cb) => Timer(d, cb); | ||
|
|
||
| /// Debounces network availability, lifecycle, and user-requested mode | ||
| /// signals into a single reconciliation callback. | ||
| /// | ||
| /// Each `setX` call updates the relevant component of the pending state | ||
| /// and resets the debounce timer. When the timer fires, [onReconcile] is | ||
| /// invoked with the final [DebouncedState]. Per-setter early-return | ||
| /// suppresses unchanged values; the consumer is responsible for deciding | ||
| /// whether the resolved state requires action. | ||
| /// | ||
| /// A [debounceWindow] of [Duration.zero] bypasses the timer entirely: | ||
| /// state changes fire [onReconcile] synchronously inside the setter that | ||
| /// produced them. With this configuration, [onReconcile] must not call | ||
| /// back into any [StateDebounceManager] setter on the same instance -- | ||
| /// doing so would recurse into [_scheduleOrFire] before the outer call | ||
| /// returns. Intended for tests and FDv1-style immediate-application paths. | ||
| /// | ||
| /// Exceptions thrown from [onReconcile] are caught and (when [logger] is | ||
| /// supplied) logged at error level. The [DebouncedState] that was about to | ||
| /// be delivered is retained as the new baseline -- subsequent setter calls | ||
| /// dedupe against it as if the reconcile had succeeded. | ||
| final class StateDebounceManager { | ||
| final Duration _debounceWindow; | ||
| final OnDebounceReconcile _onReconcile; | ||
| final DebounceTimerFactory _timerFactory; | ||
| final LDLogger? _logger; | ||
|
|
||
| DebouncedState _pending; | ||
| Timer? _timer; | ||
| bool _closed = false; | ||
|
|
||
| StateDebounceManager({ | ||
| required DebouncedState initialState, | ||
| required Duration debounceWindow, | ||
| required OnDebounceReconcile onReconcile, | ||
| DebounceTimerFactory? timerFactory, | ||
| LDLogger? logger, | ||
| }) : _pending = initialState, | ||
| _debounceWindow = debounceWindow, | ||
| _onReconcile = onReconcile, | ||
| _timerFactory = timerFactory ?? _defaultTimerFactory, | ||
| _logger = logger; | ||
|
|
||
| void setNetworkAvailable(bool available) { | ||
| if (_pending.networkAvailable == available) { | ||
| return; | ||
| } | ||
| _pending = _pending._copyWith(networkAvailable: available); | ||
| _scheduleOrFire(); | ||
| } | ||
|
|
||
| void setInForeground(bool inForeground) { | ||
| if (_pending.inForeground == inForeground) { | ||
| return; | ||
| } | ||
| _pending = _pending._copyWith(inForeground: inForeground); | ||
| _scheduleOrFire(); | ||
| } | ||
|
|
||
| void setRequestedMode(FDv2ConnectionMode? mode) { | ||
| if (_pending.requestedMode == mode) { | ||
| return; | ||
| } | ||
| _pending = _pending._copyWith(requestedMode: mode); | ||
| _scheduleOrFire(); | ||
| } | ||
|
|
||
| void close() { | ||
| _closed = true; | ||
| _timer?.cancel(); | ||
| _timer = null; | ||
| } | ||
|
|
||
| void _scheduleOrFire() { | ||
| if (_closed) { | ||
| return; | ||
| } | ||
| if (_debounceWindow == Duration.zero) { | ||
| _invokeReconcile(); | ||
| return; | ||
| } | ||
| _timer?.cancel(); | ||
| _timer = _timerFactory(_debounceWindow, _onTimer); | ||
| } | ||
|
|
||
| void _onTimer() { | ||
| _timer = null; | ||
| if (_closed) { | ||
| return; | ||
| } | ||
| _invokeReconcile(); | ||
| } | ||
|
|
||
| void _invokeReconcile() { | ||
| try { | ||
| _onReconcile(_pending); | ||
| } catch (error, stackTrace) { | ||
| _logger?.error( | ||
| 'State debounce reconcile callback threw: $error\n$stackTrace'); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,4 @@ dev_dependencies: | |
| test: ^1.24.3 | ||
| lints: ^3.0.0 | ||
| mocktail: ^1.0.1 | ||
| fake_async: ^1.3.1 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raw exception text in logs
Low Severity
The reconcile error handler logs the caught value with string interpolation (
$error), which can embed sensitive request URIs or other PII from exceptiontoString()output instead of a fixed, categorized message.Triggered by learned rule: Never expose raw exception toString() in logs or StatusEvent messages in data sources
Reviewed by Cursor Bugbot for commit 8edb1a6. Configure here.