~ruther/NosSmooth.Local

ref: e3484c3c19b7c13eb58121affde55a6e2fb2e310 NosSmooth.Local/src/Extensions/NosSmooth.Extensions.SharedBinding/Lifetime/DefaultInstanceLifetime.cs -rw-r--r-- 1.3 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
54
55
56
57
//
//  DefaultInstanceLifetime.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 Remora.Results;

namespace NosSmooth.Extensions.SharedBinding.Lifetime;

public class DefaultInstanceLifetime : IInstanceLifetime
{
    private readonly CancellationTokenSource _stoppingSource;
    private bool _stoppingHooked;

    public DefaultInstanceLifetime()
    {
        _stoppingSource = new CancellationTokenSource();
    }

    /// <inheritdoc/>
    public SharedInstanceInfo Info { get; }

    /// <inheritdoc />
    public CancellationToken InstanceStopping
    {
        get
        {
            _stoppingHooked = true;
            return _stoppingSource.Token;
        }
    }

    /// <inheritdoc />
    public Result RequestStop()
    {
        if (!_stoppingHooked)
        { // There is no way anything was hooked to InstanceStopping, thus
          // stop won't work for sure.
            return new CannotRequestStop()
        }

        try
        {
            _stoppingSource.Cancel();
            return Result.FromSuccess();
        }
        catch (TaskCanceledException)
        {
            return Result.FromSuccess();
        }
        catch (Exception e)
        {
            return e;
        }
    }
}
Do not follow this link