@page "/myorders/{orderId:int}" @using System.Threading @inject HttpClient HttpClient @implements IDisposable @attribute [Authorize]
@if (invalidOrder) {

Nope

Sorry, this order could not be loaded.

} else if (orderWithStatus == null) { Loading... } else {

Order placed @orderWithStatus.Order.CreatedTime.ToLongDateString()

Status: @orderWithStatus.StatusText

}
@functions { [Parameter] int OrderId { get; set; } OrderWithStatus orderWithStatus; bool invalidOrder; CancellationTokenSource pollingCancellationToken; protected override void OnParametersSet() { // If we were already polling for a different order, stop doing so pollingCancellationToken?.Cancel(); // Start a new poll loop PollForUpdates(); } void IDisposable.Dispose() { pollingCancellationToken?.Cancel(); } private async void PollForUpdates() { pollingCancellationToken = new CancellationTokenSource(); while (!pollingCancellationToken.IsCancellationRequested) { try { invalidOrder = false; orderWithStatus = await HttpClient.GetJsonAsync($"orders/{OrderId}"); } catch (Exception ex) { invalidOrder = true; pollingCancellationToken.Cancel(); Console.Error.WriteLine(ex); } StateHasChanged(); await Task.Delay(4000); } } }