/
Counter.razor
58 lines (43 loc) · 1.36 KB
/
Counter.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@page "/counter"
@implements IDisposable
@using System.Linq;
@using System.Threading
@using System.Reactive.Linq;
<h1>Counter</h1>
<p>Current count: @State?.Count</p>
<button class="btn btn-primary" @onclick="CounterService.Increment">Increment</button>
<button class="btn btn-primary" @onclick="CounterService.Decrement">Decrement</button>
<h4 class="mt-2 mb-2">Exception handling</h4>
<button class="btn mb-2 btn-warning" @onclick="ThrowException">Throw Exception</button>
<pre>@ExceptionInfo</pre>
@code {
[Inject] ICounterService CounterService { get; set; }
private CounterState State { get; set; }
private IDisposable StateSubscription { get; set; }
private string ExceptionInfo { get; set; }
protected override void OnInitialized()
{
var asyncState = CounterService.SubscribeAsync();
var state = asyncState.ToObservable();
StateSubscription = state.Subscribe(async state =>
{
State = state;
await InvokeAsync(() => StateHasChanged());
});
}
private async Task ThrowException()
{
try
{
await CounterService.ThrowException();
}
catch (Grpc.Core.RpcException ex)
{
ExceptionInfo = $"RpcException: {ex.Status}";
}
}
public void Dispose()
{
StateSubscription?.Dispose();
}
}