diff --git a/src/SharpIDE.Godot/Features/Build/BuildPanel.cs b/src/SharpIDE.Godot/Features/Build/BuildPanel.cs index 8e0c0b6..830fad4 100644 --- a/src/SharpIDE.Godot/Features/Build/BuildPanel.cs +++ b/src/SharpIDE.Godot/Features/Build/BuildPanel.cs @@ -1,3 +1,4 @@ +using System.Threading.Channels; using GDExtensionBindgen; using Godot; @@ -6,28 +7,26 @@ namespace SharpIDE.Godot.Features.Build; public partial class BuildPanel : Control { private Terminal _terminal = null!; - private Task _writeTask = Task.CompletedTask; + private ChannelReader? _buildOutputChannelReader; public override void _Ready() { _terminal = new Terminal(GetNode("%Terminal")); Singletons.BuildService.BuildStarted += OnBuildStarted; } + public override void _Process(double delta) + { + if (_buildOutputChannelReader is null) return; + // TODO: Buffer and write once per frame? Investigate if godot-xterm already buffers internally + while (_buildOutputChannelReader.TryRead(out var str)) + { + _terminal.Write(str); + } + } + private async Task OnBuildStarted() { - if (_writeTask.IsCompleted is not true) - { - // If the write task is already running, just clear the terminal - we reuse the channel for the build output 🤷‍♂️ - await this.InvokeAsync(() => _terminal.Clear()); - return; - } - _writeTask = Task.GodotRun(async () => - { - await this.InvokeAsync(() => _terminal.Clear()); - await foreach (var str in Singletons.BuildService.BuildTextWriter.ConsoleChannel.Reader.ReadAllAsync().ConfigureAwait(false)) - { - await this.InvokeAsync(() => _terminal.Write(str)); - } - }); + await this.InvokeAsync(() => _terminal.Clear()); + _buildOutputChannelReader ??= Singletons.BuildService.BuildTextWriter.ConsoleChannel.Reader; } } \ No newline at end of file