Remove crappy blazor class and add throttle to SSE

This commit is contained in:
Mike Nolan 2023-02-17 12:34:18 -06:00
parent 15cf9a6606
commit 53b6f8b27f
5 changed files with 61 additions and 36 deletions

View File

@ -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;
if(wasFirst || s.CanScan)
{
s.Sent();
evts.SendEvent(p);
}
};
storage.VideoFinished +=(sender,e)=>{
ProgressItem p=new ProgressItem();

View File

@ -16,11 +16,11 @@
<PackageId>Tesses.YouTubeDownloader.Server</PackageId>
<Author>Mike Nolan</Author>
<Company>Tesses</Company>
<Version>2.0.1</Version>
<AssemblyVersion>2.0.1</AssemblyVersion>
<FileVersion>2.0.1</FileVersion>
<Version>2.0.1.1</Version>
<AssemblyVersion>2.0.1.1</AssemblyVersion>
<FileVersion>2.0.1.1</FileVersion>
<Description>Adds WebServer to TYTD</Description>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>YoutubeExplode, YouTube, YouTubeDownloader</PackageTags>
<RepositoryUrl>https://gitlab.tesses.cf/tesses50/tytd</RepositoryUrl>

View File

@ -1,13 +0,0 @@
using System;
namespace Tesses.YouTubeDownloader
{
public interface IBlazorContext
{
public void StartEvents(Action<string> evt);
public void StopEvents();
}
}

View File

@ -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<IReadOnlyList<(SavedVideo Video, Resolution Resolution)>> GetQueueListAsync()
public async Task<IReadOnlyList<(SavedVideo Video, Resolution Resolution)>> GetQueueListAsync()
{
try{
@ -426,7 +427,7 @@ internal class SegmentedHttpStream : Stream
return new List<(SavedVideo video,Resolution resolution)>();
}
private async Task<SavedVideoProgress> GetProgressAsync()
public async Task<SavedVideoProgress> GetProgressAsync()
{
try{
@ -552,7 +553,6 @@ internal class SegmentedHttpStream : Stream
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
}
}
public IBlazorContext BlazorContext {get;set;} = null;
public void ResetEvents()
{
if(src != null) {
@ -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()
{

View File

@ -7,9 +7,9 @@
<PackageId>Tesses.YouTubeDownloader</PackageId>
<Author>Mike Nolan</Author>
<Company>Tesses</Company>
<Version>2.0.2.2</Version>
<AssemblyVersion>2.0.2.2</AssemblyVersion>
<FileVersion>2.0.2.2</FileVersion>
<Version>2.0.2.3</Version>
<AssemblyVersion>2.0.2.3</AssemblyVersion>
<FileVersion>2.0.2.3</FileVersion>
<Description>A YouTube Downloader</Description>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>