Skip to content

Commit 64df96c

Browse files
authored
Stop polling once pizza is delivered (#271)
1 parent 270bdf4 commit 64df96c

File tree

20 files changed

+112
-22
lines changed

20 files changed

+112
-22
lines changed

docs/03-show-order-status.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,24 @@ Now you can implement the polling. Update your `@code` block as follows:
259259
{
260260
invalidOrder = false;
261261
orderWithStatus = await HttpClient.GetFromJsonAsync<OrderWithStatus>($"orders/{OrderId}");
262+
StateHasChanged();
263+
264+
if (orderWithStatus.IsDelivered)
265+
{
266+
pollingCancellationToken.Cancel();
267+
}
268+
else
269+
{
270+
await Task.Delay(4000);
271+
}
262272
}
263273
catch (Exception ex)
264274
{
265275
invalidOrder = true;
266276
pollingCancellationToken.Cancel();
267277
Console.Error.WriteLine(ex);
278+
StateHasChanged();
268279
}
269-
270-
StateHasChanged();
271-
272-
await Task.Delay(4000);
273280
}
274281
}
275282
}
@@ -280,7 +287,7 @@ The code is a bit intricate, so be sure to go through it carefully to understand
280287
* This uses `OnParametersSet` instead of `OnInitialized` or `OnInitializedAsync`. `OnParametersSet` is another component lifecycle method, and it fires when the component is first instantiated *and* any time its parameters change value. If the user clicks a link directly from `myorders/2` to `myorders/3`, the framework will retain the `OrderDetails` instance and simply update its `OrderId` parameter in place.
281288
* As it happens, we haven't provided any links from one "my orders" screen to another, so the scenario never occurs in this application, but it's still the right lifecycle method to use in case we change the navigation rules in the future.
282289
* We're using an `async void` method to represent the polling. This method runs for arbitrarily long, even while other methods run. `async void` methods have no way to report exceptions upstream to callers (because typically the callers have already finished), so it's important to use `try/catch` and do something meaningful with any exceptions that may occur.
283-
* We're using `CancellationTokenSource` as a way of signalling when the polling should stop. Currently it only stops if there's an exception, but we'll add another stopping condition later.
290+
* We're using `CancellationTokenSource` as a way of signalling when the polling should stop. Currently it stops if there's an exception, or once the order is delivered.
284291
* We need to call `StateHasChanged` to tell Blazor that the component's data has (possibly) changed. The framework will then re-render the component. There's no way that the framework could know when to re-render your component otherwise, because it doesn't know about your polling logic.
285292

286293
## Rendering the order details

save-points/00-get-started/BlazingPizza.Shared/OrderWithStatus.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class OrderWithStatus
1313

1414
public string StatusText { get; set; }
1515

16+
public bool IsDelivered => StatusText == "Delivered";
17+
1618
public List<Marker> MapMarkers { get; set; }
1719

1820
public static OrderWithStatus FromOrder(Order order)

save-points/01-Components-and-layout/BlazingPizza.Shared/OrderWithStatus.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class OrderWithStatus
1313

1414
public string StatusText { get; set; }
1515

16+
public bool IsDelivered => StatusText == "Delivered";
17+
1618
public List<Marker> MapMarkers { get; set; }
1719

1820
public static OrderWithStatus FromOrder(Order order)

save-points/02-customize-a-pizza/BlazingPizza.Shared/OrderWithStatus.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class OrderWithStatus
1313

1414
public string StatusText { get; set; }
1515

16+
public bool IsDelivered => StatusText == "Delivered";
17+
1618
public List<Marker> MapMarkers { get; set; }
1719

1820
public static OrderWithStatus FromOrder(Order order)

save-points/03-show-order-status/BlazingPizza.Client/Pages/OrderDetails.razor

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,24 @@
5858
{
5959
invalidOrder = false;
6060
orderWithStatus = await HttpClient.GetFromJsonAsync<OrderWithStatus>($"orders/{OrderId}");
61+
StateHasChanged();
62+
63+
if (orderWithStatus.IsDelivered)
64+
{
65+
pollingCancellationToken.Cancel();
66+
}
67+
else
68+
{
69+
await Task.Delay(4000);
70+
}
6171
}
6272
catch (Exception ex)
6373
{
6474
invalidOrder = true;
6575
pollingCancellationToken.Cancel();
6676
Console.Error.WriteLine(ex);
77+
StateHasChanged();
6778
}
68-
69-
StateHasChanged();
70-
71-
await Task.Delay(4000);
7279
}
7380
}
7481

save-points/03-show-order-status/BlazingPizza.Shared/OrderWithStatus.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class OrderWithStatus
1313

1414
public string StatusText { get; set; }
1515

16+
public bool IsDelivered => StatusText == "Delivered";
17+
1618
public List<Marker> MapMarkers { get; set; }
1719

1820
public static OrderWithStatus FromOrder(Order order)

save-points/04-refactor-state-management/BlazingPizza.Client/Pages/OrderDetails.razor

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,24 @@
5858
{
5959
invalidOrder = false;
6060
orderWithStatus = await HttpClient.GetFromJsonAsync<OrderWithStatus>($"orders/{OrderId}");
61+
StateHasChanged();
62+
63+
if (orderWithStatus.IsDelivered)
64+
{
65+
pollingCancellationToken.Cancel();
66+
}
67+
else
68+
{
69+
await Task.Delay(4000);
70+
}
6171
}
6272
catch (Exception ex)
6373
{
6474
invalidOrder = true;
6575
pollingCancellationToken.Cancel();
6676
Console.Error.WriteLine(ex);
77+
StateHasChanged();
6778
}
68-
69-
StateHasChanged();
70-
71-
await Task.Delay(4000);
7279
}
7380
}
7481

save-points/04-refactor-state-management/BlazingPizza.Shared/OrderWithStatus.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class OrderWithStatus
1313

1414
public string StatusText { get; set; }
1515

16+
public bool IsDelivered => StatusText == "Delivered";
17+
1618
public List<Marker> MapMarkers { get; set; }
1719

1820
public static OrderWithStatus FromOrder(Order order)

save-points/05-checkout-with-validation/BlazingPizza.Client/Pages/OrderDetails.razor

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,24 @@
5858
{
5959
invalidOrder = false;
6060
orderWithStatus = await HttpClient.GetFromJsonAsync<OrderWithStatus>($"orders/{OrderId}");
61+
StateHasChanged();
62+
63+
if (orderWithStatus.IsDelivered)
64+
{
65+
pollingCancellationToken.Cancel();
66+
}
67+
else
68+
{
69+
await Task.Delay(4000);
70+
}
6171
}
6272
catch (Exception ex)
6373
{
6474
invalidOrder = true;
6575
pollingCancellationToken.Cancel();
6676
Console.Error.WriteLine(ex);
77+
StateHasChanged();
6778
}
68-
69-
StateHasChanged();
70-
71-
await Task.Delay(4000);
7279
}
7380
}
7481

save-points/05-checkout-with-validation/BlazingPizza.Shared/OrderWithStatus.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class OrderWithStatus
1313

1414
public string StatusText { get; set; }
1515

16+
public bool IsDelivered => StatusText == "Delivered";
17+
1618
public List<Marker> MapMarkers { get; set; }
1719

1820
public static OrderWithStatus FromOrder(Order order)

0 commit comments

Comments
 (0)