-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHarnessState.cs
More file actions
61 lines (52 loc) · 1.67 KB
/
Copy pathHarnessState.cs
File metadata and controls
61 lines (52 loc) · 1.67 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
namespace TUIKit.Example
{
using System;
using System.Collections.Generic;
using TUIKit.Modals;
/// <summary>
/// Shared, thread-safe state between the simulated agent (which runs on a background thread) and
/// the render thread: the CPU history sparkline series, the current tool progress, and a reference
/// to the notification center and clock.
/// </summary>
internal sealed class HarnessState
{
private readonly object _Sync = new object();
private readonly List<double> _Cpu = new List<double>();
private readonly Func<long> _Clock;
internal NotificationCenter Notifications { get; }
internal bool ActiveTool { get; set; }
internal double ToolProgress { get; set; }
internal HarnessState(NotificationCenter notifications, Func<long> clock)
{
Notifications = notifications ?? throw new ArgumentNullException(nameof(notifications));
_Clock = clock ?? throw new ArgumentNullException(nameof(clock));
}
internal long NowMilliseconds()
{
return _Clock();
}
internal void PushCpu(double value)
{
lock (_Sync)
{
_Cpu.Add(value);
while (_Cpu.Count > 60)
_Cpu.RemoveAt(0);
}
}
internal double CurrentCpu()
{
lock (_Sync)
{
return _Cpu.Count > 0 ? _Cpu[_Cpu.Count - 1] : 0.0;
}
}
internal double[] CpuSnapshot()
{
lock (_Sync)
{
return _Cpu.ToArray();
}
}
}
}