feat: Erweiterung der ModelExtensions um zusätzliche Subscribe-Methoden
- Hinzugefügt: `SubscribeAsync` Methode für asynchrone Verarbeitung mit Abbruch-Token. - Hinzugefügt: Überlastung von `Subscribe` Methode für sofortige Ausführung und wiederholte Ausführung mit Delay. - Hinzugefügt: Methoden zur Erstellung von `Subscribe`-Aktionen mit Unterstützung für Abbruch-Token.
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
using DigitalData.Core.Legacy.Client;
|
using DigitalData.Core.Legacy.Client;
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using static System.Net.Mime.MediaTypeNames;
|
|
||||||
|
|
||||||
namespace WindreamHub.Legacy.Client.Models
|
namespace WindreamHub.Legacy.Client.Models
|
||||||
{
|
{
|
||||||
@@ -16,13 +16,78 @@ namespace WindreamHub.Legacy.Client.Models
|
|||||||
return new SimplifiedResponse<TData, TError>(ok: message.IsSuccessStatusCode, status: message.StatusCode, data: data, error: err);
|
return new SimplifiedResponse<TData, TError>(ok: message.IsSuccessStatusCode, status: message.StatusCode, data: data, error: err);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task Subscribe<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error)
|
public static async Task SubscribeAsync<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
return;
|
||||||
|
|
||||||
var res = await responseAsync;
|
var res = await responseAsync;
|
||||||
if (res.Ok)
|
if (res.Ok)
|
||||||
next(res.Data);
|
next(res.Data);
|
||||||
else
|
else
|
||||||
error(res.Error);
|
error(res.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Subscribe<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Task.Run(async () =>
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var res = await responseAsync;
|
||||||
|
if (res.Ok)
|
||||||
|
next(res.Data);
|
||||||
|
else
|
||||||
|
error?.Invoke(res.Error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Subscribe<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, int millisecondsDelay, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Task.Run(async () =>
|
||||||
|
{
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
var res = await responseAsync;
|
||||||
|
if (res.Ok)
|
||||||
|
next(res.Data);
|
||||||
|
else
|
||||||
|
error?.Invoke(res.Error);
|
||||||
|
|
||||||
|
await Task.Delay(millisecondsDelay, cancellationToken);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Func<Task> CreateSubscribeAsyncAction<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return async () =>
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var res = await responseAsync;
|
||||||
|
if (res.Ok)
|
||||||
|
next(res.Data);
|
||||||
|
else
|
||||||
|
error?.Invoke(res.Error);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Action CreateSubscribeAction<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return () => Task.Run(async () =>
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var res = await responseAsync;
|
||||||
|
if (res.Ok)
|
||||||
|
next(res.Data);
|
||||||
|
else
|
||||||
|
error?.Invoke(res.Error);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user