// Copyright (c) Arlo Godfrey. All Rights Reserved.
// Licensed under the GNU Lesser General Public License, Version 3.0 with additional terms.
// See the LICENSE, LICENSE.LESSER and LICENSE.ADDITIONAL files in the project root for more information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using StrixMusic.Sdk.CoreModels;
namespace StrixMusic.Services.CoreManagement
{
///
/// A central registry for tracking available cores.
///
public static class CoreRegistry
{
private static readonly List _metadataRegistry = new();
private static readonly Dictionary>> _coreFactories = new();
///
/// Holds all registered core metadata.
///
public static IReadOnlyList MetadataRegistry => _metadataRegistry;
///
/// Registers a core with the Strix SDK.
///
/// A delegate that, given an instance ID, returns an instance of a core.
/// The metadata to register for this core.
public static void Register(Func coreFactory, CoreMetadata metadata)
{
_metadataRegistry.Add(metadata);
_coreFactories.Add(metadata.Id, x => Task.FromResult(coreFactory(x)));
CoreRegistered?.Invoke(null, metadata);
}
///
/// Registers a core with the Strix SDK.
///
/// A delegate that, given an instance ID, returns an instance of a core.
/// The metadata to register for this core.
public static void Register(CoreMetadata metadata, Func> coreFactory)
{
_metadataRegistry.Add(metadata);
_coreFactories.Add(metadata.Id, coreFactory);
CoreRegistered?.Invoke(null, metadata);
}
///
/// Raised when a new core is registered.
///
public static event EventHandler? CoreRegistered;
///
/// Creates a core instance using a registered core factory.
///
/// The core registry id of the core to create an instance of.
/// A unique identifier for this core instance.
/// An new instance of with the given .
public static Task CreateCoreAsync(string coreRegistryId, string instanceId)
{
return _coreFactories[coreRegistryId](instanceId);
}
}
}