diff --git a/Shared/General/Progress.cs b/Shared/General/Progress.cs index 2c44559..8e159cb 100644 --- a/Shared/General/Progress.cs +++ b/Shared/General/Progress.cs @@ -7,10 +7,11 @@ namespace MeloongCore; public class ProgressProvider { /// - /// 当进度被改变时触发。 + /// 当原始进度可能被改变时触发。 /// - public event Action? ProgressChanged; - private void InvokeProgressChanged() => ProgressChanged?.Invoke(); + public event Action<(double actual, double skiped)>? ProgressChanged; + private void InvokeProgressChanged((double actual, double skiped) raw) => ProgressChanged?.Invoke(raw); + private void InvokeProgressChanged() => InvokeProgressChanged(GetRaw()); // ===================================== 主项进度 ===================================== @@ -122,7 +123,7 @@ public class ProgressProvider { if (childrens.Any(c => c.percentage > 0)) { // 将子项的进度加入主项 var mult = progressParts.splited / childrens.Sum(c => c.percentage); childrens.ForEach(c => { - var (subActual, subSkiped) = c.child.GetTotalProgress(); + var (subActual, subSkiped) = c.child.GetRaw(); progressParts.actual += mult * c.percentage * (action == ChildrenAction.Finished ? (1 - subSkiped) : subActual); progressParts.skiped += mult * c.percentage * (action == ChildrenAction.Skiped ? (1 - subActual) : subSkiped); c.child.ProgressChanged -= InvokeProgressChanged; @@ -140,13 +141,13 @@ public class ProgressProvider { /// /// 获取包含子项进度的实际总进度值。 /// - private (double actual, double skiped) GetTotalProgress() { + public (double actual, double skiped) GetRaw() { lock (this) { (double actual, double skiped) current = (progressParts.actual, progressParts.skiped); if (childrens.Any(c => c.percentage > 0)) { // 将子项的进度加入主项 var mult = progressParts.splited / childrens.Sum(c => c.percentage); childrens.ForEach(c => { - var (subActual, subSkiped) = c.child.GetTotalProgress(); + var (subActual, subSkiped) = c.child.GetRaw(); current.actual += mult * c.percentage * subActual; current.skiped += mult * c.percentage * subSkiped; }); @@ -160,9 +161,9 @@ public class ProgressProvider { /// /// 获取一个在多次观测之间必定单调递增的进度值,范围为 [0, 1]。 /// - public double Observe() { + public double GetIncrement() { lock (this) { - (double actual, double skiped) current = GetTotalProgress(); + (double actual, double skiped) current = GetRaw(); if (current == observedProgress) return incrementProgress; // 未改变 if (current.actual + current.skiped > 0.9999999) { // 已完成 incrementProgress = 1; @@ -185,15 +186,15 @@ public class ProgressObserver : RateLimitedWorker { public ProgressObserver(ProgressProvider provider, Action updateAction, double minimalIntervalMs = 70) : base(() => Observe(provider, updateAction), minimalIntervalMs, RateLimitMode.ImmediateThenMerge) { this.provider = provider; - provider.ProgressChanged += Invoke; + provider.ProgressChanged += StartObserver; } public override void Dispose() { - provider.ProgressChanged -= Invoke; + provider.ProgressChanged -= StartObserver; base.Dispose(); } - + private void StartObserver((double actual, double skiped) raw) => Start(); private static void Observe(ProgressProvider provider, Action updateAction) { - double progress = provider.Observe(); + double progress = provider.GetIncrement(); // TODO: 添加平缓过渡 updateAction(progress); } diff --git a/Shared/General/Workers/RateLimitedWorker.cs b/Shared/General/Workers/RateLimitedWorker.cs index ee90ecc..8e125fb 100644 --- a/Shared/General/Workers/RateLimitedWorker.cs +++ b/Shared/General/Workers/RateLimitedWorker.cs @@ -21,7 +21,7 @@ public class RateLimitedWorker(Action workload, double minimalIntervalMs, RateLi RateLimitMode.ImmediateThenIgnore => Debouncer.Debounce(workload, TimeSpan.FromMilliseconds(minimalIntervalMs), leading: true, trailing: false) }; - public void Invoke() { + public void Start() { if (!disposed) action.Invoke(); } diff --git a/Shared/General/Workers/RedoableWorker.cs b/Shared/General/Workers/RedoableWorker.cs index ab1061b..7bd11cf 100644 --- a/Shared/General/Workers/RedoableWorker.cs +++ b/Shared/General/Workers/RedoableWorker.cs @@ -1,50 +1,63 @@ namespace MeloongCore; -/// -/// 可合并和重启的工作器。 -/// 在工作负载运行期间多次调用 会取消当前负载并使用最新令牌重新执行;多次调用也只重新执行一次。 -/// -public interface IRedoableWorker { +public interface IWorker { + /// 在工作线程运行工作负载。 + void Start(CancellationToken cancellationToken = default, ProgressProvider? progressOverride = null); + /// 取消当前运行。 + void Cancel(); + /// 若当前正在运行中,等待其完成。若未在运行中,则立即返回。 + /// 若未超时则返回 true。 + bool WaitIfRunning(int millisecondsTimeout = -1, CancellationToken cancellationToken = default); + /// 若当前正在运行中,异步等待其完成。若未在运行中,则立即返回。 + /// 若未超时则返回 true。 + Task WaitIfRunningAsync(int millisecondsTimeout = -1, CancellationToken cancellationToken = default); + + /// 从空闲状态进入运行状态时触发。 + event Action? Started; + /// 运行结束并进入空闲状态时触发。 + event Action? Stopped; + /// 工作负载成功完成时触发。 + public event Action? Succeeded; + /// 工作负载执行失败时触发,参数为发生的异常。 + public event Action? Failed; + /// 运行被取消时触发。 + event Action? Canceled; + /// 当前是否正在运行。 bool Running { get; } /// 是否曾经有过未被取消且未失败的运行。 bool HasSucceeded { get; } /// 标识上次进入空闲时,并非因为取消或失败,而是正常运行到结束。 bool LastSucceeded { get; } - /// 在工作线程运行工作负载。若当前已在运行,则取消当前负载并使用最新令牌重启。 - void Start(CancellationToken cancellationToken = default); - /// 取消当前运行。 - void Cancel(); - /// 仅在当前处于运行状态时,等待其完成。 - /// 若未超时则返回 true。 - bool WaitIfRunning(int millisecondsTimeout = -1, CancellationToken cancellationToken = default); - /// 仅在当前处于运行状态时,异步等待其完成。 - /// 若未超时则返回 true。 - Task WaitIfRunningAsync(int millisecondsTimeout = -1, CancellationToken cancellationToken = default); + /// 当前工作负载的执行进度。 + ProgressProvider Progress { get; } } // TODO: 现在的可观测性不佳,考虑增加调用方信息以及自身名称等,输出到日志,例如 CallerArgumentExpression -/// -public abstract class RedoableWorkerBase(Func workload, ProgressProvider? progress = null) : IRedoableWorker { +/// +/// 可合并和重启的工作器。 +/// 在工作负载运行期间多次调用 会取消当前负载并使用最新令牌重新执行;多次调用也只重新执行一次。 +/// +public abstract class RedoableWorkerBase(Func workload, ProgressProvider? progress = null) : IWorker { // ============================================ 状态与事件 ============================================ /// public bool Running { get { lock (this) return running; } } private bool running; - /// 从空闲状态进入运行状态时触发。 + /// public event Action? Started; - /// 运行结束并进入空闲状态时触发。 + /// public event Action? Stopped; /// public bool HasSucceeded { get { lock (this) return hasSucceeded; } } private bool hasSucceeded; - /// 工作负载成功完成时触发,参数为返回值。这可能会在重启前触发。 - public event Action? Succeeded; - /// 工作负载执行失败时触发,参数为发生的异常。这可能会在重启前触发。 + /// + public event Action? Succeeded; + /// public event Action? Failed; - /// 运行被取消时触发。这可能会在重启前触发。 + /// public event Action? Canceled; /// @@ -62,7 +75,8 @@ public abstract class RedoableWorkerBase(Func + public ProgressProvider Progress { get; private set; } = progress ?? new ProgressProvider(); private bool pendingRedo; @@ -72,11 +86,12 @@ public abstract class RedoableWorkerBase(Func - public void Start(CancellationToken cancellationToken = default) { + /// 在工作线程运行工作负载。若当前已在运行,则取消当前负载并使用最新令牌重启。 + public void Start(CancellationToken cancellationToken = default, ProgressProvider? progressOverride = null) { // 接取运行状态 lock (this) { lastToken = cancellationToken; + if (progressOverride != null) Progress = progressOverride; if (running) { pendingRedo = true; realCts?.Cancel(); @@ -85,10 +100,10 @@ public abstract class RedoableWorkerBase(Func(Func(Func(Func