Skip to content

Commit b4bbc25

Browse files
author
Meyn
committed
Remove events from Options
1 parent 72d7899 commit b4bbc25

File tree

5 files changed

+101
-166
lines changed

5 files changed

+101
-166
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ orchestrators, and complex data pipelines.
1313

1414
### Key Features
1515

16-
**Priority Scheduling**: High-priority requests jump the queue automatically
17-
**Smart Retries**: Configurable retry logic with exponential backoff
18-
**Pause & Resume**: Stop and restart long-running operations without data loss
19-
**Progress Tracking**: Real-time aggregated progress across multiple operations
20-
**Dynamic Parallelism**: Auto-adjusts concurrency based on system load
21-
**Zero Lock-in**: Simple wrapper pattern around your existing async code
16+
- **Priority Scheduling**: High-priority requests jump the queue automatically
17+
- **Smart Retries**: Configurable retry logic with exponential backoff
18+
- **Pause & Resume**: Stop and restart long-running operations without data loss
19+
- **Progress Tracking**: Real-time aggregated progress across multiple operations
20+
- **Dynamic Parallelism**: Auto-adjusts concurrency based on system load
21+
- **Zero Lock-in**: Simple wrapper pattern around your existing async code
2222

2323
## Quick Start
2424

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,55 @@
1-
namespace Requests.Options
1+
namespace Requests.Options
22
{
33
/// <summary>
4-
/// A generic interface that defines the structure for all <see cref="IRequest"/> types.
4+
/// Defines the configuration options for a request.
55
/// </summary>
6-
/// <typeparam name="TCompleted">The return type if the request is completed successfully.</typeparam>
7-
/// <typeparam name="TFailed">The return type if the request fails.</typeparam>
8-
public interface IRequestOptions<TCompleted, TFailed>
6+
public interface IRequestOptions
97
{
108
/// <summary>
119
/// Determines whether the <see cref="IRequest"/> should be automatically started upon initialization.
1210
/// </summary>
13-
public bool AutoStart { get; set; }
11+
bool AutoStart { get; init; }
1412

1513
/// <summary>
1614
/// Specifies if the <see cref="IRequest"/> has priority over other non-prioritized <see cref="IRequest">Requests</see>.
1715
/// </summary>
18-
public RequestPriority Priority { get; set; }
16+
RequestPriority Priority { get; init; }
1917

2018
/// <summary>
21-
/// Delays the start of the <see cref="IRequest"/> by a specified number of milliseconds on every start call.
19+
/// Delays the start of the <see cref="IRequest"/> by a specified duration on every start call.
20+
/// Mutable at runtime.
2221
/// </summary>
23-
public TimeSpan? DeployDelay { get; set; }
22+
TimeSpan? DeployDelay { get; set; }
2423

2524
/// <summary>
2625
/// Sets the <see cref="IRequestHandler"/> for the <see cref="IRequest"/>.
2726
/// </summary>
28-
public IRequestHandler? Handler { get; set; }
27+
IRequestHandler? Handler { get; init; }
2928

3029
/// <summary>
3130
/// Specifies the number of times the <see cref="IRequest"/> should be retried if it fails.
3231
/// </summary>
33-
public byte NumberOfAttempts { get; set; }
32+
byte NumberOfAttempts { get; init; }
3433

3534
/// <summary>
3635
/// Specifies the delay duration before a new attempt is made if the <see cref="IRequest"/> fails.
3736
/// </summary>
38-
public TimeSpan? DelayBetweenAttemps { get; set; }
37+
TimeSpan? DelayBetweenAttempts { get; init; }
3938

4039
/// <summary>
4140
/// A <see cref="System.Threading.CancellationToken"/> that the user can set to cancel the <see cref="IRequest"/>.
4241
/// </summary>
43-
public CancellationToken? CancellationToken { get; set; }
42+
CancellationToken CancellationToken { get; init; }
4443

4544
/// <summary>
4645
/// Specifies a request that should be executed immediately after this request completes, bypassing the queue.
46+
/// Mutable at runtime with validation.
4747
/// </summary>
4848
/// <remarks>
4949
/// The subsequent request supports auto-starting if enabled, but this behavior can be disabled if not desired.
5050
/// <br/>If the subsequent request is already running, it will not be started again.
5151
/// <br/>If the holding request fails, the subsequent request will be canceled and disposed.
5252
/// </remarks>
53-
public IRequest? SubsequentRequest { get; set; }
54-
55-
/// <summary>
56-
/// An event that will be triggered when the <see cref="IRequest"/> is cancelled.
57-
/// </summary>
58-
public Action<IRequest>? RequestCancelled { get; set; }
59-
60-
/// <summary>
61-
/// An event that will be triggered when the <see cref="IRequest"/> is started.
62-
/// </summary>
63-
public Action<IRequest>? RequestStarted { get; set; }
64-
65-
/// <summary>
66-
/// An event that will be triggered when the <see cref="IRequest"/> is completed.
67-
/// </summary>
68-
public Action<IRequest, TCompleted>? RequestCompleted { get; set; }
69-
70-
/// <summary>
71-
/// An event that will be triggered when the <see cref="IRequest"/> fails.
72-
/// </summary>
73-
public Action<IRequest, TFailed>? RequestFailed { get; set; }
74-
75-
/// <summary>
76-
/// An event that will be triggered when an exception occurs during the <see cref="IRequest"/> execution.
77-
/// </summary>
78-
public Action<IRequest, Exception>? RequestExceptionOccurred { get; set; }
53+
IRequest? SubsequentRequest { get; set; }
7954
}
8055
}

Requests/Options/RequestOptions.cs

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,45 @@
1-
namespace Requests.Options
1+
namespace Requests.Options
22
{
33
/// <summary>
4-
/// A record that implements the <see cref="IRequestOptions{TCompleted, TFailed}"/> interface.
4+
/// A record that implements the <see cref="IRequestOptions"/> interface.
5+
/// Provides configuration for request execution behavior.
56
/// </summary>
6-
/// <typeparam name="TCompleted">The return type if the request is completed successfully.</typeparam>
7-
/// <typeparam name="TFailed">The return type if the request fails.</typeparam>
8-
public record RequestOptions<TCompleted, TFailed> : IRequestOptions<TCompleted, TFailed>
7+
public record RequestOptions : IRequestOptions
98
{
10-
/// <inheritdoc />
11-
public bool AutoStart { get; set; } = true;
12-
13-
///<inheritdoc />
14-
public RequestPriority Priority { get; set; } = RequestPriority.Normal;
15-
16-
///<inheritdoc />
17-
public CancellationToken? CancellationToken { get; set; }
18-
19-
///<inheritdoc />
20-
public TimeSpan? DeployDelay { get; set; } = null;
9+
private IRequest? _subsequentRequest;
2110

22-
///<inheritdoc />
23-
public IRequestHandler? Handler { get; set; } = ParallelRequestHandler.MainRequestHandler;
24-
25-
///<inheritdoc />
26-
public byte NumberOfAttempts { get; set; } = 3;
27-
28-
///<inheritdoc />
29-
public TimeSpan? DelayBetweenAttemps { get; set; } = null;
11+
/// <inheritdoc />
12+
public bool AutoStart { get; init; } = true;
3013

31-
///<inheritdoc />
32-
public IRequest? SubsequentRequest { get; set; }
14+
/// <inheritdoc />
15+
public RequestPriority Priority { get; init; } = RequestPriority.Normal;
3316

34-
///<inheritdoc />
35-
public Action<IRequest>? RequestStarted { get; set; }
17+
/// <inheritdoc />
18+
public CancellationToken CancellationToken { get; init; }
3619

37-
///<inheritdoc />
38-
public Action<IRequest, TCompleted>? RequestCompleted { get; set; }
20+
/// <inheritdoc />
21+
public TimeSpan? DeployDelay { get; set; }
3922

40-
///<inheritdoc />
41-
public Action<IRequest, TFailed>? RequestFailed { get; set; }
23+
/// <inheritdoc />
24+
public IRequestHandler? Handler { get; init; } = ParallelRequestHandler.MainRequestHandler;
4225

43-
///<inheritdoc />
44-
public Action<IRequest>? RequestCancelled { get; set; }
26+
/// <inheritdoc />
27+
public byte NumberOfAttempts { get; init; } = 3;
4528

46-
///<inheritdoc />
47-
public Action<IRequest, Exception>? RequestExceptionOccurred { get; set; }
29+
/// <inheritdoc />
30+
public TimeSpan? DelayBetweenAttempts { get; init; }
4831

49-
/// <summary>
50-
/// Copy constructor for the RequestOptions record.
51-
/// </summary>
52-
/// <param name="options">The RequestOptions instance to copy from.</param>
53-
protected RequestOptions(RequestOptions<TCompleted, TFailed> options)
32+
/// <inheritdoc />
33+
public IRequest? SubsequentRequest
5434
{
55-
Priority = options.Priority;
56-
Handler = options.Handler;
57-
NumberOfAttempts = options.NumberOfAttempts;
58-
CancellationToken = options.CancellationToken;
59-
AutoStart = options.AutoStart;
60-
DelayBetweenAttemps = options.DelayBetweenAttemps;
61-
DeployDelay = options.DeployDelay;
62-
SubsequentRequest = options.SubsequentRequest;
63-
RequestCancelled += options.RequestCancelled;
64-
RequestStarted += options.RequestStarted;
65-
RequestFailed += options.RequestFailed;
66-
RequestCompleted += options.RequestCompleted;
67-
RequestExceptionOccurred += options.RequestExceptionOccurred;
35+
get => _subsequentRequest;
36+
set
37+
{
38+
if (value?.HasCompleted() == true)
39+
throw new ArgumentException("Cannot set a completed request as subsequent request.", nameof(value));
40+
41+
_subsequentRequest = value;
42+
}
6843
}
69-
70-
/// <summary>
71-
/// Default constructor for the RequestOptions record.
72-
/// </summary>
73-
public RequestOptions() { }
7444
}
7545
}

Requests/OwnRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Requests
55
/// <summary>
66
/// A class that simplifies implementing <see cref="Request{TOptions, TCompleted, TFailed}"/> functionality without creating a new child of <see cref="Request{TOptions, TCompleted, TFailed}"/>.
77
/// </summary>
8-
public class OwnRequest : Request<RequestOptions<object, object>, object, object>
8+
public class OwnRequest : Request<RequestOptions, object, object>
99
{
1010
private readonly Func<CancellationToken, Task<bool>> _own;
1111

@@ -14,7 +14,7 @@ public class OwnRequest : Request<RequestOptions<object, object>, object, object
1414
/// </summary>
1515
/// <param name="own">Function that contains the request logic.</param>
1616
/// <param name="requestOptions">Options to modify the behavior of <see cref="OwnRequest"/>.</param>
17-
public OwnRequest(Func<CancellationToken, Task<bool>> own, RequestOptions<object, object>? requestOptions = null) : base(requestOptions)
17+
public OwnRequest(Func<CancellationToken, Task<bool>> own, RequestOptions? requestOptions = null) : base(requestOptions)
1818
{
1919
_own = own;
2020
AutoStart();

0 commit comments

Comments
 (0)