Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Jul 13, 2025

Link issues

fixes #6408

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Refactor DefaultNetworkMonitorService to persist and reuse the latest network state for late-registered callbacks, introduce a timeout for semaphore acquisition, and update the sample component to safely handle a nullable logger

New Features:

  • Cache and immediately replay the last network monitor state to new subscribers

Bug Fixes:

  • Make ConsoleLogger nullable and use a null-conditional log call in the NetworkMonitors sample to avoid null reference

Enhancements:

  • Add a 3000ms timeout to the initialization semaphore in DefaultNetworkMonitorService

@bb-auto bb-auto bot added the enhancement New feature or request label Jul 13, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jul 13, 2025

Reviewer's Guide

This refactor adds a reuse mechanism for the last known network state in DefaultNetworkMonitorService—caching incoming states, immediately invoking newly registered callbacks with the cached state, and introducing a semaphore timeout—while also updating the sample component for nullable logging.

Sequence diagram for RegisterStateChangedCallback with state reuse

sequenceDiagram
    participant Component
    participant DefaultNetowrkMonitorService
    participant Callback

    Component->>DefaultNetowrkMonitorService: RegisterStateChangedCallback(callback)
    DefaultNetowrkMonitorService->>DefaultNetowrkMonitorService: Add or update callback in _callbacks
    alt _state is not null
        DefaultNetowrkMonitorService->>Callback: Invoke callback(_state)
    end
    alt !_init
        DefaultNetowrkMonitorService->>DefaultNetowrkMonitorService: Wait on _semaphoreSlim (timeout 3000ms)
        DefaultNetowrkMonitorService->>DefaultNetowrkMonitorService: Set _init = true (if not already)
    end
Loading

Class diagram for DefaultNetowrkMonitorService state reuse refactor

classDiagram
    class DefaultNetowrkMonitorService {
        - ConcurrentDictionary<IComponent, Func<NetworkMonitorState, Task>> _callbacks
        - bool _init
        - SemaphoreSlim _semaphoreSlim
        - NetworkMonitorState? _state
        + DefaultNetowrkMonitorService(IJSRuntime jsRuntime)
        + Task<NetworkMonitorState> GetNetworkMonitorState(CancellationToken token)
        + Task RegisterStateChangedCallback(IComponent component, Func<NetworkMonitorState, Task> callback)
        + void UnregisterStateChangedCallback(IComponent component)
        + Task TriggerNetworkStateChanged(NetworkMonitorState state)
    }

    DefaultNetowrkMonitorService --> IJSRuntime
    DefaultNetowrkMonitorService --> IComponent
    DefaultNetowrkMonitorService --> NetworkMonitorState
    DefaultNetowrkMonitorService --> SemaphoreSlim
Loading

Class diagram for NetworkMonitors sample component logger update

classDiagram
    class NetworkMonitors {
        - ConsoleLogger? _logger
        + Task OnNetworkStateChanged(NetworkMonitorState state)
    }
    NetworkMonitors --> ConsoleLogger
    NetworkMonitors --> NetworkMonitorState
Loading

File-Level Changes

Change Details Files
Implement state caching and reuse in network monitor service
  • Added private _state field to store last NetworkMonitorState
  • Stored state in TriggerNetworkStateChanged before dispatching callbacks
  • Immediately invoked new callbacks with cached state in RegisterStateChangedCallback
src/BootstrapBlazor/Services/DefaultNetworkMonitorService.cs
Introduce timeout for initialization semaphore
  • Updated semaphore WaitAsync call to include a 3000ms timeout
src/BootstrapBlazor/Services/DefaultNetworkMonitorService.cs
Update sample component to handle nullable logger
  • Changed ConsoleLogger field to nullable
  • Used null-conditional operator when logging network state
src/BootstrapBlazor.Server/Components/Samples/NetworkMonitors.razor.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#6408 Add a reuse state function to INetworkMonitorService.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto bot added this to the 9.8.0 milestone Jul 13, 2025
@ArgoZhang ArgoZhang merged commit cd6c8fd into main Jul 13, 2025
3 of 4 checks passed
@ArgoZhang ArgoZhang deleted the doc-net branch July 13, 2025 09:54
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(INetworkMonitorService): add reuse state function

2 participants