summaryrefslogtreecommitdiff
path: root/Pages/Component/LinkAction.razor
blob: ee6d189981498a6de4834b5be89f3e29c6563a64 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
@using System.Timers
@implements IDisposable
@inject IJSRuntime js

<a
    class=@(isLoading || errorTimer.Enabled ? "disabled" : "")
    @attributes=Attributes
    @onclick=Trigger>

    @if(errorTimer.Enabled) {
        @("Error!")
    } else if(isLoading) {
        @(LoadingText + "...")
    } else {
        @Text
    }
</a>

@code {
    [Parameter]
    public Func<Task> OnClick { get; set; }

    [Parameter]
    public string Text{ get; set; }

    [Parameter]
    public string LoadingText { get; set; }

    private Dictionary<string, object?> Attributes => new() {
        ["disabled"] = isLoading,
    };

    private Timer loadingTimer;
    private Timer errorTimer;

    private object triggerLock = new();
    private bool   isRunning   = false;
    private bool   isLoading   = false;

    protected override void OnInitialized() {
        loadingTimer = new(50) {
            AutoReset = false
        };
        loadingTimer.Elapsed += LoadingTimerElapsed;

        errorTimer = new(1000) {
            AutoReset = false
        };
        errorTimer.Elapsed += ErrorTimerElapsed;
    }

    // Most link-click actions (e.g. triggering dialogs, page
    // navigation, etc) complete very quickly (under 50ms).
    // Triggering a render cycle to show a loading animation is
    // usually pointless as doing so would waste resources and
    // appear less seemless to the user who will see brief
    // flickering while the browser quickly recalculates it's
    // layout twice. To that end, a timer is used to only render
    // the loading animation on the link if the delegate task
    // takes longer than 50ms
    private void LoadingTimerElapsed(object? sender, EventArgs e) {
        lock(triggerLock) {
            if(isRunning)
                isLoading = true;
            InvokeAsync(() => StateHasChanged());
        }
    }

    private void ErrorTimerElapsed(object? sender, EventArgs e) =>
        InvokeAsync(() => StateHasChanged());

    private async Task Trigger() {
        lock(triggerLock) {
            if(isRunning)
                return;
            isRunning = true;
            loadingTimer.Start();
        }

        bool error = false;

        try {
            await Task.Run(OnClick);
        } catch {
            error = true;
        } finally {
            lock(triggerLock) {
                loadingTimer.Stop();
                isRunning = false;
                isLoading = false;
            }
        }

        if(error)
            errorTimer.Start();

        await InvokeAsync(() => StateHasChanged());
    }

    public void Dispose() =>
        loadingTimer.Dispose();
}