Skip to content

Commit 94f86f6

Browse files
chore: move away from persisted device list
1 parent e07d83f commit 94f86f6

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

src/ChromaControl.SDK.OpenRGB.Sample/Worker.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace ChromaControl.SDK.OpenRGB.Sample;
1313
/// </summary>
1414
public partial class Worker : BackgroundService
1515
{
16-
private bool _devicesReady;
16+
private IReadOnlyList<OpenRGBDevice> _devices = [];
1717

1818
private readonly ILogger<Worker> _logger;
1919
private readonly IOpenRGBService _service;
@@ -41,7 +41,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
4141

4242
while (!stoppingToken.IsCancellationRequested)
4343
{
44-
if (!_devicesReady)
44+
if (_devices.Count == 0)
4545
{
4646
await Task.Delay(1000, stoppingToken);
4747
continue;
@@ -62,7 +62,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
6262

6363
LogColorChangeMessage(_logger, currentColor.R, currentColor.G, currentColor.B);
6464

65-
foreach (var device in _service.Devices)
65+
foreach (var device in _devices)
6666
{
6767
var buffer = device.CreateColorBuffer();
6868

@@ -77,6 +77,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
7777

7878
private void OnDeviceListUpdated(object? sender, IReadOnlyList<OpenRGBDevice> e)
7979
{
80-
_devicesReady = true;
80+
_devices = e;
8181
}
8282
}

src/ChromaControl.SDK.OpenRGB/IOpenRGBService.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using ChromaControl.SDK.OpenRGB.Structs;
6-
using System.Collections.Immutable;
76
using System.Drawing;
87
using System.Text.Json.Nodes;
98

@@ -25,12 +24,7 @@ public interface IOpenRGBService
2524
event EventHandler<bool>? StartedChanged;
2625

2726
/// <summary>
28-
/// The devices available in OpenRGB.
29-
/// </summary>
30-
public ImmutableList<OpenRGBDevice> Devices { get; }
31-
32-
/// <summary>
33-
/// Occurs when the <see cref="Devices"/> is updated.
27+
/// Occurs when the device list is updated.
3428
/// </summary>
3529
event EventHandler<IReadOnlyList<OpenRGBDevice>>? DeviceListUpdated;
3630

@@ -48,11 +42,11 @@ public interface IOpenRGBService
4842
Task Restart(CancellationToken cancellationToken = default);
4943

5044
/// <summary>
51-
/// Updates the device list.
45+
/// Gets the device list.
5246
/// </summary>
5347
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
5448
/// <returns>A <see cref="Task"/>.</returns>
55-
Task UpdateDeviceListAsync(CancellationToken cancellationToken = default);
49+
Task<IReadOnlyList<OpenRGBDevice>> GetDeviceListAsync(CancellationToken cancellationToken = default);
5650

5751
/// <summary>
5852
/// Resizes a zone.

src/ChromaControl.SDK.OpenRGB/OpenRGBService.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
using ChromaControl.SDK.OpenRGB.Internal;
66
using ChromaControl.SDK.OpenRGB.Structs;
7-
using System.Collections.Concurrent;
8-
using System.Collections.Immutable;
97
using System.Drawing;
108
using System.Text.Json.Nodes;
119

@@ -15,7 +13,6 @@ namespace ChromaControl.SDK.OpenRGB;
1513
public class OpenRGBService : IOpenRGBService, IAsyncDisposable
1614
{
1715
private NativeOpenRGBService _service;
18-
private BlockingCollection<OpenRGBDevice> _devices;
1916
private readonly OpenRGBManager _manager;
2017

2118
/// <inheritdoc/>
@@ -24,9 +21,6 @@ public class OpenRGBService : IOpenRGBService, IAsyncDisposable
2421
/// <inheritdoc/>
2522
public event EventHandler<bool>? StartedChanged;
2623

27-
/// <inheritdoc/>
28-
public ImmutableList<OpenRGBDevice> Devices => [.. _devices];
29-
3024
/// <inheritdoc/>
3125
public event EventHandler<IReadOnlyList<OpenRGBDevice>>? DeviceListUpdated;
3226

@@ -37,7 +31,6 @@ public OpenRGBService()
3731
{
3832
_manager = new();
3933
_service = new();
40-
_devices = [];
4134

4235
_service.DeviceListUpdated += OnDeviceListUpdated;
4336
}
@@ -61,32 +54,27 @@ public async Task Restart(CancellationToken cancellationToken = default)
6154
_service = new();
6255
_service.DeviceListUpdated += OnDeviceListUpdated;
6356

64-
_devices = [];
65-
6657
_manager.Stop();
6758

6859
await StartServiceAsync(cancellationToken);
6960
}
7061
}
7162

7263
/// <inheritdoc/>
73-
public async Task UpdateDeviceListAsync(CancellationToken cancellationToken = default)
64+
public async Task<IReadOnlyList<OpenRGBDevice>> GetDeviceListAsync(CancellationToken cancellationToken = default)
7465
{
75-
while (_devices.Count > 0)
76-
{
77-
_devices.TryTake(out _);
78-
}
66+
var result = new List<OpenRGBDevice>();
7967

8068
var controllerCountResult = await _service.RequestControllerCountAsync(cancellationToken);
8169

8270
for (uint i = 0; i < controllerCountResult.Count; i++)
8371
{
8472
var controllerDataResult = await _service.RequestControllerDataAsync(i, cancellationToken);
8573

86-
_devices.Add(controllerDataResult.Device, cancellationToken);
74+
result.Add(controllerDataResult.Device);
8775
}
8876

89-
DeviceListUpdated?.Invoke(this, Devices);
77+
return result;
9078
}
9179

9280
/// <inheritdoc/>
@@ -243,7 +231,8 @@ internal async Task StopServiceAsync()
243231

244232
private async void OnDeviceListUpdated(object? sender, EventArgs e)
245233
{
246-
await UpdateDeviceListAsync();
234+
var devices = await GetDeviceListAsync();
235+
DeviceListUpdated?.Invoke(this, devices);
247236

248237
Started = true;
249238
StartedChanged?.Invoke(this, Started);

0 commit comments

Comments
 (0)