-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAsyncCounterStore.cs
61 lines (50 loc) · 1.69 KB
/
AsyncCounterStore.cs
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
59
60
61
using System.Collections.Immutable;
namespace Memento.Sample.Blazor.Stores;
public record AsyncCounterState {
public int Count { get; init; } = 0;
public bool IsLoading { get; init; } = false;
public ImmutableArray<int> Histories { get; init; } = [];
}
public enum StateChangedType {
CountUp,
Loading,
CountUpAsync,
SetCount,
CountUpWithAmount
}
public struct Test();
public class AsyncCounterStore() : Store<AsyncCounterState, StateChangedType>(() => new()) {
public void CountUp() {
Mutate(state => state with {
Count = state.Count + 1,
IsLoading = false,
Histories = [.. state.Histories, state.Count + 1],
}, StateChangedType.CountUp);
}
public async Task CountUpAsync() {
Mutate(state => state with { IsLoading = true, }, StateChangedType.Loading);
await Task.Delay(800);
Mutate(state => state with {
Count = state.Count + 1,
IsLoading = false,
Histories = [.. state.Histories, state.Count + 1],
}, StateChangedType.CountUp);
}
public void CountUpManyTimes(int count) {
for (var i = 0; i < count; i++) {
Mutate(state => state with {
Count = state.Count + 1,
}, StateChangedType.CountUpAsync);
}
}
public void SetCount(int count) {
Mutate(state => state with {
Count = count,
}, StateChangedType.SetCount);
}
public void CountUpWithAmount(int amount) {
Mutate(state => state with {
Count = state.Count + amount,
}, StateChangedType.CountUpWithAmount);
}
}