Skip to content

Commit 97d898e

Browse files
committed
Shared delegates
1 parent 8ccc584 commit 97d898e

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace Soenneker.Utils.BackgroundQueue.Abstract;
5+
6+
/// <summary>
7+
/// Represents an asynchronous work item that operates on a provided state and supports cancellation.
8+
/// </summary>
9+
/// <remarks>Use this delegate to define asynchronous operations that require state and support
10+
/// cancellation. The operation should observe the provided cancellation token and complete the returned ValueTask
11+
/// when finished or canceled.</remarks>
12+
/// <typeparam name="TState">The type of the state object to be passed to the work item.</typeparam>
13+
/// <param name="state">The state object to be used by the work item. The type and meaning of this value are determined by the caller.</param>
14+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
15+
/// <returns>A ValueTask that represents the asynchronous operation of the work item.</returns>
16+
public delegate ValueTask ValueTaskWorkItem<in TState>(TState state, CancellationToken cancellationToken);
17+
18+
/// <summary>
19+
/// Represents an asynchronous work item that executes with a specified state and supports cancellation.
20+
/// </summary>
21+
/// <remarks>The delegate should honor the cancellation request by observing the provided cancellation
22+
/// token and responding appropriately if cancellation is requested.</remarks>
23+
/// <typeparam name="TState">The type of the state object to be passed to the work item.</typeparam>
24+
/// <param name="state">The state object to be used by the work item during execution.</param>
25+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
26+
/// <returns>A task that represents the asynchronous operation.</returns>
27+
public delegate Task TaskWorkItem<in TState>(TState state, CancellationToken cancellationToken);

src/Abstract/IBackgroundQueue.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ namespace Soenneker.Utils.BackgroundQueue.Abstract;
1313
/// </remarks>
1414
public interface IBackgroundQueue
1515
{
16+
/// <summary>
17+
/// Queues a work item represented by a ValueTask for execution, passing the specified state to the work item
18+
/// delegate.
19+
/// </summary>
20+
/// <typeparam name="TState">The type of the state object to pass to the work item.</typeparam>
21+
/// <param name="state">The state object to pass to the work item delegate when it is executed.</param>
22+
/// <param name="workItem">A delegate that represents the work to execute. The delegate receives the provided state object.</param>
23+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the queued work item before it starts executing. The default
24+
/// value is <see cref="CancellationToken.None"/>.</param>
25+
/// <returns>A ValueTask that represents the queued work item. The task completes when the work item has finished executing.</returns>
26+
ValueTask QueueValueTask<TState>(TState state, ValueTaskWorkItem<TState> workItem, CancellationToken cancellationToken = default);
27+
28+
/// <summary>
29+
/// Queues a work item for execution, passing the specified state to the provided delegate.
30+
/// </summary>
31+
/// <typeparam name="TState">The type of the state object to pass to the work item.</typeparam>
32+
/// <param name="state">The state object to pass to the work item when it is executed.</param>
33+
/// <param name="workItem">A delegate that represents the work item to execute. The delegate receives the specified state object.</param>
34+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the queued work item before it starts executing. The default
35+
/// value is <see cref="CancellationToken.None"/>.</param>
36+
/// <returns>A <see cref="ValueTask"/> that represents the queued work item. The task completes when the work item has been
37+
/// executed or canceled.</returns>
38+
ValueTask QueueTask<TState>(TState state, TaskWorkItem<TState> workItem, CancellationToken cancellationToken = default);
39+
1640
/// <summary>
1741
/// Queues a <see cref="ValueTask"/> to be executed by the background service.
1842
/// </summary>

src/BackgroundQueue.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ namespace Soenneker.Utils.BackgroundQueue;
1717
/// <inheritdoc cref="IBackgroundQueue"/>
1818
public sealed class BackgroundQueue : IBackgroundQueue
1919
{
20-
public delegate ValueTask ValueTaskWorkItem<in TState>(TState state, CancellationToken ct);
21-
22-
public delegate Task TaskWorkItem<in TState>(TState state, CancellationToken ct);
23-
2420
private readonly Channel<ValueTaskEnvelope> _valueTaskChannel;
2521
private readonly Channel<TaskEnvelope> _taskChannel;
2622

0 commit comments

Comments
 (0)