deprived-main-website/OpenBirchWebsite/Services/OpenBirchConsole.cs

45 lines
1.2 KiB
C#

using Microsoft.JSInterop;
namespace OpenBirchWebsite.Services
{
public class OpenBirchConsole(IJSRuntime js) : IDisposable
{
public List<ConsoleLine> history = new();
private readonly IJSRuntime js = js;
public static event Action<List<ConsoleLine>> OnLinesChanged;
public Task pushCommand(string command)
{
history.Add(new ConsoleLine() { source = ConsoleSource.User, text = command });
return ExecuteCommand(command);
}
private async Task ExecuteCommand(string command)
{
string result = await js.InvokeAsync<string>("runEval", command);
history.Add(new ConsoleLine() { source = ConsoleSource.OpenBirch, text = result });
OnLinesChanged?.Invoke(history);
}
public void Dispose()
{
// The following prevents derived types that introduce a
// finalizer from needing to re-implement IDisposable.
GC.SuppressFinalize(this);
}
}
public struct ConsoleLine
{
public ConsoleSource source;
public string text;
}
public enum ConsoleSource
{
User, // From the user
OpenBirch, // Guess
}
}