LenovoLegionToolkit
110 строк · 4.0 Кб
1using System;
2using System.Threading.Tasks;
3
4namespace LenovoLegionToolkit.Lib.Features;
5
6public abstract class AbstractCompositeFeature<T, T1, T2> : IFeature<T>
7where T : struct
8where T1 : IFeature<T>
9where T2 : IFeature<T>
10{
11protected readonly T1 Feature1;
12protected readonly T2 Feature2;
13
14private readonly Lazy<Task<IFeature<T>?>> _lazyAsyncFeature;
15
16protected AbstractCompositeFeature(T1 feature1, T2 feature2)
17{
18Feature1 = feature1 ?? throw new ArgumentNullException(nameof(feature1));
19Feature2 = feature2 ?? throw new ArgumentNullException(nameof(feature2));
20
21_lazyAsyncFeature = new(GetFeatureLazyAsync);
22}
23
24public async Task<bool> IsSupportedAsync() => await _lazyAsyncFeature.Value.ConfigureAwait(false) != null;
25
26public async Task<T[]> GetAllStatesAsync()
27{
28var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
29return await feature.GetAllStatesAsync().ConfigureAwait(false);
30}
31
32public async Task<T> GetStateAsync()
33{
34var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
35return await feature.GetStateAsync().ConfigureAwait(false);
36}
37
38public async Task SetStateAsync(T state)
39{
40var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
41await feature.SetStateAsync(state).ConfigureAwait(false);
42}
43
44protected virtual async Task<IFeature<T>?> GetFeatureLazyAsync()
45{
46if (await Feature1.IsSupportedAsync().ConfigureAwait(false))
47return Feature1;
48
49if (await Feature2.IsSupportedAsync().ConfigureAwait(false))
50return Feature2;
51
52return null;
53}
54}
55
56public abstract class AbstractCompositeFeature<T, T1, T2, T3> : IFeature<T>
57where T : struct
58where T1 : IFeature<T>
59where T2 : IFeature<T>
60where T3 : IFeature<T>
61{
62protected readonly T1 Feature1;
63protected readonly T2 Feature2;
64protected readonly T3 Feature3;
65
66private readonly Lazy<Task<IFeature<T>?>> _lazyAsyncFeature;
67
68protected AbstractCompositeFeature(T1 feature1, T2 feature2, T3 feature3)
69{
70Feature1 = feature1 ?? throw new ArgumentNullException(nameof(feature1));
71Feature2 = feature2 ?? throw new ArgumentNullException(nameof(feature2));
72Feature3 = feature3 ?? throw new ArgumentNullException(nameof(feature3));
73
74_lazyAsyncFeature = new(GetFeatureLazyAsync);
75}
76
77public async Task<bool> IsSupportedAsync() => await _lazyAsyncFeature.Value.ConfigureAwait(false) != null;
78
79public async Task<T[]> GetAllStatesAsync()
80{
81var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
82return await feature.GetAllStatesAsync().ConfigureAwait(false);
83}
84
85public async Task<T> GetStateAsync()
86{
87var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
88return await feature.GetStateAsync().ConfigureAwait(false);
89}
90
91public async Task SetStateAsync(T state)
92{
93var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException($"No supported feature found. [type={GetType().Name}");
94await feature.SetStateAsync(state).ConfigureAwait(false);
95}
96
97protected virtual async Task<IFeature<T>?> GetFeatureLazyAsync()
98{
99if (await Feature1.IsSupportedAsync().ConfigureAwait(false))
100return Feature1;
101
102if (await Feature2.IsSupportedAsync().ConfigureAwait(false))
103return Feature2;
104
105if (await Feature3.IsSupportedAsync().ConfigureAwait(false))
106return Feature3;
107
108return null;
109}
110}
111