Skip to content

Commit 2e88f22

Browse files
author
Meyn
committed
Implement support for overwriting SubsequentRequest property in the Request<> class
1 parent 3e34a66 commit 2e88f22

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

Requests/IRequest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ public interface IRequest : IDisposable
5858
public abstract void Start();
5959

6060
/// <summary>
61-
/// Puts the <see cref="IRequest"/> on hold.
61+
/// Puts the <see cref="IRequest"/> on hold, allowing it to be resumed later.
6262
/// </summary>
6363
public abstract void Pause();
6464

6565
/// <summary>
66-
/// Tries to set the <see cref="IRequest"/> <see cref="State"/> to idle.
66+
/// Attempts to set the <see cref="IRequest"/> <see cref="State"/> to idle.
6767
/// </summary>
68+
/// <returns><c>true</c> if the state was successfully set to idle; otherwise, <c>false</c>.</returns>
6869
public abstract bool TrySetIdle();
70+
71+
/// <summary>
72+
/// Checks whether the <see cref="IRequest"/> has reached a final state and will no longer change.
73+
/// </summary>
74+
/// <returns><c>true</c> if the request is in a final state; otherwise, <c>false</c>.</returns>
75+
public abstract bool HasCompleted();
6976
}
7077
}

Requests/Request.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public virtual RequestState State
100100
{
101101
get => _state; protected set
102102
{
103-
if (_state is RequestState.Compleated or RequestState.Failed or RequestState.Cancelled)
103+
if (HasCompleted())
104104
return;
105105
_state = value;
106106
SynchronizationContext.Post((o) => StateChanged?.Invoke((IRequest)o!, value), this);
@@ -357,6 +357,25 @@ public bool TrySetIdle()
357357
return State == RequestState.Idle;
358358
}
359359

360+
/// <summary>
361+
/// Sets a subsequent <see cref="IRequest"/> if neither the current nor the provided request has completed.
362+
/// </summary>
363+
/// <param name="request">The subsequent request to set.</param>
364+
/// <returns><c>true</c> if the request was set; otherwise, <c>false</c>.</returns>
365+
public bool TrySetSubsequentRequest(IRequest request)
366+
{
367+
if (HasCompleted() || request.HasCompleted())
368+
return false;
369+
Options.SubsequentRequest = request;
370+
return true;
371+
}
372+
373+
/// <summary>
374+
/// Checks whether the <see cref="IRequest"/> has reached a final state (e.g., completed, failed, or cancelled) and will no longer change.
375+
/// </summary>
376+
/// <returns><c>true</c> if the request is in a final state; otherwise, <c>false</c>.</returns>
377+
public bool HasCompleted() => State is RequestState.Compleated or RequestState.Failed or RequestState.Cancelled;
378+
360379
/// <summary>
361380
/// A class that encapsulates the return objects and notifications for <see cref="Request{TOptions, TCompleated, TFailed}"/>.
362381
/// </summary>

Requests/RequestContainer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,5 +378,11 @@ public bool TrySetIdle()
378378
/// </summary>
379379
/// <returns>An enumerable of non-null requests.</returns>
380380
private IEnumerable<TRequest> GetStored() => _requests[.._count].Where(req => req != null)!;
381+
382+
/// <summary>
383+
/// Since the container's state depends on the items it holds, it cannot complete.
384+
/// </summary>
385+
/// <returns>Always returns <c>false</c></returns>
386+
public bool HasCompleted() => false;
381387
}
382388
}

Requests/RequestHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ private async Task HandleRequests(PriorityItem<IRequest> pair)
301301
if (request.State is RequestState.Compleated or RequestState.Failed or RequestState.Cancelled)
302302
{
303303
request.Dispose();
304-
305304
if (request.SubsequentRequest != null)
306305
await SubsequentRequest(request);
307306
}
@@ -360,6 +359,12 @@ public bool TrySetIdle()
360359
return requests.All(x => x.Item.State == RequestState.Idle);
361360
}
362361

362+
/// <summary>
363+
/// Checks whether the <see cref="RequestHandler"/> has reached a final state and will process <see cref="IRequest"/> objects.
364+
/// </summary>
365+
/// <returns><c>true</c> if the hanlder is in a final state; otherwise, <c>false</c>.</returns>
366+
public bool HasCompleted() => _requestsChannel.Reader.Completion.IsCompleted;
367+
363368
/// <summary>
364369
/// Disposes the <see cref="RequestHandler"/> instance and canceling all ongoing tasks.
365370
/// </summary>
@@ -398,6 +403,7 @@ public override string ToString()
398403

399404
return sb.ToString();
400405
}
406+
401407
/// <summary>
402408
/// Attempts to remove the specified requests from the priority channel.
403409
/// </summary>

Requests/Requests.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
<PackageTags>async; channel; priority; request; parallel; </PackageTags>
1717
<RepositoryUrl>https://github.com/TypNull/Requests</RepositoryUrl>
1818
<PackageIcon>logo.png</PackageIcon>
19-
<Version>2.2.0</Version>
19+
<Version>2.2.1</Version>
2020
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2121
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2222
<PackageId>Shard.Requests</PackageId>
23-
<PackageReleaseNotes>Implement SubsequentRequest
24-
Fix RequestContainer GetRequests issue</PackageReleaseNotes>
23+
<PackageReleaseNotes>Implement support for overwriting SubsequentRequest property in the Request&lt;&gt; class</PackageReleaseNotes>
2524
</PropertyGroup>
2625

2726
<ItemGroup>

0 commit comments

Comments
 (0)