1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-22 08:42:44 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web/Shared/InstanceLog.razor

73 lines
1.8 KiB
Plaintext

@inherits PhantomComponent
@using Phantom.Server.Services.Instances
@using Phantom.Utils.Collections
@using Phantom.Utils.Events
@using System.Diagnostics
@implements IDisposable
@inject IJSRuntime Js;
@inject InstanceLogManager InstanceLogManager
<div id="log" class="font-monospace mb-3">
@foreach (var line in instanceLogs.EnumerateLast(uint.MaxValue)) {
<p>@(new MarkupString(line))</p>
}
</div>
@code {
[Parameter, EditorRequired]
public Guid InstanceGuid { get; set; }
private IJSObjectReference? PageJs { get; set; }
private EventSubscribers<RingBuffer<string>> instanceLogsSubs = null!;
private RingBuffer<string> instanceLogs = null!;
private readonly Stopwatch recheckPermissionsStopwatch = Stopwatch.StartNew();
protected override void OnInitialized() {
instanceLogsSubs = InstanceLogManager.GetSubs(InstanceGuid);
instanceLogsSubs.Subscribe(this, buffer => {
instanceLogs = buffer;
InvokeAsyncChecked(RefreshLog);
});
}
protected override async Task OnAfterRenderAsync(bool firstRender) {
if (firstRender) {
PageJs = await Js.InvokeAsync<IJSObjectReference>("import", "./Shared/InstanceLog.razor.js");
await RecheckPermissions();
StateHasChanged();
await PageJs.InvokeVoidAsync("initLog");
}
}
private async Task RefreshLog() {
if (recheckPermissionsStopwatch.Elapsed > TimeSpan.FromSeconds(2)) {
await RecheckPermissions();
}
StateHasChanged();
if (PageJs != null) {
await PageJs.InvokeVoidAsync("scrollLog");
}
}
private async Task RecheckPermissions() {
recheckPermissionsStopwatch.Restart();
if (!await CheckPermission(Permission.ViewInstanceLogs)) {
await Task.Yield();
Dispose();
instanceLogs = new RingBuffer<string>(0);
}
}
public void Dispose() {
instanceLogsSubs.Unsubscribe(this);
}
}