Add TimeProvider support to DebounceDispatcher and ThrottleDispatcher#12435
Conversation
Co-authored-by: danielchalmers <7112040+danielchalmers@users.noreply.github.com>
Co-authored-by: danielchalmers <7112040+danielchalmers@users.noreply.github.com>
Co-authored-by: danielchalmers <7112040+danielchalmers@users.noreply.github.com>
|
@copilot instead of adding optional |
Co-authored-by: ScarletKuro <19953225+ScarletKuro@users.noreply.github.com>
Changed to use separate constructor overloads. The original constructors remain unchanged, and new overloads accept |
There was a problem hiding this comment.
Pull request overview
This pull request adds TimeProvider support to DebounceDispatcher and ThrottleDispatcher to enable better testability using FakeTimeProvider instead of real time delays. This allows tests to advance time artificially rather than waiting for actual delays to elapse.
Changes:
- Added TimeProvider parameter support to DebounceDispatcher and ThrottleDispatcher with constructor overloads
- Updated components (MudTabs, MudDebouncedInput, MudColorPicker) to inject and pass TimeProvider
- Converted select tests to use FakeTimeProvider for time-controlled testing
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/MudBlazor/Utilities/Throttle/ThrottleDispatcher.cs | Added TimeProvider parameter with constructor overloads, migrated DateTime to DateTimeOffset, replaced DateTime.UtcNow with timeProvider.GetUtcNow() |
| src/MudBlazor/Utilities/Debounce/DebounceDispatcher.cs | Added TimeProvider parameter with constructor overloads, migrated DateTime to DateTimeOffset, replaced Task.Delay with TimeProvider-aware delay |
| src/MudBlazor/Components/Tabs/MudTabs.razor.cs | Injected TimeProvider via DI and passed to ThrottleDispatcher, changed field to nullable |
| src/MudBlazor/Components/Input/MudDebouncedInput.cs | Injected TimeProvider via DI and passed to DebounceDispatcher |
| src/MudBlazor/Components/ColorPicker/MudColorPicker.razor.cs | Injected TimeProvider via DI and passed to ThrottleDispatcher |
| src/MudBlazor.UnitTests/Utilities/Throttle/ThrottleDispatcherTests.cs | Updated one test to use FakeTimeProvider for time-controlled testing |
| src/MudBlazor.UnitTests/Utilities/Debounce/DebounceDispatcherTests.cs | Updated two tests to use FakeTimeProvider for time-controlled testing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| private IResizeObserver? _resizeObserver; | ||
| private MudDropContainer<MudTabPanel>? _dropContainer; | ||
| private readonly ThrottleDispatcher _throttleDispatcher; | ||
| private ThrottleDispatcher? _throttleDispatcher; |
There was a problem hiding this comment.
The _throttleDispatcher field is used in the MudTabs.razor markup without null-checking (lines 17 and 56 of the razor file), but it's declared as nullable and only initialized in OnInitialized(). This could cause a NullReferenceException if the component attempts to render before OnInitialized() is called. Consider either: (1) making the field non-nullable and initializing it with a default value, or (2) adding null-conditional operator usage in the razor markup.
| private ThrottleDispatcher? _throttleDispatcher; | |
| private ThrottleDispatcher _throttleDispatcher = new(); |
|
@copilot the copilot code-review found this concern
Wouldn't it be better to wrap ThrottleDispatcher in lazy for |
Wraps ThrottleDispatcher in Lazy to allow initialization in constructor while accessing injected TimeProvider. This eliminates the nullable field and potential null reference issues in razor markup. Co-authored-by: ScarletKuro <19953225+ScarletKuro@users.noreply.github.com>
You're correct! Using |
Plan: Make DebounceDispatcher/ThrottleDispatcher TimeProvider-aware
DebounceDispatcherto acceptTimeProviderparameterTimeProviderconstructor parameter (defaults toTimeProvider.System)DateTime.UtcNowwithtimeProvider.GetUtcNow()Task.DelaywithtimeProvider.Delay(using TimeProviderTaskExtensions)ThrottleDispatcherto acceptTimeProviderparameterTimeProviderconstructor parameter (defaults toTimeProvider.System)DateTime.UtcNowwithtimeProvider.GetUtcNow()TimeProviderfrom DIMudDebouncedInputto inject and passTimeProviderMudTabsto inject and passTimeProvider(using Lazy for safe initialization)MudColorPickerto inject and passTimeProviderFakeTimeProviderDebounceDispatcherTeststo useFakeTimeProviderThrottleDispatcherTeststo useFakeTimeProviderOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.