From 53b6f8b27f43ae3385e7296189b33d44d7a8a69f Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Fri, 17 Feb 2023 12:34:18 -0600 Subject: [PATCH] Remove crappy blazor class and add throttle to SSE --- Tesses.YouTubeDownloader.Server/Class1.cs | 48 ++++++++++++++++++- .../Tesses.YouTubeDownloader.Server.csproj | 8 ++-- Tesses.YouTubeDownloader/IBlazorContext.cs | 13 ----- Tesses.YouTubeDownloader/TYTDClient.cs | 22 +++------ .../Tesses.YouTubeDownloader.csproj | 6 +-- 5 files changed, 61 insertions(+), 36 deletions(-) delete mode 100644 Tesses.YouTubeDownloader/IBlazorContext.cs diff --git a/Tesses.YouTubeDownloader.Server/Class1.cs b/Tesses.YouTubeDownloader.Server/Class1.cs index 2e4d6ee..cb83c35 100644 --- a/Tesses.YouTubeDownloader.Server/Class1.cs +++ b/Tesses.YouTubeDownloader.Server/Class1.cs @@ -16,6 +16,7 @@ using Tesses.WebServer.Swagme; namespace Tesses.Extensions { + public static class Extensions { public static string Substring(this string value,string str) @@ -31,8 +32,37 @@ namespace Tesses.Extensions namespace Tesses.YouTubeDownloader.Server { + using Tesses.YouTubeDownloader; +internal class EventSender +{ + DateTime lastScan = DateTime.Now; + TimeSpan ts; + public EventSender(TimeSpan interval) + { + this.ts = interval; + } + public bool CanScan + { + get{ + return DateTime.Now - lastScan >= ts; + } + } + + public void SendEvent(Action action) + { + if(CanScan) + { + lastScan = DateTime.Now; + action(); + } + } + public void Sent() + { + lastScan = DateTime.Now; + } +} internal static class B64 { @@ -988,6 +1018,13 @@ internal static class B64 } public async Task Event(ServerContext ctx) { + double interval=0; + string intervalStr; + if(ctx.QueryParams.TryGetFirst("interval",out intervalStr)) + { + if(!double.TryParse(intervalStr,out interval)) interval=0; + } + IStorage storage=Downloader as IStorage; if(storage != null){ @@ -1010,7 +1047,10 @@ internal static class B64 p.Video=e.VideoInfo; evts.SendEvent(p); }; + + EventSender s = new EventSender(TimeSpan.FromSeconds(interval)); storage.VideoProgress += (sender,e)=>{ + bool wasFirst= first; ProgressItem p=new ProgressItem(); p.StartEvent=false; p.StopEvent=false; @@ -1022,7 +1062,13 @@ internal static class B64 p.Video=e.VideoInfo; first=false; - evts.SendEvent(p); + if(wasFirst || s.CanScan) + { + s.Sent(); + evts.SendEvent(p); + } + + }; storage.VideoFinished +=(sender,e)=>{ ProgressItem p=new ProgressItem(); diff --git a/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj b/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj index 10666a9..536e20f 100644 --- a/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj +++ b/Tesses.YouTubeDownloader.Server/Tesses.YouTubeDownloader.Server.csproj @@ -16,11 +16,11 @@ Tesses.YouTubeDownloader.Server Mike Nolan Tesses - 2.0.1 - 2.0.1 - 2.0.1 + 2.0.1.1 + 2.0.1.1 + 2.0.1.1 Adds WebServer to TYTD - LGPL-3.0-only + GPL-3.0-only true YoutubeExplode, YouTube, YouTubeDownloader https://gitlab.tesses.cf/tesses50/tytd diff --git a/Tesses.YouTubeDownloader/IBlazorContext.cs b/Tesses.YouTubeDownloader/IBlazorContext.cs deleted file mode 100644 index c011789..0000000 --- a/Tesses.YouTubeDownloader/IBlazorContext.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace Tesses.YouTubeDownloader -{ - public interface IBlazorContext - { - public void StartEvents(Action evt); - - public void StopEvents(); - - - } -} \ No newline at end of file diff --git a/Tesses.YouTubeDownloader/TYTDClient.cs b/Tesses.YouTubeDownloader/TYTDClient.cs index 622b346..9e8684f 100644 --- a/Tesses.YouTubeDownloader/TYTDClient.cs +++ b/Tesses.YouTubeDownloader/TYTDClient.cs @@ -211,6 +211,7 @@ internal class SegmentedHttpStream : Stream return v.Major >= 2; } string url; + public string Url {get{return url;}} public TYTDClient(string url) { client=new HttpClient(); @@ -413,7 +414,7 @@ internal class SegmentedHttpStream : Stream } return false; } - private async Task> GetQueueListAsync() + public async Task> GetQueueListAsync() { try{ @@ -426,7 +427,7 @@ internal class SegmentedHttpStream : Stream return new List<(SavedVideo video,Resolution resolution)>(); } - private async Task GetProgressAsync() + public async Task GetProgressAsync() { try{ @@ -552,8 +553,7 @@ internal class SegmentedHttpStream : Stream _error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex)); } } - public IBlazorContext BlazorContext {get;set;} = null; - public void ResetEvents() + public void ResetEvents() { if(src != null) { src.Cancel(); @@ -668,7 +668,7 @@ internal class SegmentedHttpStream : Stream } if(item.BellEvent) { - + bell=true; } progress.Length = item.Length; progress.ProgressRaw=item.Percent; @@ -713,17 +713,9 @@ internal class SegmentedHttpStream : Stream } private void _startEventStream() { - if(BlazorContext != null) - { - BlazorContext.StartEvents((evt)=>{ - sse_Event(this,new SSEEvent(evt)); - }); - src=new CancellationTokenSource(); - hadBeenListeningToEvents=true; - src.Token.Register(BlazorContext.StopEvents); - }else{ + Task.Run(_startEventStreamAsync).Wait(0); - } + } public ExtraData GetExtraDataOnce() { diff --git a/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj b/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj index 4e30f3d..65df3ec 100644 --- a/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj +++ b/Tesses.YouTubeDownloader/Tesses.YouTubeDownloader.csproj @@ -7,9 +7,9 @@ Tesses.YouTubeDownloader Mike Nolan Tesses - 2.0.2.2 - 2.0.2.2 - 2.0.2.2 + 2.0.2.3 + 2.0.2.3 + 2.0.2.3 A YouTube Downloader GPL-3.0-only true