using Chronos.Core.Model; namespace Chronos.App.Editor; /// /// The analyses and renders of the open sequence that wait, run or are paused, as the source reports them (BRIEF §12.3 /// "while a job runs on this sequence, settings are disabled", §11.5): the editor's busy lock, whether Analyse would /// queue a second analysis, and the server job the header of a server sequence shows. Call from one thread. /// public sealed class SequenceJobs(Guid sequenceId) { private readonly Dictionary _active = []; /// An analysis or render runs: settings wait (a local render's send home does not hold them). public bool IsBusy => _active.Values.Any(j => j.State == JobState.Running && j.Kind != JobKind.WorkspaceSend); /// A deep analysis waits, runs or is paused. public bool IsAnalysing => _active.Values.Any(j => j.Kind == JobKind.DeepAnalyse); /// The job to show: one running, else one paused, else the oldest waiting; null when there is none. public Job? Shown => First(_active.Values); /// The job of to show, chosen as is (the Edit path's steps). public Job? Active(JobKind kind) => First(_active.Values.Where(j => j.Kind == kind)); /// An analysis or render of this sequence, a workspace project's render on this PC or its send home (phase 5c). public bool Concerns(Job job) { ArgumentNullException.ThrowIfNull(job); return job.TargetIds.Contains(sequenceId) && IsSequenceKind(job.Kind); } /// The kinds the editor follows. public static bool IsSequenceKind(JobKind kind) => kind is JobKind.DeepAnalyse or JobKind.LightAnalyse or JobKind.Render or JobKind.WorkspaceRender or JobKind.WorkspaceSend; /// The job is one of those that wait, run or are paused. public bool IsActive(Guid jobId) => _active.ContainsKey(jobId); /// Takes in a job's new state. /// Whether a job joined or left the active ones or changed state; progress alone does not count. public bool Track(Job job) { if (!Concerns(job)) return false; if (job.State is not (JobState.Queued or JobState.Running or JobState.Paused)) return _active.Remove(job.Id); var changed = !_active.TryGetValue(job.Id, out var known) || known.State != job.State; _active[job.Id] = job; return changed; } private static Job? First(IEnumerable jobs) => jobs .OrderBy(j => j.State switch { JobState.Running => 0, JobState.Paused => 1, _ => 2 }) .ThenBy(j => j.Started ?? j.Created) .FirstOrDefault(); }