From 82ce7996d1141cbd63d3c0307519704bf22310c7 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 30 Jul 2024 11:12:31 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Erweiterung=20der=20ModelExtensions=20u?= =?UTF-8?q?m=20zus=C3=A4tzliche=20Subscribe-Methoden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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. --- .../Models/ModelExtensions.cs | 69 ++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs b/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs index 630c774..3c61b3a 100644 --- a/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs +++ b/src/WindreamHub.Legacy.Client/Models/ModelExtensions.cs @@ -1,8 +1,8 @@ using DigitalData.Core.Legacy.Client; using System; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; -using static System.Net.Mime.MediaTypeNames; namespace WindreamHub.Legacy.Client.Models { @@ -16,13 +16,78 @@ namespace WindreamHub.Legacy.Client.Models return new SimplifiedResponse(ok: message.IsSuccessStatusCode, status: message.StatusCode, data: data, error: err); } - public static async Task Subscribe(this Task> responseAsync, Action next, Action error) + public static async Task SubscribeAsync(this Task> responseAsync, Action next, Action error, CancellationToken cancellationToken = default) { + if (cancellationToken.IsCancellationRequested) + return; + var res = await responseAsync; if (res.Ok) next(res.Data); else error(res.Error); } + + public static void Subscribe(this Task> responseAsync, Action next, Action 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(this Task> responseAsync, int millisecondsDelay, Action next, Action 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 CreateSubscribeAsyncAction(this Task> responseAsync, Action next, Action 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(this Task> responseAsync, Action next, Action 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); + }); + } } } \ No newline at end of file