-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAutomationWorkTracker.cs
More file actions
70 lines (58 loc) · 1.47 KB
/
AutomationWorkTracker.cs
File metadata and controls
70 lines (58 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
namespace TidyWindow.App.Services;
public sealed class AutomationWorkTracker : IAutomationWorkTracker
{
private readonly Dictionary<Guid, AutomationWorkItem> _active = new();
private readonly object _lock = new();
public event EventHandler? ActiveWorkChanged;
public bool HasActiveWork
{
get
{
lock (_lock)
{
return _active.Count > 0;
}
}
}
public Guid BeginWork(AutomationWorkType type, string description)
{
var token = Guid.NewGuid();
var normalized = string.IsNullOrWhiteSpace(description) ? "Automation task" : description.Trim();
lock (_lock)
{
_active[token] = new AutomationWorkItem(token, type, normalized);
}
OnActiveWorkChanged();
return token;
}
public void CompleteWork(Guid token)
{
if (token == Guid.Empty)
{
return;
}
var removed = false;
lock (_lock)
{
removed = _active.Remove(token);
}
if (removed)
{
OnActiveWorkChanged();
}
}
public IReadOnlyList<AutomationWorkItem> GetActiveWork()
{
lock (_lock)
{
return _active.Values.ToArray();
}
}
private void OnActiveWorkChanged()
{
ActiveWorkChanged?.Invoke(this, EventArgs.Empty);
}
}