@using RobotNet10.ScriptEditor.Clients
@using Microsoft.JSInterop
@inject ConsoleHubClient HubClient
@inject IJSRuntime JSRuntime
@implements IAsyncDisposable
@code {
private bool _autoScroll = true;
private DotNetObjectReference? _objRef;
protected override async Task OnInitializedAsync()
{
// Đăng ký events từ HubClient
HubClient.ErrorReceived += OnErrorReceived;
HubClient.InfoReceived += OnInfoReceived;
HubClient.WarningReceived += OnWarningReceived;
// Kết nối đến hub nếu chưa connected
if (!HubClient.IsConnected)
{
await HubClient.StartAsync();
}
// Đảm bảo đã connected trước khi đăng ký
if (HubClient.IsConnected)
{
// Đăng ký nhận tất cả console messages
await HubClient.RegisterAllAsync();
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_objRef = DotNetObjectReference.Create(this);
await JSRuntime.InvokeVoidAsync("robotnet.console.init", _objRef);
}
}
private void OnErrorReceived(string message)
{
_ = JSRuntime.InvokeVoidAsync("robotnet.console.addMessage", "ERROR", message, _autoScroll);
}
private void OnInfoReceived(string message)
{
_ = JSRuntime.InvokeVoidAsync("robotnet.console.addMessage", "INFO", message, _autoScroll);
}
private void OnWarningReceived(string message)
{
_ = JSRuntime.InvokeVoidAsync("robotnet.console.addMessage", "WARN", message, _autoScroll);
}
private async Task ClearLogs()
{
await JSRuntime.InvokeVoidAsync("robotnet.console.clear");
}
private void ToggleAutoScroll()
{
_autoScroll = !_autoScroll;
}
public async ValueTask DisposeAsync()
{
HubClient.ErrorReceived -= OnErrorReceived;
HubClient.InfoReceived -= OnInfoReceived;
HubClient.WarningReceived -= OnWarningReceived;
_objRef?.Dispose();
}
}