//
// SharedLifetime.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using NosSmooth.Extensions.SharedBinding.EventArgs;
using Remora.Results;
namespace NosSmooth.Extensions.SharedBinding.Lifetime;
///
/// Events for shared lifetime.
///
public class SharedLifetime
{
///
/// A new instance has been attached.
///
public event EventHandler? InstanceAttached;
///
/// A new instance has been attached and a conflict was detected,
/// the same instance type is already attached.
///
public event EventHandler? ConflictDetected;
///
/// Initialize a new shared instance.
///
/// The new instance.
/// A result, if errorful, the new instance should not start.
public Result Initialize(SharedInstanceInfo instanceInfo)
{
return Result.FromSuccess(new CancellationTokenSource());
}
///
/// Initialize a new shared instance without its cooperation.
/// In case the instance is not allowed, an exception will be thrown.
///
/// The shared instance.
/// Throws an exception in case the instance cannot be attached.
public void ForceInitialize(SharedInstanceInfo instanceInfo)
{
var result = Initialize(instanceInfo);
if (!result.IsSuccess)
{
throw new Exception($"Initialization not allowed! {result.Error.Message}");
}
}
}