-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up most of razor code to code-behind files (#164)
I applied this for files where there was non-trivial logic Resolves #78
- Loading branch information
Showing
17 changed files
with
967 additions
and
892 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/Aspire.Dashboard/Components/Controls/LogViewer.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Concurrent; | ||
using Aspire.Dashboard.ConsoleLogs; | ||
using Aspire.Dashboard.Model; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Aspire.Dashboard.Components; | ||
public partial class LogViewer | ||
{ | ||
private static readonly AnsiParser s_ansiParser = new(); | ||
|
||
private readonly ConcurrentQueue<IEnumerable<LogEntry>> _preRenderQueue = new(); | ||
private bool _renderComplete; | ||
private IJSObjectReference? _jsModule; | ||
|
||
internal async Task ClearLogsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
if (_jsModule is not null) | ||
{ | ||
await _jsModule.InvokeVoidAsync("clearLogs", cancellationToken); | ||
} | ||
} | ||
|
||
private ValueTask WriteLogsToDomAsync(IEnumerable<LogEntry> logs) | ||
=> _jsModule is null ? ValueTask.CompletedTask : _jsModule.InvokeVoidAsync("addLogEntries", logs); | ||
|
||
internal async Task WatchLogsAsync(Func<IAsyncEnumerable<string[]>> watchMethod, LogEntryType logEntryType) | ||
{ | ||
string? parentTimestamp = null; | ||
Guid? parentId = null; | ||
int lineIndex = 0; | ||
AnsiParser.ParserState? residualState = null; | ||
|
||
await foreach (var logs in watchMethod()) | ||
{ | ||
var logEntries = new List<LogEntry>(logs.Length); | ||
foreach (var log in logs) | ||
{ | ||
var logEntry = LogEntry.Create(log, logEntryType); | ||
|
||
var conversionResult = s_ansiParser.ConvertToHtml(logEntry.Content, residualState); | ||
logEntry.Content = conversionResult.ConvertedText; | ||
residualState = conversionResult.ResidualState; | ||
|
||
if (logEntry.IsFirstLine) | ||
{ | ||
parentTimestamp = logEntry.Timestamp; | ||
parentId = logEntry.Id; | ||
lineIndex = 0; | ||
} | ||
else if (parentId.HasValue) | ||
{ | ||
logEntry.ParentTimestamp = parentTimestamp; | ||
logEntry.ParentId = parentId; | ||
logEntry.LineIndex = ++lineIndex; | ||
} | ||
logEntries.Add(logEntry); | ||
} | ||
|
||
await WriteLogsAsync(logEntries); | ||
} | ||
} | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
_jsModule ??= await JS.InvokeAsync<IJSObjectReference>("import", "/_content/Aspire.Dashboard/Components/Controls/LogViewer.razor.js"); | ||
|
||
while (_preRenderQueue.TryDequeue(out var logs)) | ||
{ | ||
await WriteLogsToDomAsync(logs); | ||
} | ||
_renderComplete = true; | ||
} | ||
} | ||
|
||
private async Task WriteLogsAsync(IEnumerable<LogEntry> logs) | ||
{ | ||
if (_renderComplete) | ||
{ | ||
await WriteLogsToDomAsync(logs); | ||
} | ||
else | ||
{ | ||
_preRenderQueue.Enqueue(logs); | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
try | ||
{ | ||
if (_jsModule is not null) | ||
{ | ||
await _jsModule.DisposeAsync(); | ||
} | ||
} | ||
catch (JSDisconnectedException) | ||
{ | ||
// Per https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-7.0#javascript-interop-calls-without-a-circuit | ||
// this is one of the calls that will fail if the circuit is disconnected, and we just need to catch the exception so it doesn't pollute the logs | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.