Skip to content

Commit

Permalink
Process job output using single channel
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Aug 7, 2022
1 parent 3d203cd commit 394c697
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Avalonia.Media;

namespace ScriptRunner.GUI.ViewModels;

Expand Down Expand Up @@ -188,6 +190,28 @@ private void ChangeStatus(RunningJobStatus status)

private static readonly Regex ConsoleSpecialCharsPattern = new Regex(@"\u001b\[[\d;]+\w?");

static readonly Channel<(RunningJobViewModel, string)> ch = Channel.CreateUnbounded<(RunningJobViewModel, string)>();

static RunningJobViewModel()
{
try
{
Dispatcher.UIThread.InvokeAsync(async () =>
{
while (await ch.Reader.WaitToReadAsync())
{
var (instance, newContent) = await ch.Reader.ReadAsync();
instance.CurrentRunOutput += newContent + Environment.NewLine;
instance.OutputIndex = instance.CurrentRunOutput.Length;
}
});
}
catch (Exception e)
{

}
}

private void AppendToOutput(string? s)
{
if (s != null)
Expand All @@ -198,12 +222,7 @@ private void AppendToOutput(string? s)
return;
}

Dispatcher.UIThread.Post(() =>
{

CurrentRunOutput += newContent + Environment.NewLine;
OutputIndex = CurrentRunOutput.Length;
});
ch.Writer.WriteAsync((this, s));
}
}

Expand Down

0 comments on commit 394c697

Please sign in to comment.