Skip to content

refactor(core): Optimize and share network state flows#4320

Merged
jamesarich merged 2 commits into
mainfrom
fix/network-state
Jan 26, 2026
Merged

refactor(core): Optimize and share network state flows#4320
jamesarich merged 2 commits into
mainfrom
fix/network-state

Conversation

@jamesarich

Copy link
Copy Markdown
Collaborator

optimizes the NetworkRepository by converting networkAvailable and resolvedList from simple properties into shared, lazy-initialized flows.

By using shareIn with SharingStarted.WhileSubscribed, these flows now remain active for 5 seconds after the last subscriber is gone, preventing unnecessary re-subscriptions and resource churn. distinctUntilChanged is also added to networkAvailable to avoid emitting redundant state updates.

Key Changes:

  • NetworkRepository.kt:
    • Changed networkAvailable and resolvedList to be lazy properties.
    • Both flows now use shareIn to broadcast their state to multiple subscribers efficiently, with a WhileSubscribed strategy.
    • networkAvailable now uses distinctUntilChanged() to prevent duplicate emissions.
    • The flows are scoped to the processLifecycle, ensuring they align with the application's lifecycle.
  • RadioInterfaceService.kt:
    • Introduced a thread-safe, double-checked locking mechanism to ensure state listeners in initStateListeners() are initialized only once.

This commit optimizes the `NetworkRepository` by converting `networkAvailable` and `resolvedList` from simple properties into shared, lazy-initialized flows.

By using `shareIn` with `SharingStarted.WhileSubscribed`, these flows now remain active for 5 seconds after the last subscriber is gone, preventing unnecessary re-subscriptions and resource churn. `distinctUntilChanged` is also added to `networkAvailable` to avoid emitting redundant state updates.

### Key Changes:
- **`NetworkRepository.kt`**:
    - Changed `networkAvailable` and `resolvedList` to be `lazy` properties.
    - Both flows now use `shareIn` to broadcast their state to multiple subscribers efficiently, with a `WhileSubscribed` strategy.
    - `networkAvailable` now uses `distinctUntilChanged()` to prevent duplicate emissions.
    - The flows are scoped to the `processLifecycle`, ensuring they align with the application's lifecycle.
- **`RadioInterfaceService.kt`**:
    - Introduced a thread-safe, double-checked locking mechanism to ensure state listeners in `initStateListeners()` are initialized only once.

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
@github-actions github-actions Bot added the bugfix PR tag label Jan 26, 2026
@jamesarich jamesarich enabled auto-merge January 26, 2026 12:38
@codecov

codecov Bot commented Jan 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (a28aa4d) to head (868671c).
⚠️ Report is 7 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #4320   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files          3       3           
  Lines         28      28           
  Branches       8       8           
=====================================
  Misses        28      28           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jamesarich jamesarich added this pull request to the merge queue Jan 26, 2026
Merged via the queue into main with commit fc5255a Jan 26, 2026
11 checks passed
@jamesarich jamesarich deleted the fix/network-state branch January 26, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix PR tag

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant