Skip to content

Conversation

@celadaris
Copy link
Collaborator

@celadaris celadaris commented Mar 6, 2025

After testing live from the pull request I did before, I could still see problems, less when using OnBlurAsync but it was more noticeable in OnValueChanged.

These changes address the core issue where the input text was being overwritten by stale data from older asynchronous operations. The dual approach (both C# and JavaScript checks) provides protection against race conditions. Even if one side fails, the other will catch it.

Link issues

fixes #5110
fixes #5574
[Please fill in the relevant Issue number after the # above, such as #42]
[请在上方 # 后面填写相关 Issue 编号,如 #42]

Summary By Copilot

This pull request includes several changes to the AutoComplete component in the BootstrapBlazor project, focusing on improving its rendering and behavior. The most important changes include updating the version, modifying the component structure, and introducing a new AutoCompleteItems class.

Version Update:

  • Updated the project version from 9.4.8 to 9.4.9-beta01 in BootstrapBlazor.csproj.

Component Structure Improvements:

  • Replaced the ul element with a RenderTemplate in AutoComplete.razor to improve rendering flexibility. [1] [2]
  • Introduced a RenderFragment named RenderDropdown to encapsulate dropdown rendering logic in AutoComplete.razor.

New Class Introduction:

  • Added a new AutoCompleteItems class to handle the rendering of autocomplete items, including methods for attaching the render handle and rendering content.

Behavioral Enhancements:

  • Added a _render flag and overridden the ShouldRender method in AutoComplete.razor.cs to control rendering behavior.
  • Refactored the TriggerFilter and TriggerChange methods to use the new rendering logic and _render flag in AutoComplete.razor.cs. [1] [2] [3]

Regression?

  • Yes
  • No

[If yes, specify the version the behavior has regressed from]
[是否影响老版本]

Risk

  • High
  • Medium
  • Low

[Justify the selection above]

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

Addresses an issue where the input text in the AutoComplete component was being overwritten by stale data from older asynchronous operations by adding checks in both C# and JavaScript to ensure that the component only processes the latest user input.

Bug Fixes:

  • Fixes an issue where the AutoComplete component could display stale data due to race conditions in asynchronous operations.

Enhancements:

  • Improves the AutoComplete component by adding a SpinLock to protect against race conditions when updating the current input value.
  • Enhances the JavaScript logic to verify that the input value is still current before triggering the filter, preventing the processing of outdated operations.

After testing live, i could still see problems, less when using OnBlurAsync but it was more noticeable in OnValueChanged.

These changes address the core issue where the input text was being overwritten by stale data from older asynchronous operations. The dual approach (both C# and JavaScript checks) provides protection against race conditions. Even if one side fails, the other will catch it.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 6, 2025

Reviewer's Guide by Sourcery

This pull request addresses a race condition in the AutoComplete component where stale data from older asynchronous operations could overwrite the input text. It introduces both C# and JavaScript checks to ensure that only the latest input value is used to update the component's state.

Sequence diagram for TriggerFilter with stale data protection

sequenceDiagram
    participant User
    participant InputElement
    participant AutoCompleteJs
    participant AutoCompleteCs

    User->>InputElement: Types in input
    InputElement->>AutoCompleteJs: Triggers Input.composition with value v
    AutoCompleteJs->>AutoCompleteJs: debounce(filterCallback(v))
    AutoCompleteJs->>InputElement: Stores v in data-lastValue
    alt Input value has not changed
        AutoCompleteJs->>AutoCompleteCs: invokeMethodAsync('TriggerFilter', v)
        AutoCompleteCs->>AutoCompleteCs: _spinLock.Enter()
        AutoCompleteCs->>AutoCompleteCs: _userCurrentInput = v
        AutoCompleteCs->>AutoCompleteCs: _spinLock.Exit()
        AutoCompleteCs->>AutoCompleteCs: OnCustomFilter(v) or Items.Where(s => s.StartsWith(v))
        AutoCompleteCs->>AutoCompleteCs: _spinLock.Enter()
        AutoCompleteCs->>AutoCompleteCs: latestInput = _userCurrentInput
        AutoCompleteCs->>AutoCompleteCs: _spinLock.Exit()
        alt latestInput == v
            AutoCompleteCs->>AutoCompleteCs: CurrentValue = v
            AutoCompleteCs->>AutoCompleteCs: ValueChanged.HasDelegate ? skip : StateHasChanged()
        else latestInput != v
            AutoCompleteCs-->>AutoCompleteCs: Discard stale data
        end
    else Input value has changed
        AutoCompleteJs-->>AutoCompleteJs: Discard stale operation
    end
Loading

Updated class diagram for AutoComplete component

classDiagram
    class AutoComplete {
        -string _userCurrentInput
        -SpinLock _spinLock
        +Task TriggerFilter(string val)
    }
    note for AutoComplete "Added _userCurrentInput and _spinLock for handling race conditions"
Loading

File-Level Changes

Change Details Files
Implements a mechanism to prevent the input text from being overwritten by stale data from older asynchronous operations in the AutoComplete component.
  • Introduces a _userCurrentInput field to track the current input value.
  • Uses a SpinLock to ensure thread-safe updates to _userCurrentInput.
  • Updates the TriggerFilter method to compare the current input value with the latest input value before updating the CurrentValue property.
  • Adds a JavaScript check in the filterCallback function to ensure that the input value is still the same before invoking the TriggerFilter method.
src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.cs
src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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
Copy link

bb-auto bot commented Mar 6, 2025

Thanks for your PR, @celadaris. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@bb-auto bb-auto bot requested a review from ArgoZhang March 6, 2025 17:22
@bb-auto bb-auto bot added the bug Something isn't working label Mar 6, 2025
@bb-auto bb-auto bot added this to the v9.2.0 milestone Mar 6, 2025
sourcery-ai[bot]
sourcery-ai bot previously approved these changes Mar 6, 2025
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 @celadaris - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider if SpinLock is the best choice here, given its potential for starvation and the relatively short critical section.
  • The javascript changes introduce a data attribute; ensure this doesn't conflict with any existing attributes or introduce security concerns.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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

codecov bot commented Mar 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (dbe0b7e) to head (01035af).
Report is 9 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #5555      +/-   ##
===========================================
+ Coverage   99.98%   100.00%   +0.01%     
===========================================
  Files         649       650       +1     
  Lines       29605     29627      +22     
  Branches     4172      4170       -2     
===========================================
+ Hits        29602     29627      +25     
+ Misses          2         0       -2     
+ Partials        1         0       -1     

☔ 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.

@celadaris
Copy link
Collaborator Author

sourcery-ai gave good feedback, ill update with SemaphoreSlim instead... just a moment...

switched SpinLock to SemaphoreSlim
@celadaris
Copy link
Collaborator Author

when network lag is introduced the problem is still present, just a moment for a fix...

This should work better under bad network conditions
@celadaris
Copy link
Collaborator Author

just updated for the network problems

@ArgoZhang
Copy link
Member

@celadaris Thank you very much for submitting the PR. Could you let us know what problem you encountered, why you changed it this way, whether there is a better solution, and whether this change will affect other components?

So can you create an Issue to provide a min-repro project for the problem? This PR is associated with this Issue so that we can track it.

Thank you very much for your hard work

@ArgoZhang ArgoZhang changed the title update fix(AutoComplete): network delay causes input lag Mar 7, 2025
@ArgoZhang ArgoZhang linked an issue Mar 7, 2025 that may be closed by this pull request
1 task
@ArgoZhang
Copy link
Member

@sourcery-ai review

@celadaris celadaris enabled auto-merge (rebase) March 7, 2025 17:40
@celadaris celadaris self-assigned this Mar 7, 2025
Copy link
Collaborator Author

@celadaris celadaris left a comment

Choose a reason for hiding this comment

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

tested, and works!

@celadaris celadaris disabled auto-merge March 7, 2025 17:53
@celadaris
Copy link
Collaborator Author

its asking for your review @ArgoZhang and i cant assign myself as a reviewer, but I did review the changes and it looks good!

@ArgoZhang ArgoZhang merged commit def8f94 into dotnetcore:main Mar 8, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(AutoComplete): network delay causes input lag bug(AutoComplete): interrupt input cause network delay

2 participants