* Changed update awaiting mechanism. Uses TaskCompletationSource, instead of ManualResetEvent

* Updated Dependencies
* Version incremented
This commit is contained in:
gutii
2026-04-23 21:29:20 +04:00
parent d4a5d3d6ee
commit 5dbf7f4073
9 changed files with 34 additions and 25 deletions
+1 -1
View File
@@ -4529,7 +4529,7 @@
Gets the update that triggered this awaiter handler.
</summary>
</member>
<member name="M:Telegrator.Handlers.Building.AwaiterHandler.Wait(System.Threading.CancellationToken)">
<member name="M:Telegrator.Handlers.Building.AwaiterHandler.Await(System.Threading.CancellationToken)">
<summary>
Waits for the specified update type to be received.
</summary>
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting.Web</Title>
<Version>1.16.8</Version>
<Version>1.16.9</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting</Title>
<Version>1.16.8</Version>
<Version>1.16.9</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -31,8 +31,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.7" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.7" />
</ItemGroup>
<ItemGroup>
@@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="10.0.7" />
</ItemGroup>
<ItemGroup>
+1
View File
@@ -9,3 +9,4 @@ using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style", "IDE0090")]
[assembly: SuppressMessage("Style", "IDE0057")]
[assembly: SuppressMessage("Style", "IDE0270")]
[assembly: SuppressMessage("Roslynator", "RCS1037")]
@@ -15,8 +15,8 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
/// <summary>
/// Manual reset event used for synchronization.
/// </summary>
private ManualResetEventSlim ResetEvent = new ManualResetEventSlim(false);
private readonly TaskCompletionSource<Update> ResetEvent = new TaskCompletionSource<Update>();
/// <summary>
/// Gets the update that triggered this awaiter handler.
/// </summary>
@@ -26,10 +26,10 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
/// Waits for the specified update type to be received.
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel the wait operation.</param>
public void Wait(CancellationToken cancellationToken)
public async Task<Update> Await(CancellationToken cancellationToken)
{
ResetEvent.Reset();
ResetEvent.Wait(cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
return await ResetEvent.Task.ConfigureAwait(false);
}
/// <summary>
@@ -51,8 +51,18 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
/// <returns>A completed task.</returns>
protected override Task<Result> 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());
}
}
/// <inheritdoc/>
@@ -61,8 +71,7 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
if (!disposing)
return true;
ResetEvent.Dispose();
ResetEvent = null!;
ResetEvent.TrySetCanceled();
return true;
}
}
@@ -73,13 +73,11 @@ public class AwaiterHandlerBuilder<TUpdate> : 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<TUpdate>();
}
await Task.CompletedTask;
return handlerInstance.HandlingUpdate.GetActualUpdateObject<TUpdate>();
}
}
+1 -1
View File
@@ -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);
}
+5 -4
View File
@@ -14,7 +14,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator : Telegram.Bot mediator framework</Title>
<Version>1.16.8</Version>
<Version>1.16.9</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -23,6 +23,7 @@
<PackageIcon>telegrator_nuget.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<UserSecretsId>b78bc62d-e49f-4ef0-b2de-ff4ceb3971af</UserSecretsId>
</PropertyGroup>
<ItemGroup>
@@ -32,9 +33,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.5" />
<PackageReference Include="System.Threading.Channels" Version="10.0.5" />
<PackageReference Include="Telegram.Bot" Version="22.9.5.3" />
<PackageReference Include="Telegram.Bot" Version="22.9.6.1" />
<PackageReference Include="System.Threading.Channels" Version="10.0.7" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.7" />
</ItemGroup>
<ItemGroup>