Skip to content

Commit

Permalink
Adjusts IVarantManager to use ValueTask (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossgrambo authored Oct 25, 2023
1 parent 197c325 commit e05591c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/Microsoft.FeatureManagement/FeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ public FeatureManager(

public Task<bool> IsEnabledAsync(string feature)
{
return IsEnabledWithVariantsAsync<object>(feature, appContext: null, useAppContext: false, CancellationToken.None);
return IsEnabledWithVariantsAsync<object>(feature, appContext: null, useAppContext: false, CancellationToken.None).AsTask();
}

public Task<bool> IsEnabledAsync<TContext>(string feature, TContext appContext)
{
return IsEnabledWithVariantsAsync(feature, appContext, useAppContext: true, CancellationToken.None);
return IsEnabledWithVariantsAsync(feature, appContext, useAppContext: true, CancellationToken.None).AsTask();
}

public Task<bool> IsEnabledAsync(string feature, CancellationToken cancellationToken)
public ValueTask<bool> IsEnabledAsync(string feature, CancellationToken cancellationToken)
{
return IsEnabledWithVariantsAsync<object>(feature, appContext: null, useAppContext: false, cancellationToken);
}

public Task<bool> IsEnabledAsync<TContext>(string feature, TContext appContext, CancellationToken cancellationToken)
public ValueTask<bool> IsEnabledAsync<TContext>(string feature, TContext appContext, CancellationToken cancellationToken)
{
return IsEnabledWithVariantsAsync(feature, appContext, useAppContext: true, cancellationToken);
}

private async Task<bool> IsEnabledWithVariantsAsync<TContext>(string feature, TContext appContext, bool useAppContext, CancellationToken cancellationToken)
private async ValueTask<bool> IsEnabledWithVariantsAsync<TContext>(string feature, TContext appContext, bool useAppContext, CancellationToken cancellationToken)
{
bool isFeatureEnabled = false;

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.FeatureManagement/FeatureManagerSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.FeatureManagement
class FeatureManagerSnapshot : IFeatureManagerSnapshot, IVariantFeatureManagerSnapshot
{
private readonly IVariantFeatureManager _featureManager;
private readonly ConcurrentDictionary<string, Task<bool>> _flagCache = new ConcurrentDictionary<string, Task<bool>>();
private readonly ConcurrentDictionary<string, ValueTask<bool>> _flagCache = new ConcurrentDictionary<string, ValueTask<bool>>();
private readonly ConcurrentDictionary<string, Variant> _variantCache = new ConcurrentDictionary<string, Variant>();
private IEnumerable<string> _featureNames;

Expand Down Expand Up @@ -55,24 +55,24 @@ public Task<bool> IsEnabledAsync(string feature)
{
return _flagCache.GetOrAdd(
feature,
(key) => _featureManager.IsEnabledAsync(key, CancellationToken.None));
(key) => _featureManager.IsEnabledAsync(key, CancellationToken.None)).AsTask();
}

public Task<bool> IsEnabledAsync<TContext>(string feature, TContext context)
{
return _flagCache.GetOrAdd(
feature,
(key) => _featureManager.IsEnabledAsync(key, context, CancellationToken.None));
(key) => _featureManager.IsEnabledAsync(key, context, CancellationToken.None)).AsTask();
}

public Task<bool> IsEnabledAsync(string feature, CancellationToken cancellationToken)
public ValueTask<bool> IsEnabledAsync(string feature, CancellationToken cancellationToken)
{
return _flagCache.GetOrAdd(
feature,
(key) => _featureManager.IsEnabledAsync(key, cancellationToken));
}

public Task<bool> IsEnabledAsync<TContext>(string feature, TContext context, CancellationToken cancellationToken)
public ValueTask<bool> IsEnabledAsync<TContext>(string feature, TContext context, CancellationToken cancellationToken)
{
return _flagCache.GetOrAdd(
feature,
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.FeatureManagement/IVariantFeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface IVariantFeatureManager
/// <param name="feature">The name of the feature to check.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>True if the feature is enabled, otherwise false.</returns>
Task<bool> IsEnabledAsync(string feature, CancellationToken cancellationToken);
ValueTask<bool> IsEnabledAsync(string feature, CancellationToken cancellationToken);

/// <summary>
/// Checks whether a given feature is enabled.
Expand All @@ -34,7 +34,7 @@ public interface IVariantFeatureManager
/// <param name="context">A context providing information that can be used to evaluate whether a feature should be on or off.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>True if the feature is enabled, otherwise false.</returns>
Task<bool> IsEnabledAsync<TContext>(string feature, TContext context, CancellationToken cancellationToken);
ValueTask<bool> IsEnabledAsync<TContext>(string feature, TContext context, CancellationToken cancellationToken);

/// <summary>
/// Gets the assigned variant for a specific feature.
Expand Down

0 comments on commit e05591c

Please sign in to comment.