Skip to content

Implements a debounced notification listener in MeshCoreConnector#239

Merged
zjs81 merged 1 commit into
mainfrom
dev-notifyListener
Mar 6, 2026
Merged

Implements a debounced notification listener in MeshCoreConnector#239
zjs81 merged 1 commit into
mainfrom
dev-notifyListener

Conversation

@wel97459

Copy link
Copy Markdown
Collaborator

Implement debounced notification listener updates in MeshCoreConnector.
This help with grouping UI rebuilds.

Copilot AI review requested due to automatic review settings February 26, 2026 06:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a debounced notification listener mechanism in MeshCoreConnector to batch UI rebuilds and improve performance. The implementation adds a 50ms debounce to notifyListeners() calls, which helps group rapid state changes into single UI updates.

Changes:

  • Added debouncing infrastructure with a timer and dirty flag to batch notifyListeners() calls
  • Override notifyListeners() to use the debouncing mechanism instead of immediate notifications
  • Added proper cleanup of the debounce timer in dispose()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +3654 to +3664
void markNotifyDirty() {
if (_notifyListenersDirty && _notifyListenersTimer != null) {
return;
}

_notifyListenersDirty = true;
_notifyListenersTimer ??= Timer(
_notifyListenersDebounce,
_flushBatchedNotify,
);
}

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

The markNotifyDirty() method and the debouncing mechanism lack documentation explaining their purpose and behavior. Consider adding documentation to explain that this implements a 50ms debounce for notifyListeners() calls to group UI rebuilds, how the dirty flag and timer interact, and that notifications occurring during a flush will be batched into the next flush cycle. This will help future maintainers understand the intended behavior and avoid accidentally breaking the debouncing logic.

Copilot uses AI. Check for mistakes.
Comment on lines +3654 to +3681
void markNotifyDirty() {
if (_notifyListenersDirty && _notifyListenersTimer != null) {
return;
}

_notifyListenersDirty = true;
_notifyListenersTimer ??= Timer(
_notifyListenersDebounce,
_flushBatchedNotify,
);
}

void _flushBatchedNotify() {
_notifyListenersTimer = null;
if (!_notifyListenersDirty) {
return;
}

_notifyListenersDirty = false;
super.notifyListeners();

if (_notifyListenersDirty && _notifyListenersTimer == null) {
_notifyListenersTimer = Timer(
_notifyListenersDebounce,
_flushBatchedNotify,
);
}
}

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

The codebase already has a pattern for safely scheduling notifications using Flutter's SchedulerBinding, as seen in BleDebugLogService._notifyListenersSafely() at lib/services/ble_debug_log_service.dart:92-108. That approach uses SchedulerBinding.instance.addPostFrameCallback which is more framework-aware and ensures notifications happen at safe points in the Flutter frame lifecycle. Consider whether this Timer-based debouncing approach is appropriate for MeshCoreConnector, or whether a SchedulerBinding-based approach would be more consistent with existing patterns and potentially avoid the race conditions inherent in the current implementation.

Copilot uses AI. Check for mistakes.
@wel97459 wel97459 closed this Feb 26, 2026
@wel97459 wel97459 reopened this Feb 27, 2026
@zjs81

zjs81 commented Mar 3, 2026

Copy link
Copy Markdown
Owner

Wasn't this already a thing?

@wel97459

wel97459 commented Mar 3, 2026

Copy link
Copy Markdown
Collaborator Author

Not unless there is a system in place already that I didn't know about.

@zjs81 zjs81 merged commit 8eb6f32 into main Mar 6, 2026
16 checks passed
@wel97459 wel97459 deleted the dev-notifyListener branch March 7, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants