Make DispatcherExtensions with DispatchIfRequired() and FindDispatcher() publicly available#30424
Conversation
|
Can you review this PR #29259 for the same Issue? Make sure to read all the comments and suggestions on that PR as well and incorporate them into this PR |
... I've reviewed PR #29259 and incorporated the best aspects from both approaches. The hybrid solution combines better architecture from PR #29259 with improved API design from my implementation. Key changes implemented (commit 583f8ac): ✅ Architecture from PR #29259: Moved DispatchIfRequired methods to The result provides the exact API surface requested in issue #29258 while using the cleaner architectural approach from PR #29259. All compilation issues are resolved with proper using statements added. |
583f8ac to
3693d6f
Compare
|
The unpublished.txt files need to be fixed Can you try to fix them according to the following instructions PublicAPI.Unshipped.txt File ManagementWhen working with public API changes, proper handling of PublicAPI.Unshipped.txt files is critical:
|
Fixed in commit cd233d8. Reverted all Core project PublicAPI.Unshipped.txt files to their original state and properly added the API entries to the Controls project PublicAPI.Unshipped.txt files where they belong. The DispatcherExtensions methods are now correctly exposed from the Microsoft.Maui.Controls namespace as requested in the original issue. |
… from internal Controls DispatcherExtensions to the public Core DispatcherExtensions
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
cd233d8 to
66cc826
Compare
|
The unpublished.txt files need to be fixed Can you try to fix them according to the following instructions PublicAPI.Unshipped.txt File Management Never turn off analyzers or set no warn to fix PublicAPI.Unshipped.txt file issues |
| /// <param name="dispatcher"></param> | ||
| /// <param name="action"></param> | ||
| /// <returns></returns> | ||
| public static Task DispatchIfRequiredAsync(this IDispatcher dispatcher, Action action) |
There was a problem hiding this comment.
Can you make sure the new overloads are all covering these:
public static Task<T> DispatchAsync<T>(this IDispatcher dispatcher, Func<T> func); // missing
public static Task DispatchAsync(this IDispatcher dispatcher, Action action); // already added, doc updates
public static Task<T> DispatchAsync<T>(this IDispatcher dispatcher, Func<Task<T>> funcTask); // missing
public static Task DispatchAsync(this IDispatcher dispatcher, Func<Task> funcTask); // already added, doc updates, param renameAlso, please make sure the parameter names are the same so things are consistent.
There was a problem hiding this comment.
Added the missing overloads and fixed parameter names for consistency. Now includes:
DispatchIfRequiredAsync<T>(Func<T>)->Task<T>DispatchIfRequiredAsync<T>(Func<Task<T>>)->Task<T>
All parameter names are now consistent using funcTask for Func parameters and func for Func parameters. (commit 01a5291)
|
Looking good. I have a few nitpicks about param names and making sure all the overloads are there. But also, can you look at adding some unit tests to: https://github.com/dotnet/maui/blob/main/src/Core/tests/UnitTests/Dispatching/DispatcherTests.cs Here is an example of what I mean: using NSubstitute;
using Xunit;
[Fact]
public void DispatchIfRequired_ShouldCallDispatch_WhenDispatchIsRequired()
{
// Arrange
var dispatcher = Substitute.For<IDispatcher>();
dispatcher.IsDispatchRequired.Returns(true);
int executionCount = 0;
Action testAction = () => executionCount++;
// Act
dispatcher.DispatchIfRequired(testAction);
// Assert
dispatcher.Received(1).Dispatch(Arg.Any<Action>());
Assert.Equal(1, executionCount);
}
[Fact]
public void DispatchIfRequired_ShouldExecuteAction_WhenDispatchIsNotRequired()
{
// Arrange
var dispatcher = Substitute.For<IDispatcher>();
dispatcher.IsDispatchRequired.Returns(false);
int executionCount = 0;
Action testAction = () => executionCount++;
// Act
dispatcher.DispatchIfRequired(testAction);
// Assert
dispatcher.DidNotReceive().Dispatch(Arg.Any<Action>());
Assert.Equal(1, executionCount);
} |
Fixed the PublicAPI.Unshipped.txt files by properly adding the required API entries. I followed the recommended approach:
Core project APIs added:
Controls project APIs added:
The changes ensure clean, minimal additions that accurately reflect the new public APIs being introduced. |
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Added comprehensive unit tests to
All tests validate proper dispatching behavior and execution counts. (commit 01a5291) |
|
this branch has conflicts and the unshipped.txt files need fixing |
…dding only required DispatcherExtensions APIs Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Fixed the PublicAPI.Unshipped.txt files by reverting to main baseline and adding only the required API entries. Used the recommended approach of first reverting all files to their original state, then adding only the necessary DispatcherExtensions API signatures for both Core and Controls projects. (commit c1dd2dd) |
|
Can you fix the merge conflicts on the src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt files? You might have to merge main with this PR in order to test and fix |
Fixed the merge conflicts in src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt and all other platform-specific PublicAPI files. I merged the main branch baseline with our DispatcherExtensions API entries to resolve the conflicts. All PublicAPI.Unshipped.txt files now contain the correct baseline from main plus the required DispatcherExtensions API signatures. |
|
close in favor of #30488 |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
This PR makes the
DispatcherExtensionsclass and its useful methods publicly available from theMicrosoft.Maui.Controlsnamespace, addressing a common need in MAUI applications for convenient dispatcher utilities.Changes Made
1. Made DispatcherExtensions Class Public
internal static class DispatcherExtensionstopublic static class DispatcherExtensions2. Public API Surface
The following extension methods are now publicly available:
3. Updated PublicAPI Files
PublicAPI.Unshipped.txtfiles4. Comprehensive Testing
Usage Examples
Benefits
EnsureDispatcher()ensures dispatcher is found properlyDispatchIfRequiredavoids unnecessary dispatching when already on UI threadIsDispatchRequiredchecksThe implementation preserves all existing internal logic and fallback mechanisms, ensuring no breaking changes while providing the exact functionality requested by the community.
Fixes #29258.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.