| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="BusyService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using MyNet.Observable; |
| | | 12 | | using MyNet.UI.Loading.Models; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.Loading; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Default <see cref="IBusyService"/> implementation with nested scopes and cancellation support. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class BusyService : ObservableObject, IBusyService |
| | | 20 | | { |
| | 480 | 21 | | private readonly Lock _gate = new(); |
| | 480 | 22 | | private readonly Stack<BusyStackEntry> _stack = new(); |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public bool IsBusy |
| | | 26 | | { |
| | | 27 | | get |
| | 81 | 28 | | { |
| | | 29 | | lock (_gate) |
| | 81 | 30 | | return _stack.Count > 0; |
| | 81 | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public IBusy? CurrentBusy |
| | | 36 | | { |
| | | 37 | | get |
| | 9 | 38 | | { |
| | | 39 | | lock (_gate) |
| | 9 | 40 | | return _stack.Count > 0 ? _stack.Peek().Busy : null; |
| | 9 | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public async Task RunAsync<TBusy>(Func<TBusy, CancellationToken, Task> action, CancellationToken cancellationToken = |
| | | 46 | | where TBusy : IBusy, new() |
| | | 47 | | { |
| | 102 | 48 | | ArgumentNullException.ThrowIfNull(action); |
| | | 49 | | |
| | 102 | 50 | | using var scope = BeginScope<TBusy>(cancellationToken); |
| | 102 | 51 | | await action((TBusy)scope.Busy, scope.CancellationToken).ConfigureAwait(false); |
| | 93 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public async Task<TResult> RunAsync<TBusy, TResult>(Func<TBusy, CancellationToken, Task<TResult>> action, Cancellati |
| | | 56 | | where TBusy : IBusy, new() |
| | | 57 | | { |
| | 0 | 58 | | ArgumentNullException.ThrowIfNull(action); |
| | | 59 | | |
| | 0 | 60 | | using var scope = BeginScope<TBusy>(cancellationToken); |
| | 0 | 61 | | return await action((TBusy)scope.Busy, scope.CancellationToken).ConfigureAwait(false); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public IBusyScope Begin<TBusy>(CancellationToken cancellationToken = default) |
| | | 66 | | where TBusy : IBusy, new() |
| | 6 | 67 | | => BeginScope<TBusy>(cancellationToken); |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public TBusy? GetCurrent<TBusy>() |
| | | 71 | | where TBusy : class, IBusy |
| | 54 | 72 | | { |
| | | 73 | | lock (_gate) |
| | 54 | 74 | | return _stack.Count > 0 ? _stack.Peek().Busy as TBusy : null; |
| | 54 | 75 | | } |
| | | 76 | | |
| | | 77 | | internal void Pop(BusyScope scope) |
| | | 78 | | { |
| | 108 | 79 | | ArgumentNullException.ThrowIfNull(scope); |
| | | 80 | | |
| | | 81 | | lock (_gate) |
| | | 82 | | { |
| | 108 | 83 | | if (_stack.Count == 0 || !ReferenceEquals(_stack.Peek().Scope, scope)) |
| | 0 | 84 | | return; |
| | | 85 | | |
| | 108 | 86 | | var entry = _stack.Pop(); |
| | 108 | 87 | | entry.DisposeResources(); |
| | 108 | 88 | | RaiseStateChanged(); |
| | 108 | 89 | | } |
| | 108 | 90 | | } |
| | | 91 | | |
| | | 92 | | private static CancellationToken Bind<TBusy>(TBusy busy, CancellationToken cancellationToken, out CancellationTokenS |
| | | 93 | | where TBusy : IBusy |
| | | 94 | | { |
| | 108 | 95 | | ArgumentNullException.ThrowIfNull(busy); |
| | | 96 | | |
| | 108 | 97 | | if (busy is not Busy bindable) |
| | | 98 | | { |
| | 0 | 99 | | linkedSource = null; |
| | 0 | 100 | | return cancellationToken; |
| | | 101 | | } |
| | | 102 | | |
| | 108 | 103 | | linkedSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 108 | 104 | | bindable.BindCancellation(linkedSource); |
| | 108 | 105 | | return linkedSource.Token; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private BusyScope BeginScope<TBusy>(CancellationToken cancellationToken) |
| | | 109 | | where TBusy : IBusy, new() |
| | | 110 | | { |
| | 108 | 111 | | var busy = new TBusy(); |
| | 108 | 112 | | var token = Bind(busy, cancellationToken, out var linkedSource); |
| | 108 | 113 | | var scope = new BusyScope(this, busy, token); |
| | | 114 | | |
| | | 115 | | lock (_gate) |
| | | 116 | | { |
| | 108 | 117 | | _stack.Push(new(busy, linkedSource, scope)); |
| | 108 | 118 | | RaiseStateChanged(); |
| | 108 | 119 | | } |
| | | 120 | | |
| | 108 | 121 | | return scope; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private void RaiseStateChanged() |
| | | 125 | | { |
| | 216 | 126 | | NotifyPropertyChanged(nameof(IsBusy)); |
| | 216 | 127 | | NotifyPropertyChanged(nameof(CurrentBusy)); |
| | 216 | 128 | | } |
| | | 129 | | |
| | | 130 | | private sealed class BusyStackEntry(IBusy busy, CancellationTokenSource? linkedSource, BusyScope scope) |
| | | 131 | | { |
| | | 132 | | public IBusy Busy { get; } = busy; |
| | | 133 | | |
| | | 134 | | public BusyScope Scope { get; } = scope; |
| | | 135 | | |
| | 108 | 136 | | public void DisposeResources() => linkedSource?.Dispose(); |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | |