LenovoLegionToolkit

Форк
0
110 строк · 4.0 Кб
1
using System;
2
using System.Threading.Tasks;
3

4
namespace LenovoLegionToolkit.Lib.Features;
5

6
public abstract class AbstractCompositeFeature<T, T1, T2> : IFeature<T>
7
    where T : struct
8
    where T1 : IFeature<T>
9
    where T2 : IFeature<T>
10
{
11
    protected readonly T1 Feature1;
12
    protected readonly T2 Feature2;
13

14
    private readonly Lazy<Task<IFeature<T>?>> _lazyAsyncFeature;
15

16
    protected AbstractCompositeFeature(T1 feature1, T2 feature2)
17
    {
18
        Feature1 = feature1 ?? throw new ArgumentNullException(nameof(feature1));
19
        Feature2 = feature2 ?? throw new ArgumentNullException(nameof(feature2));
20

21
        _lazyAsyncFeature = new(GetFeatureLazyAsync);
22
    }
23

24
    public async Task<bool> IsSupportedAsync() => await _lazyAsyncFeature.Value.ConfigureAwait(false) != null;
25

26
    public async Task<T[]> GetAllStatesAsync()
27
    {
28
        var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
29
        return await feature.GetAllStatesAsync().ConfigureAwait(false);
30
    }
31

32
    public async Task<T> GetStateAsync()
33
    {
34
        var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
35
        return await feature.GetStateAsync().ConfigureAwait(false);
36
    }
37

38
    public async Task SetStateAsync(T state)
39
    {
40
        var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
41
        await feature.SetStateAsync(state).ConfigureAwait(false);
42
    }
43

44
    protected virtual async Task<IFeature<T>?> GetFeatureLazyAsync()
45
    {
46
        if (await Feature1.IsSupportedAsync().ConfigureAwait(false))
47
            return Feature1;
48

49
        if (await Feature2.IsSupportedAsync().ConfigureAwait(false))
50
            return Feature2;
51

52
        return null;
53
    }
54
}
55

56
public abstract class AbstractCompositeFeature<T, T1, T2, T3> : IFeature<T>
57
    where T : struct
58
    where T1 : IFeature<T>
59
    where T2 : IFeature<T>
60
    where T3 : IFeature<T>
61
{
62
    protected readonly T1 Feature1;
63
    protected readonly T2 Feature2;
64
    protected readonly T3 Feature3;
65

66
    private readonly Lazy<Task<IFeature<T>?>> _lazyAsyncFeature;
67

68
    protected AbstractCompositeFeature(T1 feature1, T2 feature2, T3 feature3)
69
    {
70
        Feature1 = feature1 ?? throw new ArgumentNullException(nameof(feature1));
71
        Feature2 = feature2 ?? throw new ArgumentNullException(nameof(feature2));
72
        Feature3 = feature3 ?? throw new ArgumentNullException(nameof(feature3));
73

74
        _lazyAsyncFeature = new(GetFeatureLazyAsync);
75
    }
76

77
    public async Task<bool> IsSupportedAsync() => await _lazyAsyncFeature.Value.ConfigureAwait(false) != null;
78

79
    public async Task<T[]> GetAllStatesAsync()
80
    {
81
        var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
82
        return await feature.GetAllStatesAsync().ConfigureAwait(false);
83
    }
84

85
    public async Task<T> GetStateAsync()
86
    {
87
        var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
88
        return await feature.GetStateAsync().ConfigureAwait(false);
89
    }
90

91
    public async Task SetStateAsync(T state)
92
    {
93
        var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
94
        await feature.SetStateAsync(state).ConfigureAwait(false);
95
    }
96

97
    protected virtual async Task<IFeature<T>?> GetFeatureLazyAsync()
98
    {
99
        if (await Feature1.IsSupportedAsync().ConfigureAwait(false))
100
            return Feature1;
101

102
        if (await Feature2.IsSupportedAsync().ConfigureAwait(false))
103
            return Feature2;
104

105
        if (await Feature3.IsSupportedAsync().ConfigureAwait(false))
106
            return Feature3;
107

108
        return null;
109
    }
110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.