Skip to content

Commit dea8c3f

Browse files
committed
Added new capability to NetworkRequestManager to allow removal of queued requests
1 parent acdc1b1 commit dea8c3f

4 files changed

Lines changed: 105 additions & 7 deletions

File tree

src/MADE.Networking/Http/INetworkRequestManager.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ void AddOrUpdate<TRequest, TResponse, TErrorResponse>(
8181
Action<TErrorResponse> errorCallback)
8282
where TRequest : NetworkRequest;
8383

84+
/// <summary>
85+
/// Removes a network request from the queue.
86+
/// <para>
87+
/// If the request is no longer in the queue, this method does nothing.
88+
/// </para>
89+
/// </summary>
90+
/// <param name="request">The request to remove from the queue.</param>
91+
void Remove(INetworkRequest request);
92+
93+
/// <summary>
94+
/// Removes a network request from the queue by the registered key identifier.
95+
/// <para>
96+
/// If the request is no longer in the queue, this method does nothing.
97+
/// </para>
98+
/// </summary>
99+
/// <param name="key">The key corresponding to the network request to remove from the queue.</param>
100+
void RemoveByKey(string key);
101+
84102
/// <summary>
85103
/// Processes the current queue of network requests.
86104
/// </summary>

src/MADE.Networking/Http/NetworkRequestManager.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ namespace MADE.Networking.Http
99
using System.Linq;
1010
using System.Threading;
1111
using System.Threading.Tasks;
12-
1312
using MADE.Networking.Http.Requests;
1413
using MADE.Runtime;
15-
1614
using Timer = MADE.Threading.Timer;
1715

1816
/// <summary>
@@ -93,8 +91,8 @@ public void ProcessCurrentQueue()
9391
while (this.CurrentQueue.Count > 0)
9492
{
9593
if (this.CurrentQueue.TryRemove(
96-
this.CurrentQueue.FirstOrDefault().Key,
97-
out NetworkRequestCallback request))
94+
this.CurrentQueue.FirstOrDefault().Key,
95+
out NetworkRequestCallback request))
9896
{
9997
requestCallbacks.Add(request);
10098
}
@@ -164,7 +162,9 @@ public void AddOrUpdate<TRequest, TResponse, TErrorResponse>(
164162
where TRequest : NetworkRequest
165163
{
166164
var weakSuccessCallback = new WeakReferenceCallback(successCallback, typeof(TResponse));
167-
var weakErrorCallback = new WeakReferenceCallback(errorCallback, typeof(TErrorResponse));
165+
var weakErrorCallback = errorCallback == null
166+
? null
167+
: new WeakReferenceCallback(errorCallback, typeof(TErrorResponse));
168168
var requestCallback = new NetworkRequestCallback(request, weakSuccessCallback, weakErrorCallback);
169169

170170
this.CurrentQueue.AddOrUpdate(
@@ -173,6 +173,30 @@ public void AddOrUpdate<TRequest, TResponse, TErrorResponse>(
173173
(s, callback) => requestCallback);
174174
}
175175

176+
/// <summary>
177+
/// Removes a network request from the queue.
178+
/// <para>
179+
/// If the request is no longer in the queue, this method does nothing.
180+
/// </para>
181+
/// </summary>
182+
/// <param name="request">The request to remove from the queue.</param>
183+
public void Remove(INetworkRequest request)
184+
{
185+
RemoveByKey(request.Identifier.ToString());
186+
}
187+
188+
/// <summary>
189+
/// Removes a network request from the queue by the registered key identifier.
190+
/// <para>
191+
/// If the request is no longer in the queue, this method does nothing.
192+
/// </para>
193+
/// </summary>
194+
/// <param name="key">The key corresponding to the network request to remove from the queue.</param>
195+
public void RemoveByKey(string key)
196+
{
197+
this.CurrentQueue.TryRemove(key, out NetworkRequestCallback _);
198+
}
199+
176200
private static async Task ExecuteRequestsAsync(
177201
ConcurrentDictionary<string, NetworkRequestCallback> queue,
178202
NetworkRequestCallback requestCallback,
@@ -200,7 +224,7 @@ private static async Task ExecuteRequestsAsync(
200224
catch (Exception ex)
201225
{
202226
successCallback.Invoke(Activator.CreateInstance(successCallback.Type));
203-
errorCallback.Invoke(ex);
227+
errorCallback?.Invoke(ex);
204228
}
205229
}
206230

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace MADE.Networking.Http.Requests
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
/// <summary>
9+
/// Defines an interface for a basic network request.
10+
/// </summary>
11+
public interface INetworkRequest
12+
{
13+
/// <summary>
14+
/// Gets the identifier for the request.
15+
/// </summary>
16+
Guid Identifier { get; }
17+
18+
/// <summary>
19+
/// Gets or sets the URL for the request.
20+
/// </summary>
21+
string Url { get; set; }
22+
23+
/// <summary>
24+
/// Gets the headers for the request.
25+
/// </summary>
26+
Dictionary<string, string> Headers { get; }
27+
28+
/// <summary>
29+
/// Executes the network request.
30+
/// </summary>
31+
/// <typeparam name="TResponse">
32+
/// The type of object returned from the request.
33+
/// </typeparam>
34+
/// <param name="cancellationToken">
35+
/// The cancellation token.
36+
/// </param>
37+
/// <returns>
38+
/// Returns the response of the request as the specified type.
39+
/// </returns>
40+
Task<TResponse> ExecuteAsync<TResponse>(CancellationToken cancellationToken = default);
41+
42+
/// <summary>
43+
/// Executes the network request.
44+
/// </summary>
45+
/// <param name="expectedResponse">
46+
/// The type expected by the response of the request.
47+
/// </param>
48+
/// <param name="cancellationToken">
49+
/// The cancellation token.
50+
/// </param>
51+
/// <returns>
52+
/// Returns the response of the request as an object.
53+
/// </returns>
54+
Task<object> ExecuteAsync(Type expectedResponse, CancellationToken cancellationToken = default);
55+
}
56+
}

src/MADE.Networking/Http/Requests/NetworkRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace MADE.Networking.Http.Requests
1111
/// <summary>
1212
/// Defines the model for a network request.
1313
/// </summary>
14-
public abstract class NetworkRequest
14+
public abstract class NetworkRequest : INetworkRequest
1515
{
1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="NetworkRequest"/> class.

0 commit comments

Comments
 (0)