diff --git a/docs/Telegrator.xml b/docs/Telegrator.xml index 2fe8be2..35a1997 100644 --- a/docs/Telegrator.xml +++ b/docs/Telegrator.xml @@ -4529,7 +4529,7 @@ Gets the update that triggered this awaiter handler. - + Waits for the specified update type to be received. diff --git a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj index 3597694..1fba795 100644 --- a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj +++ b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj @@ -15,7 +15,7 @@ True Telegrator.Hosting.Web - 1.16.8 + 1.16.9 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Hosting/Telegrator.Hosting.csproj b/src/Telegrator.Hosting/Telegrator.Hosting.csproj index 05e0b09..3ae138e 100644 --- a/src/Telegrator.Hosting/Telegrator.Hosting.csproj +++ b/src/Telegrator.Hosting/Telegrator.Hosting.csproj @@ -15,7 +15,7 @@ True Telegrator.Hosting - 1.16.8 + 1.16.9 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator @@ -31,8 +31,8 @@ - - + + diff --git a/src/Telegrator.Localized/Telegrator.Localized.csproj b/src/Telegrator.Localized/Telegrator.Localized.csproj index 11fe976..ee731d7 100644 --- a/src/Telegrator.Localized/Telegrator.Localized.csproj +++ b/src/Telegrator.Localized/Telegrator.Localized.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/Telegrator/GlobalSuppressions.cs b/src/Telegrator/GlobalSuppressions.cs index e18cf2d..cef1ccc 100644 --- a/src/Telegrator/GlobalSuppressions.cs +++ b/src/Telegrator/GlobalSuppressions.cs @@ -9,3 +9,4 @@ using System.Diagnostics.CodeAnalysis; [assembly: SuppressMessage("Style", "IDE0090")] [assembly: SuppressMessage("Style", "IDE0057")] [assembly: SuppressMessage("Style", "IDE0270")] +[assembly: SuppressMessage("Roslynator", "RCS1037")] diff --git a/src/Telegrator/Handlers/Building/AwaiterHandler.cs b/src/Telegrator/Handlers/Building/AwaiterHandler.cs index efc140e..8ab6245 100644 --- a/src/Telegrator/Handlers/Building/AwaiterHandler.cs +++ b/src/Telegrator/Handlers/Building/AwaiterHandler.cs @@ -15,8 +15,8 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase /// /// Manual reset event used for synchronization. /// - private ManualResetEventSlim ResetEvent = new ManualResetEventSlim(false); - + private readonly TaskCompletionSource ResetEvent = new TaskCompletionSource(); + /// /// Gets the update that triggered this awaiter handler. /// @@ -26,10 +26,10 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase /// Waits for the specified update type to be received. /// /// The cancellation token to cancel the wait operation. - public void Wait(CancellationToken cancellationToken) + public async Task Await(CancellationToken cancellationToken) { - ResetEvent.Reset(); - ResetEvent.Wait(cancellationToken); + cancellationToken.ThrowIfCancellationRequested(); + return await ResetEvent.Task.ConfigureAwait(false); } /// @@ -51,8 +51,18 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase /// A completed task. protected override Task ExecuteInternal(IHandlerContainer container, CancellationToken cancellation) { - ResetEvent.Set(); - return Task.FromResult(Result.Ok()); + try + { + if (!ResetEvent.TrySetResult(HandlingUpdate)) + ResetEvent.TrySetCanceled(cancellation); + + return Task.FromResult(Result.Ok()); + } + catch (Exception ex) + { + ResetEvent.TrySetException(ex); + return Task.FromResult(Result.Fault()); + } } /// @@ -61,8 +71,7 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase if (!disposing) return true; - ResetEvent.Dispose(); - ResetEvent = null!; + ResetEvent.TrySetCanceled(); return true; } } diff --git a/src/Telegrator/Handlers/Building/AwaiterHandlerBuilder.cs b/src/Telegrator/Handlers/Building/AwaiterHandlerBuilder.cs index 21a5368..b991c5b 100644 --- a/src/Telegrator/Handlers/Building/AwaiterHandlerBuilder.cs +++ b/src/Telegrator/Handlers/Building/AwaiterHandlerBuilder.cs @@ -73,13 +73,11 @@ public class AwaiterHandlerBuilder : HandlerBuilderBase, IAwaiterHandle AwaiterHandler handlerInstance = new AwaiterHandler(UpdateType); HandlerDescriptor descriptor = BuildImplicitDescriptor(handlerInstance); - + using (HandlerProvider.UseHandler(descriptor)) { - handlerInstance.Wait(cancellationToken); + await handlerInstance.Await(cancellationToken); + return handlerInstance.HandlingUpdate.GetActualUpdateObject(); } - - await Task.CompletedTask; - return handlerInstance.HandlingUpdate.GetActualUpdateObject(); } } diff --git a/src/Telegrator/Providers/AwaitingProvider.cs b/src/Telegrator/Providers/AwaitingProvider.cs index f4af7c4..0697abf 100644 --- a/src/Telegrator/Providers/AwaitingProvider.cs +++ b/src/Telegrator/Providers/AwaitingProvider.cs @@ -46,7 +46,7 @@ public class AwaitingProvider(TelegratorOptions options) : HandlersProvider([], public readonly void Register() { if (handlerDescriptor.SingletonInstance == null) - throw new Exception(); + throw new Exception("Handler descriptor has no singleton instance."); handlersList.Add(handlerDescriptor); } diff --git a/src/Telegrator/Telegrator.csproj b/src/Telegrator/Telegrator.csproj index 469cc7e..f4c0664 100644 --- a/src/Telegrator/Telegrator.csproj +++ b/src/Telegrator/Telegrator.csproj @@ -14,7 +14,7 @@ True Telegrator : Telegram.Bot mediator framework - 1.16.8 + 1.16.9 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator @@ -23,6 +23,7 @@ telegrator_nuget.png README.md LICENSE + b78bc62d-e49f-4ef0-b2de-ff4ceb3971af @@ -32,9 +33,9 @@ - - - + + +