Skip to content

feat(WebClientService): improve performance#8038

Merged
ArgoZhang merged 6 commits into
mainfrom
feat-client
May 22, 2026
Merged

feat(WebClientService): improve performance#8038
ArgoZhang merged 6 commits into
mainfrom
feat-client

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented May 22, 2026

Link issues

fixes #8037

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 WebClientService client info retrieval to use direct JS interop return values instead of callback-based invocation and improve cancellation and timeout handling.

New Features:

  • Allow GetClientInfo to accept a CancellationToken to support cooperative cancellation when fetching client data.

Bug Fixes:

  • Remove reliance on JS-invokable callback and TaskCompletionSource to avoid race conditions and timeout issues when retrieving client info from the browser.

Enhancements:

  • Simplify WebClientService by eliminating DotNetObjectReference usage and disposing only the JS module resource.
  • Update client.js ping helper to return client info directly, aligning with the new WebClientService interaction pattern.
  • Tighten JSModule timeout handling by using a scoped CancellationTokenSource variable declaration style.

Tests:

  • Simplify WebClientService unit tests to use JSInterop return values instead of repeated SetData calls and polling loops.
  • Add coverage for GetClientInfo cancellation token behavior and adjust existing tests to the new interaction model for client info retrieval.
  • Remove now-obsolete ConnectionHub tests that depended on WebClientService.SetData.

Copilot AI review requested due to automatic review settings May 22, 2026 04:42
@bb-auto bb-auto Bot added the enhancement New feature or request label May 22, 2026
@bb-auto bb-auto Bot added this to the v10.6.0 milestone May 22, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 22, 2026

Reviewer's Guide

Refactors WebClientService client info retrieval to use a direct JS interop return value with cancellation support instead of a JSInvokable callback/TaskCompletionSource, simplifies resource disposal, updates the JS client module accordingly, and adapts unit tests to the new async pattern while tightening JSModule timeout token handling.

Sequence diagram for updated WebClientService client info retrieval

sequenceDiagram
    participant WebClientService
    participant JSRuntime
    participant JSModule
    participant ClientJs

    WebClientService->>JSRuntime: LoadModuleByName(client)
    JSRuntime-->>WebClientService: JSModule

    WebClientService->>JSModule: InvokeAsync_ClientInfo(ping, token, ip.axd)
    JSModule->>ClientJs: ping(ip.axd)
    ClientJs-->>JSModule: ClientInfo
    JSModule-->>WebClientService: ClientInfo

    WebClientService->>WebClientService: set _client and RequestUrl
    WebClientService->>WebClientService: GetClientInfo returns ClientInfo
Loading

File-Level Changes

Change Details Files
Refactor WebClientService to use direct JS interop return values with cancellation support instead of JSInvokable callbacks and TaskCompletionSource.
  • Change GetClientInfo to accept an optional CancellationToken and call JSModule.InvokeAsync("ping", token, "ip.axd") to get client data directly
  • Remove TaskCompletionSource and DotNetObjectReference fields and the JSInvokable SetData method, simplifying state and disposal logic
  • On successful JS call, update the cached ClientInfo and always refresh RequestUrl from the current NavigationManager Uri
  • Keep IP locator enrichment logic but rely on the directly returned ClientInfo instead of callback-populated data
src/BootstrapBlazor/Services/WebClientService.cs
Update JS client module to return client info instead of invoking a .NET callback.
  • Change ping(url, invoke, method) to ping(url) that awaits getClientInfo(url) and returns the data
  • Remove the DotNetObjectReference-based invokeMethodAsync call in favor of a simple return value
src/BootstrapBlazor/wwwroot/modules/client.js
Tighten timeout-related cancellation handling in JSModule helper.
  • Use a scoped using var when creating CancellationTokenSource for InvokeVoidAsync with timeout, maintaining behavior but simplifying disposal syntax
src/BootstrapBlazor/Utils/JSModule.cs
Align unit tests and ConnectionHub tests with the new direct-return WebClientService behavior and cancellation support.
  • Simplify WebClientService_Ok test to configure JSInterop to return a ClientInfo from ping and await GetClientInfo directly
  • Remove tests and code paths dependent on SetData, TaskCompletionSource, and timeout behavior; add a new GetClientInfo_CancellationToken_Ok test that verifies cancellation-token-based calls
  • Adjust GetClientInfo_Error test to simulate JS exceptions using Setup("ping") instead of SetupVoid and remove timeout-based assertions
  • Clean up ConnectionHub tests to no longer call WebClientService.SetData, instead relying on direct Callback invocations with ClientInfo
test/UnitTest/Services/WebClientServiceTest.cs
test/UnitTest/Services/ConnectionHubTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#8037 Improve the performance of WebClientService (particularly the GetClientInfo flow) without breaking its observable behavior.

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

Copy link
Copy Markdown
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 - I've left some high level feedback:

  • GetClientInfo still relies on mutable shared state (_client) as a cache while also doing async work; consider refactoring to use a local ClientInfo instance and only assign to the field once at the end to avoid stale or partially populated state in concurrent calls.
  • CancellationToken passed into GetClientInfo is treated the same as any other exception and effectively ignored after logging; if cancellation is expected to be meaningful, consider detecting OperationCanceledException and returning early or rethrowing instead of continuing with IP lookup and returning cached data.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- GetClientInfo still relies on mutable shared state (_client) as a cache while also doing async work; consider refactoring to use a local ClientInfo instance and only assign to the field once at the end to avoid stale or partially populated state in concurrent calls.
- CancellationToken passed into GetClientInfo is treated the same as any other exception and effectively ignored after logging; if cancellation is expected to be meaningful, consider detecting OperationCanceledException and returning early or rethrowing instead of continuing with IP lookup and returning cached data.

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ac1453f) to head (0a2b02c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8038   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          766       766           
  Lines        34157     34154    -3     
  Branches      4698      4696    -2     
=========================================
- Hits         34157     34154    -3     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 updates WebClientService to fetch ClientInfo by directly awaiting a JS module call that returns data, removing the previous callback/TaskCompletionSource-based flow to reduce overhead and simplify the interop path.

Changes:

  • Refactor WebClientService.GetClientInfo to call client.ping(url) via InvokeAsync<T> and return the result.
  • Update client.js so ping(url) returns the ClientInfo payload instead of calling back into .NET.
  • Simplify and stabilize unit tests by removing polling/Task.Run patterns and adding cancellation-token coverage.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/UnitTest/Services/WebClientServiceTest.cs Updates tests to mock ping returning ClientInfo; adds cancellation-token test; adjusts error-path test.
test/UnitTest/Services/ConnectionHubTest.cs Removes usage of removed WebClientService.SetData test helper pattern.
src/BootstrapBlazor/wwwroot/modules/client.js Changes ping to return the client info instead of invoking a .NET callback.
src/BootstrapBlazor/Utils/JSModule.cs Minor disposal syntax change for timeout CTS in InvokeVoidAsync.
src/BootstrapBlazor/Services/WebClientService.cs Refactors GetClientInfo to await JS return value; removes SetData JS callback pathway and interop reference.

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

Comment thread src/BootstrapBlazor/Services/WebClientService.cs Outdated
Comment thread src/BootstrapBlazor/Services/WebClientService.cs
Comment thread test/UnitTest/Services/WebClientServiceTest.cs
@ArgoZhang ArgoZhang merged commit 52abd8f into main May 22, 2026
4 checks passed
@ArgoZhang ArgoZhang deleted the feat-client branch May 22, 2026 05:08
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.

feat(WebClientService): improve performance

2 participants