From e27585a504478b2f980fb8a88ac8e46f79c3182b Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Sat, 2 Apr 2022 00:23:58 +0200 Subject: [PATCH] Fixed previewer: first document refresh sometimes does not work --- QuestPDF.Previewer/CommunicationService.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/QuestPDF.Previewer/CommunicationService.cs b/QuestPDF.Previewer/CommunicationService.cs index 84050a6..0a78ef5 100644 --- a/QuestPDF.Previewer/CommunicationService.cs +++ b/QuestPDF.Previewer/CommunicationService.cs @@ -11,7 +11,7 @@ class CommunicationService { public static CommunicationService Instance { get; } = new (); - public event Action> OnDocumentRefreshed; + public event Action>? OnDocumentRefreshed; private WebApplication? Application { get; set; } @@ -31,8 +31,8 @@ class CommunicationService builder.Services.AddLogging(x => x.ClearProviders()); Application = builder.Build(); - Application.MapGet("ping", () => Results.Ok()); - Application.MapGet("version", () => Results.Json(GetType().Assembly.GetName().Version)); + Application.MapGet("ping", HandlePing); + Application.MapGet("version", HandleVersion); Application.MapPost("update/preview", HandleUpdatePreview); return Application.RunAsync($"http://localhost:{port}/"); @@ -44,6 +44,18 @@ class CommunicationService await Application.DisposeAsync(); } + private async Task HandlePing() + { + return OnDocumentRefreshed == null + ? Results.StatusCode(StatusCodes.Status503ServiceUnavailable) + : Results.Ok(); + } + + private async Task HandleVersion() + { + return Results.Json(GetType().Assembly.GetName().Version); + } + private async Task HandleUpdatePreview(HttpRequest request) { var command = JsonSerializer.Deserialize(request.Form["command"], JsonSerializerOptions);