// 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.AdapterModels;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.CoreModels;
using StrixMusic.Sdk.Plugins.Model;
namespace StrixMusic.Sdk.PluginModels;
///
/// Wraps an instance of with the provided plugins.
///
public class UrlPluginWrapper : IUrl, IPluginWrapper
{
private readonly IUrl _url;
///
/// Initializes a new instance of the class.
///
/// The instance to wrap around and apply plugins to.
/// The plugins that are applied to items returned from or emitted by this collection.
internal UrlPluginWrapper(IUrl url, params SdkModelPlugin[] plugins)
{
foreach (var item in plugins)
ActivePlugins.Import(item);
ActivePlugins = GlobalModelPluginConnector.Create(ActivePlugins);
_url = ActivePlugins.Url.Execute(url);
AttachEvents(_url);
}
private void AttachEvents(IUrl url)
{
url.SourcesChanged += OnSourcesChanged;
}
private void DetachEvents(IUrl url)
{
url.SourcesChanged -= OnSourcesChanged;
}
private void OnSourcesChanged(object sender, EventArgs e) => SourcesChanged?.Invoke(sender, e);
///
public SdkModelPlugin ActivePlugins { get; } = new(PluginModelWrapperInfo.Metadata);
///
public ValueTask DisposeAsync()
{
DetachEvents(_url);
return _url.DisposeAsync();
}
///
public bool Equals(ICoreUrl other) => _url.Equals(other);
///
public IReadOnlyList Sources => _url.Sources;
///
public event EventHandler? SourcesChanged;
///
public string Label => _url.Label;
///
public Uri Url => _url.Url;
///
public UrlType Type => _url.Type;
}