| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Progresser.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.Concurrent; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Diagnostics.CodeAnalysis; |
| | | 11 | | using System.Linq; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Utilities.Progress; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Concrete implementation of <see cref="IProgresser"/> that works with <see cref="ProgressMessage"/>. |
| | | 17 | | /// String-based convenience overloads are available via <see cref="ProgresserExtensions"/>. |
| | | 18 | | /// </summary> |
| | | 19 | | public class Progresser : Progresser<ProgressMessage>, IProgresser; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Generic implementation of <see cref="IProgresser{T}"/>. |
| | | 23 | | /// Manages a stack of active <see cref="ProgressStep{T}"/> instances and |
| | | 24 | | /// notifies registered <see cref="IProgress{T}"/> subscribers on every state change. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <typeparam name="T">The type of messages associated with progress steps.</typeparam> |
| | | 27 | | public class Progresser<T> : IProgresser<T> |
| | | 28 | | { |
| | | 29 | | // ── Fields ─────────────────────────────────────────────────────────────── |
| | | 30 | | |
| | | 31 | | /// <summary>Thread-safe set of progress subscribers (value is unused; the key is the subscriber).</summary> |
| | 33 | 32 | | private readonly ConcurrentDictionary<IProgress<ProgressReport<T>>, byte> _subscribers = new(); |
| | | 33 | | |
| | | 34 | | /// <summary>LIFO stack of active steps, from root (bottom) to innermost (top).</summary> |
| | 33 | 35 | | private readonly ConcurrentStack<ProgressStep<T>> _steps = new(); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The root step of the current session. Stored separately so that |
| | | 39 | | /// <see cref="Report"/> can access it in O(1) without iterating the stack. |
| | | 40 | | /// Volatile to avoid stale reads across threads. |
| | | 41 | | /// </summary> |
| | | 42 | | private volatile ProgressStep<T>? _rootStep; |
| | | 43 | | |
| | | 44 | | // ── IProgresser<T> — Begin ─────────────────────────────────────────────── |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | | 47 | | public IProgressStep<T> Begin(T message, Action? cancelAction = null) |
| | | 48 | | { |
| | 27 | 49 | | DiscardCurrentSession(); |
| | 27 | 50 | | return CreateRootStep([], message, cancelAction); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc/> |
| | | 54 | | public IProgressStep<T> Begin(int numberOfSteps, T message, Action? cancelAction = null) |
| | | 55 | | { |
| | 15 | 56 | | DiscardCurrentSession(); |
| | 15 | 57 | | return CreateRootStep(EqualWeights(numberOfSteps), message, cancelAction); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | | 61 | | public IProgressStep<T> Begin(IEnumerable<double> subStepWeightings, T message, Action? cancelAction = null) |
| | | 62 | | { |
| | 12 | 63 | | DiscardCurrentSession(); |
| | 12 | 64 | | return CreateRootStep(subStepWeightings, message, cancelAction); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // ── IProgresser<T> — StartStep ─────────────────────────────────────────── |
| | | 68 | | |
| | | 69 | | /// <inheritdoc/> |
| | | 70 | | public IProgressStep<T> StartStep(T message, bool canCancel = true) |
| | | 71 | | { |
| | 24 | 72 | | var parent = GetCurrentStep(); |
| | 18 | 73 | | return new ProgressStep<T>(this, parent, parent.AllocateChildSlot(), message, [], null, canCancel); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc/> |
| | | 77 | | public IProgressStep<T> StartStep(int numberOfSteps, T message, bool canCancel = true) |
| | | 78 | | { |
| | 9 | 79 | | var parent = GetCurrentStep(); |
| | 9 | 80 | | return new ProgressStep<T>(this, parent, parent.AllocateChildSlot(), message, EqualWeights(numberOfSteps), null, |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc/> |
| | | 84 | | public IProgressStep<T> StartStep(IEnumerable<double> subStepWeightings, T message, bool canCancel = true) |
| | | 85 | | { |
| | 9 | 86 | | var parent = GetCurrentStep(); |
| | 9 | 87 | | return new ProgressStep<T>(this, parent, parent.AllocateChildSlot(), message, subStepWeightings, null, canCancel |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // ── IProgresser<T> — Subscription ──────────────────────────────────────── |
| | | 91 | | |
| | | 92 | | /// <inheritdoc/> |
| | 12 | 93 | | public void Subscribe(IProgress<ProgressReport<T>> progress) => _subscribers.TryAdd(progress, 0); |
| | | 94 | | |
| | | 95 | | /// <inheritdoc/> |
| | 3 | 96 | | public void Unsubscribe(IProgress<ProgressReport<T>> progress) => _subscribers.TryRemove(progress, out _); |
| | | 97 | | |
| | | 98 | | // ── Internal API (called by ProgressStep<T>) ───────────────────────────── |
| | | 99 | | [SuppressMessage("ReSharper", "NonAtomicCompoundOperator", Justification = "ConcurrentStack is thread-safe for Push/ |
| | | 100 | | internal void Push(ProgressStep<T> step) |
| | | 101 | | { |
| | 90 | 102 | | _steps.Push(step); |
| | | 103 | | |
| | | 104 | | // The first step pushed after DiscardCurrentSession() becomes the root. |
| | 90 | 105 | | _rootStep ??= step; |
| | 90 | 106 | | } |
| | | 107 | | |
| | | 108 | | internal void Pop() |
| | | 109 | | { |
| | 90 | 110 | | _steps.TryPop(out _); |
| | 90 | 111 | | if (_steps.IsEmpty) |
| | 54 | 112 | | _rootStep = null; |
| | 90 | 113 | | } |
| | | 114 | | |
| | | 115 | | internal void Report() |
| | | 116 | | { |
| | 150 | 117 | | var root = _rootStep; |
| | 177 | 118 | | if (root is null) return; |
| | | 119 | | |
| | | 120 | | // Snapshot the stack to avoid races during enumeration. |
| | | 121 | | // ConcurrentStack enumerates top → bottom (LIFO), so Reverse() gives root → leaf. |
| | 123 | 122 | | var snapshot = _steps.ToArray(); |
| | 123 | 123 | | var messages = snapshot |
| | 123 | 124 | | .Reverse() |
| | 123 | 125 | | .Where(s => s.Message is not null) |
| | 123 | 126 | | .Select(s => s.Message!) |
| | 123 | 127 | | .ToList(); |
| | | 128 | | |
| | 123 | 129 | | var canCancel = snapshot.All(s => s.CanCancel); |
| | 123 | 130 | | var report = new ProgressReport<T>(root.Progress, messages, root.CancelAction, canCancel); |
| | | 131 | | |
| | 306 | 132 | | foreach (var subscriber in _subscribers.Keys) |
| | 30 | 133 | | subscriber.Report(report); |
| | 123 | 134 | | } |
| | | 135 | | |
| | | 136 | | // ── Private helpers ─────────────────────────────────────────────────────── |
| | | 137 | | private static IEnumerable<double> EqualWeights(int count) |
| | 24 | 138 | | => count <= 0 |
| | 24 | 139 | | ? [] |
| | 24 | 140 | | : Enumerable.Repeat(1.0 / count, count); |
| | | 141 | | |
| | | 142 | | /// <summary>Clears any ongoing session without triggering dispose logic on individual steps.</summary> |
| | | 143 | | private void DiscardCurrentSession() |
| | | 144 | | { |
| | 54 | 145 | | _steps.Clear(); |
| | 54 | 146 | | _rootStep = null; |
| | 54 | 147 | | } |
| | | 148 | | |
| | | 149 | | private ProgressStep<T> CreateRootStep(IEnumerable<double> weightings, T message, Action? cancelAction) => |
| | 54 | 150 | | new(this, parent: null, parentSlotIndex: -1, message, weightings, cancelAction, canCancel: true); |
| | | 151 | | |
| | | 152 | | private ProgressStep<T> GetCurrentStep() |
| | 42 | 153 | | => _steps.TryPeek(out var top) |
| | 42 | 154 | | ? top |
| | 42 | 155 | | : throw new InvalidOperationException( |
| | 42 | 156 | | "No active progress session. Call Begin() before StartStep()."); |
| | | 157 | | } |
| | | 158 | | |