allow disposing even when not initialized

This commit is contained in:
Ell 2021-05-01 21:17:05 +02:00
parent 97eb02eb55
commit 1085b303aa

View file

@ -11,15 +11,19 @@ namespace Illumilib {
public static class IllumilibLighting { public static class IllumilibLighting {
private static List<LightingSystem> systems; private static List<LightingSystem> systems;
/// <summary>
/// A property that returns whether Illumilib is currently initialized
/// </summary>
public static bool Initialized => systems != null;
/// <summary> /// <summary>
/// Initializes Illumilib, starting all of the supported lighting systems. /// Initializes Illumilib, starting all of the supported lighting systems.
/// Any lighting systems that are not supported, or for which devices are not present, will be ignored. /// Any lighting systems that are not supported, or for which devices are not present, will be ignored.
/// This function runs asynchronously. /// This function runs asynchronously.
/// </summary> /// </summary>
/// <exception cref="InvalidOperationException">Thrown if Illumilib has already been initialized</exception> /// <exception cref="InvalidOperationException">Thrown if Illumilib has already been <see cref="Initialized"/></exception>
public static async Task Initialize() { public static async Task Initialize() {
if (systems != null) if (Initialized)
throw new InvalidOperationException("Illumilib has already been initialized"); throw new InvalidOperationException("Illumilib has already been initialized");
var ret = new List<LightingSystem>(); var ret = new List<LightingSystem>();
foreach (var system in new LightingSystem[] {new LogitechLighting(), new RazerLighting()}) { foreach (var system in new LightingSystem[] {new LogitechLighting(), new RazerLighting()}) {
@ -33,6 +37,8 @@ namespace Illumilib {
/// Disposes Illumilib, disposing all of the underlying lighting systems /// Disposes Illumilib, disposing all of the underlying lighting systems
/// </summary> /// </summary>
public static void Dispose() { public static void Dispose() {
if (!Initialized)
return;
ForEach(s => s.Dispose()); ForEach(s => s.Dispose());
systems = null; systems = null;
} }
@ -81,7 +87,7 @@ namespace Illumilib {
} }
private static void ForEach(Action<LightingSystem> action) { private static void ForEach(Action<LightingSystem> action) {
if (systems == null) if (!Initialized)
throw new InvalidOperationException("Illumilib has not been initialized yet"); throw new InvalidOperationException("Illumilib has not been initialized yet");
foreach (var system in systems) foreach (var system in systems)
action(system); action(system);