|
| 1 | +namespace Umbraco.Cms.Core; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Makes a code block timed (take at least a certain amount of time). This class cannot be inherited. |
| 5 | +/// </summary> |
| 6 | +public sealed class TimedScope : IDisposable, IAsyncDisposable |
| 7 | +{ |
| 8 | + private readonly TimeSpan _duration; |
| 9 | + private readonly TimeProvider _timeProvider; |
| 10 | + private readonly CancellationTokenSource _cancellationTokenSource; |
| 11 | + private readonly long _startingTimestamp; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Gets the elapsed time. |
| 15 | + /// </summary> |
| 16 | + /// <value> |
| 17 | + /// The elapsed time. |
| 18 | + /// </value> |
| 19 | + public TimeSpan Elapsed |
| 20 | + => _timeProvider.GetElapsedTime(_startingTimestamp); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Gets the remaining time. |
| 24 | + /// </summary> |
| 25 | + /// <value> |
| 26 | + /// The remaining time. |
| 27 | + /// </value> |
| 28 | + public TimeSpan Remaining |
| 29 | + => TryGetRemaining(out TimeSpan remaining) ? remaining : TimeSpan.Zero; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the <see cref="TimedScope" /> class. |
| 33 | + /// </summary> |
| 34 | + /// <param name="millisecondsDuration">The number of milliseconds the scope should at least take.</param> |
| 35 | + public TimedScope(long millisecondsDuration) |
| 36 | + : this(TimeSpan.FromMilliseconds(millisecondsDuration)) |
| 37 | + { } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Initializes a new instance of the <see cref="TimedScope" /> class. |
| 41 | + /// </summary> |
| 42 | + /// <param name="millisecondsDuration">The number of milliseconds the scope should at least take.</param> |
| 43 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 44 | + public TimedScope(long millisecondsDuration, CancellationToken cancellationToken) |
| 45 | + : this(TimeSpan.FromMilliseconds(millisecondsDuration), cancellationToken) |
| 46 | + { } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Initializes a new instance of the <see cref="TimedScope" /> class. |
| 50 | + /// </summary> |
| 51 | + /// <param name="millisecondsDuration">The number of milliseconds the scope should at least take.</param> |
| 52 | + /// <param name="timeProvider">The time provider.</param> |
| 53 | + public TimedScope(long millisecondsDuration, TimeProvider timeProvider) |
| 54 | + : this(TimeSpan.FromMilliseconds(millisecondsDuration), timeProvider) |
| 55 | + { } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Initializes a new instance of the <see cref="TimedScope" /> class. |
| 59 | + /// </summary> |
| 60 | + /// <param name="millisecondsDuration">The number of milliseconds the scope should at least take.</param> |
| 61 | + /// <param name="timeProvider">The time provider.</param> |
| 62 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 63 | + public TimedScope(long millisecondsDuration, TimeProvider timeProvider, CancellationToken cancellationToken) |
| 64 | + : this(TimeSpan.FromMilliseconds(millisecondsDuration), timeProvider, cancellationToken) |
| 65 | + { } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Initializes a new instance of the <see cref="TimedScope"/> class. |
| 69 | + /// </summary> |
| 70 | + /// <param name="duration">The duration the scope should at least take.</param> |
| 71 | + public TimedScope(TimeSpan duration) |
| 72 | + : this(duration, TimeProvider.System) |
| 73 | + { } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Initializes a new instance of the <see cref="TimedScope" /> class. |
| 77 | + /// </summary> |
| 78 | + /// <param name="duration">The duration the scope should at least take.</param> |
| 79 | + /// <param name="timeProvider">The time provider.</param> |
| 80 | + public TimedScope(TimeSpan duration, TimeProvider timeProvider) |
| 81 | + : this(duration, timeProvider, new CancellationTokenSource()) |
| 82 | + { } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Initializes a new instance of the <see cref="TimedScope" /> class. |
| 86 | + /// </summary> |
| 87 | + /// <param name="duration">The duration the scope should at least take.</param> |
| 88 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 89 | + public TimedScope(TimeSpan duration, CancellationToken cancellationToken) |
| 90 | + : this(duration, TimeProvider.System, cancellationToken) |
| 91 | + { } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Initializes a new instance of the <see cref="TimedScope" /> class. |
| 95 | + /// </summary> |
| 96 | + /// <param name="duration">The duration the scope should at least take.</param> |
| 97 | + /// <param name="timeProvider">The time provider.</param> |
| 98 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 99 | + public TimedScope(TimeSpan duration, TimeProvider timeProvider, CancellationToken cancellationToken) |
| 100 | + : this(duration, timeProvider, CancellationTokenSource.CreateLinkedTokenSource(cancellationToken)) |
| 101 | + { } |
| 102 | + |
| 103 | + private TimedScope(TimeSpan duration, TimeProvider timeProvider, CancellationTokenSource cancellationTokenSource) |
| 104 | + { |
| 105 | + _duration = duration; |
| 106 | + _timeProvider = timeProvider; |
| 107 | + _cancellationTokenSource = cancellationTokenSource; |
| 108 | + _startingTimestamp = timeProvider.GetTimestamp(); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Cancels the timed scope. |
| 113 | + /// </summary> |
| 114 | + public void Cancel() |
| 115 | + => _cancellationTokenSource.Cancel(); |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Cancels the timed scope asynchronously. |
| 119 | + /// </summary> |
| 120 | + public async Task CancelAsync() |
| 121 | + => await _cancellationTokenSource.CancelAsync().ConfigureAwait(false); |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 125 | + /// </summary> |
| 126 | + /// <remarks> |
| 127 | + /// This will block using <see cref="Thread.Sleep(TimeSpan)" /> until the remaining time has elapsed, if not cancelled. |
| 128 | + /// </remarks> |
| 129 | + public void Dispose() |
| 130 | + { |
| 131 | + if (_cancellationTokenSource.IsCancellationRequested is false && |
| 132 | + TryGetRemaining(out TimeSpan remaining)) |
| 133 | + { |
| 134 | + Thread.Sleep(remaining); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously. |
| 140 | + /// </summary> |
| 141 | + /// <returns> |
| 142 | + /// A task that represents the asynchronous dispose operation. |
| 143 | + /// </returns> |
| 144 | + /// <remarks> |
| 145 | + /// This will delay using <see cref="Task.Delay(TimeSpan, TimeProvider, CancellationToken)" /> until the remaining time has elapsed, if not cancelled. |
| 146 | + /// </remarks> |
| 147 | + public async ValueTask DisposeAsync() |
| 148 | + { |
| 149 | + if (_cancellationTokenSource.IsCancellationRequested is false && |
| 150 | + TryGetRemaining(out TimeSpan remaining)) |
| 151 | + { |
| 152 | + await Task.Delay(remaining, _timeProvider, _cancellationTokenSource.Token).ConfigureAwait(false); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private bool TryGetRemaining(out TimeSpan remaining) |
| 157 | + { |
| 158 | + remaining = _duration.Subtract(Elapsed); |
| 159 | + |
| 160 | + return remaining > TimeSpan.Zero; |
| 161 | + } |
| 162 | +} |
0 commit comments