| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SingleTaskDeferrer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | using MyNet.Utilities.Deferring; |
| | | 12 | | using MyNet.Utilities.Suspending; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Utilities.Threading; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Coordinates deferred, optionally throttled refresh requests with at-most-one asynchronous execution. |
| | | 18 | | /// While a task runs, a new request cancels it and schedules a rerun when the current execution ends. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// Compose <see cref="Deferring.DeferredAction"/> (batch updates), <see cref="Suspender"/> (temporary mute), |
| | | 22 | | /// and <see cref="SingleTaskRunner"/> (single in-flight task). Use <see cref="Request"/> from property setters |
| | | 23 | | /// and <see cref="Defer"/> when applying several changes in one scope. |
| | | 24 | | /// </remarks> |
| | | 25 | | public sealed class SingleTaskDeferrer : IDisposable, IDeferrable, ISuspendable |
| | | 26 | | { |
| | | 27 | | private readonly DeferredAction _deferredAction; |
| | 15 | 28 | | private readonly Suspender _suspender = new(); |
| | | 29 | | private readonly SingleTaskRunner _runner; |
| | | 30 | | private readonly Debouncer? _debouncer; |
| | | 31 | | private readonly Action<bool>? _onRunningChanged; |
| | | 32 | | private bool _mustRerun; |
| | | 33 | | private bool _disposed; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="SingleTaskDeferrer"/> class using a synchronous action (wrapped as |
| | | 37 | | /// </summary> |
| | | 38 | | public SingleTaskDeferrer( |
| | | 39 | | Action<CancellationToken> action, |
| | | 40 | | Action<bool>? onRunningChanged = null, |
| | | 41 | | Action? onCancelled = null, |
| | | 42 | | int throttleMilliseconds = 0, |
| | | 43 | | ILogger? logger = null) |
| | 0 | 44 | | : this( |
| | 0 | 45 | | cancellationToken => |
| | 0 | 46 | | { |
| | 0 | 47 | | action(cancellationToken); |
| | 0 | 48 | | return Task.CompletedTask; |
| | 0 | 49 | | }, |
| | 0 | 50 | | onRunningChanged, |
| | 0 | 51 | | onCancelled, |
| | 0 | 52 | | throttleMilliseconds, |
| | 0 | 53 | | logger) |
| | | 54 | | { |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Initializes a new instance of the <see cref="SingleTaskDeferrer"/> class using an asynchronous action. |
| | | 59 | | /// </summary> |
| | | 60 | | public SingleTaskDeferrer( |
| | | 61 | | Func<CancellationToken, Task> action, |
| | | 62 | | Action<bool>? onRunningChanged = null, |
| | | 63 | | Action? onCancelled = null, |
| | | 64 | | int throttleMilliseconds = 0, |
| | | 65 | | ILogger? logger = null) |
| | | 66 | | { |
| | 15 | 67 | | ArgumentNullException.ThrowIfNull(action); |
| | | 68 | | |
| | 15 | 69 | | if (throttleMilliseconds < 0) |
| | 0 | 70 | | ArgumentOutOfRangeException.ThrowIfNegative(throttleMilliseconds); |
| | | 71 | | |
| | 15 | 72 | | _deferredAction = new(FlushDeferredRequest); |
| | 15 | 73 | | _debouncer = throttleMilliseconds > 0 ? new Debouncer(throttleMilliseconds) : null; |
| | 15 | 74 | | _onRunningChanged = onRunningChanged; |
| | | 75 | | |
| | 15 | 76 | | _runner = new( |
| | 15 | 77 | | action, |
| | 15 | 78 | | OnRunningChanged, |
| | 15 | 79 | | onCancelled, |
| | 15 | 80 | | logger); |
| | 15 | 81 | | } |
| | | 82 | | |
| | | 83 | | private void OnRunningChanged(bool isRunning) |
| | | 84 | | { |
| | 30 | 85 | | _onRunningChanged?.Invoke(isRunning); |
| | | 86 | | |
| | 30 | 87 | | if (isRunning || !_mustRerun) |
| | 27 | 88 | | return; |
| | | 89 | | |
| | 3 | 90 | | _mustRerun = false; |
| | 3 | 91 | | _ = _runner.RunAsync(); |
| | 3 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets a value indicating whether execution is currently deferred. |
| | | 96 | | /// </summary> |
| | 0 | 97 | | public bool IsDeferred => _deferredAction.IsDeferred; |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets a value indicating whether refresh requests are currently suspended. |
| | | 101 | | /// </summary> |
| | 0 | 102 | | public bool IsSuspended => _suspender.IsSuspended; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets a value indicating whether the underlying task is running. |
| | | 106 | | /// </summary> |
| | 0 | 107 | | public bool IsRunning => _runner.IsRunning; |
| | | 108 | | |
| | | 109 | | /// <inheritdoc /> |
| | 3 | 110 | | public IDisposable Defer() => _deferredAction.Defer(); |
| | | 111 | | |
| | | 112 | | /// <inheritdoc /> |
| | 3 | 113 | | public IDisposable Suspend() => _suspender.Suspend(); |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Requests execution of the bound work, honoring deferral, suspension, and throttle settings. |
| | | 117 | | /// </summary> |
| | 27 | 118 | | public void Request() => _deferredAction.Request(); |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Cancels the current execution if any. |
| | | 122 | | /// </summary> |
| | 0 | 123 | | public void Cancel() => _runner.Cancel(); |
| | | 124 | | |
| | | 125 | | private void FlushDeferredRequest() |
| | | 126 | | { |
| | 24 | 127 | | if (_suspender.IsSuspended) |
| | 3 | 128 | | return; |
| | | 129 | | |
| | 21 | 130 | | if (_debouncer is null) |
| | | 131 | | { |
| | 12 | 132 | | DispatchExecution(); |
| | 12 | 133 | | return; |
| | | 134 | | } |
| | | 135 | | |
| | 9 | 136 | | _debouncer.Schedule(DispatchExecution); |
| | 9 | 137 | | } |
| | | 138 | | |
| | | 139 | | private void DispatchExecution() |
| | | 140 | | { |
| | 15 | 141 | | if (_runner.IsRunning) |
| | | 142 | | { |
| | 3 | 143 | | _mustRerun = true; |
| | 3 | 144 | | _runner.Cancel(); |
| | 3 | 145 | | return; |
| | | 146 | | } |
| | | 147 | | |
| | 12 | 148 | | _ = _runner.RunAsync(); |
| | 12 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <inheritdoc /> |
| | | 152 | | public void Dispose() |
| | | 153 | | { |
| | 15 | 154 | | if (_disposed) |
| | 0 | 155 | | return; |
| | | 156 | | |
| | 15 | 157 | | _disposed = true; |
| | 15 | 158 | | _debouncer?.Dispose(); |
| | 15 | 159 | | _runner.Dispose(); |
| | 15 | 160 | | } |
| | | 161 | | } |
| | | 162 | | |