Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CancellationToken overloads. #77

Merged
merged 6 commits into from
Dec 31, 2024
Merged

Conversation

S-Luiten
Copy link
Contributor

Fixes #76

Copy link
Collaborator

@ADefWebserver ADefWebserver left a comment

Choose a reason for hiding this comment

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

Looks good, but, I don't understand why the Interop class has to be changed to internal. This would cause a breaking change. Yes, I know developers should not reference it, but, for those that are, I never want to introduce a breaking change to add a feature.

using System.Threading.Tasks;

namespace Blazored.TextEditor
{
public static class Interop
internal static class Interop
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this required?

@S-Luiten
Copy link
Contributor Author

Looks good, but, I don't understand why the Interop class has to be changed to internal. This would cause a breaking change. Yes, I know developers should not reference it, but, for those that are, I never want to introduce a breaking change to add a feature.

I just noticed the class only contains internal members so it might as well be internal itself, but you're right it would be a breaking change. I reverted this change.

@ADefWebserver
Copy link
Collaborator

Can you add examples on the Server project and the Client project that shows how this would be used?

@S-Luiten
Copy link
Contributor Author

I have updated the example projects to show how to use the cancellationToken parameter. You can trigger it by adding a large image or simply a lot of text to the editor. The default size limit is 32kB, if you go over this limit the method will never complete, so a timeout is used to force the method to cancel.

I also changed all async void to async Task, by doing this you no longer need to manually invoke StateHasChanged.

Copy link
Collaborator

@ADefWebserver ADefWebserver left a comment

Choose a reason for hiding this comment

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

Ok I asked ChatGPT about this one...

In most cases, yes—changing a public async void method to public async Task is considered a breaking change from an API/contract standpoint because you have changed the method’s return type. Any consumer code that depends on the signature being void (for example, storing the method in a Action delegate instead of a Func<Task>, or using reflection to find a method that returns void) could break at compile-time or runtime.

That said, if your consumers are simply calling InsertText() and do not rely on its return type, then they likely will not notice a difference (i.e., calling InsertText() will still compile and work as before). However, strictly speaking, changing a public method’s return type is considered a breaking change, and should be treated as such when versioning or communicating changes in your library.


Why switching from async void to async Task is typically recommended:

  1. Error Handling:
    Exceptions thrown in an async void method cannot be caught by the caller, making error handling more difficult and potentially allowing unhandled exceptions to crash the application. With an async Task method, the caller can await it, catch exceptions, and handle them.

  2. Composition:
    async Task methods can be composed (e.g., awaited or used in Task.WhenAll, etc.). async void methods cannot.

  3. API Consistency:
    Returning Task for asynchronous operations is the more common pattern in .NET libraries and helps signal to consumers that the method is asynchronous and should be awaited if they want to ensure it completes before proceeding.

So while there is a technical breaking change in the signature, it is usually a net positive change in terms of best practices and correctness.

@ADefWebserver ADefWebserver merged commit 4ab96a9 into Blazored:main Dec 31, 2024
@S-Luiten S-Luiten deleted the fix/#76 branch December 31, 2024 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Add overloads with CancellationToken parameter to async methods.
2 participants