Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/BootstrapBlazor/Services/ThrottleDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ThrottleDispatcher(ThrottleOptions options)
/// </summary>
/// <param name="function">异步回调方法</param>
/// <param name="cancellationToken">取消令牌</param>
public Task ThrottleAsync(Func<Task> function, CancellationToken cancellationToken = default) => InternalThrottleAsync(() => Task.Run(function), cancellationToken);
public Task ThrottleAsync(Func<Task> function, CancellationToken cancellationToken = default) => InternalThrottleAsync(function, cancellationToken);

/// <summary>
/// 同步限流方法
Expand All @@ -32,19 +32,12 @@ public class ThrottleDispatcher(ThrottleOptions options)
/// <param name="token">取消令牌</param>
public void Throttle(Action action, CancellationToken token = default)
{
var task = InternalThrottleAsync(() => Task.Run(() =>
var task = InternalThrottleAsync(() =>
{
action();
return Task.CompletedTask;
}, CancellationToken.None), token);
try
{
task.Wait(token);
}
catch (Exception)
{
throw;
}
}, token);
task.Wait(token);
return;
}

Expand Down
24 changes: 0 additions & 24 deletions test/UnitTest/Services/ThrottleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,6 @@ await dispatcher.ThrottleAsync(() =>
Assert.Equal(2, count);
}

[Fact]
public async Task Cancel_Ok()
{
var factory = Context.Services.GetRequiredService<IThrottleDispatcherFactory>();
var dispatcher = factory.GetOrCreate("Cancel");

var cts = new CancellationTokenSource();
var ex = await Assert.ThrowsAsync<OperationCanceledException>(() =>
{
dispatcher.Throttle(() =>
{
cts.Cancel();
}, cts.Token);
return Task.CompletedTask;
});
Assert.NotNull(ex);

cts = new CancellationTokenSource(100);
await dispatcher.ThrottleAsync(async () =>
{
await Task.Delay(300);
}, cts.Token);
}

[Fact]
public void Clear()
{
Expand Down
Loading