~ruther/NosSmooth.Local

ref: e3484c3c19b7c13eb58121affde55a6e2fb2e310 NosSmooth.Local/src/Extensions/NosSmooth.Extensions.SharedBinding/Lifetime/SharedLifetime.cs -rw-r--r-- 1.8 KiB
e3484c3c — Rutherther feat(shared): add part of shared lifetime 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
//  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;

/// <summary>
/// Events for shared lifetime.
/// </summary>
public class SharedLifetime
{
    /// <summary>
    /// A new instance has been attached.
    /// </summary>
    public event EventHandler<InstanceEventArgs>? InstanceAttached;

    /// <summary>
    /// A new instance has been attached and a conflict was detected,
    /// the same instance type is already attached.
    /// </summary>
    public event EventHandler<ConflictEventArgs>? ConflictDetected;

    /// <summary>
    /// Initialize a new shared instance.
    /// </summary>
    /// <param name="instanceInfo">The new instance.</param>
    /// <returns>A result, if errorful, the new instance should not start.</returns>
    public Result<CancellationTokenSource> Initialize(SharedInstanceInfo instanceInfo)
    {
        return Result<CancellationTokenSource>.FromSuccess(new CancellationTokenSource());
    }

    /// <summary>
    /// Initialize a new shared instance without its cooperation.
    /// In case the instance is not allowed, an exception will be thrown.
    /// </summary>
    /// <param name="instanceInfo">The shared instance.</param>
    /// <exception cref="Exception">Throws an exception in case the instance cannot be attached.</exception>
    public void ForceInitialize(SharedInstanceInfo instanceInfo)
    {
        var result = Initialize(instanceInfo);

        if (!result.IsSuccess)
        {
            throw new Exception($"Initialization not allowed! {result.Error.Message}");
        }
    }
}
Do not follow this link