Skip to content

Commit

Permalink
no longer use anonymous objects since the project does not support yet
Browse files Browse the repository at this point in the history
  • Loading branch information
EriksonBahr committed Jan 19, 2024
1 parent fd8bf95 commit b86c1aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
13 changes: 10 additions & 3 deletions src/Unleash/DefaultUnleash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Unleash
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unleash.Events;
using Unleash.Utilities;
using Unleash.Variants;
Expand Down Expand Up @@ -82,7 +83,13 @@ public DefaultUnleash(UnleashSettings settings, bool overrideDefaultStrategies,
}

/// <inheritdoc />
public ICollection<FeatureToggle> FeatureToggles => services.ToggleCollection.Instance.Features;
public ICollection<FeatureToggle> FeatureToggles => services.GetFeatureFlags().Instance.Features;

public async Task<ICollection<FeatureToggle>> GetFeatureTogglesAsync()
{
var toggles = await services.GetFeatureFlagsAsync();
return toggles.Instance.Features;
}

private EventCallbackConfig EventConfig { get; } = new EventCallbackConfig();

Expand Down Expand Up @@ -283,7 +290,7 @@ public IEnumerable<VariantDefinition> GetVariants(string toggleName, UnleashCont
private FeatureToggle GetToggle(string toggleName)
{
return services
.ToggleCollection
.GetFeatureFlags()
.Instance
.GetToggleByName(toggleName);
}
Expand Down Expand Up @@ -337,7 +344,7 @@ private IEnumerable<Constraint> ResolveConstraints(ActivationStrategy activation
{
foreach (var segmentId in activationStrategy.Segments)
{
var segment = services.ToggleCollection.Instance.GetSegmentById(segmentId);
var segment = services.GetFeatureFlags().Instance.GetSegmentById(segmentId);
if (segment != null)
{
foreach (var constraint in segment.Constraints)
Expand Down
16 changes: 11 additions & 5 deletions src/Unleash/Internal/FetchFeatureToggles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FetchFeatureToggles(
this.eventConfig = eventConfig;
}

public async Task<Tuple<ToggleCollection, string, bool>> FetchToggles(CancellationToken cancellationToken)
public async Task<FetchTogglesResult> FetchToggles(CancellationToken cancellationToken)
{
FetchTogglesResult result;
try
Expand All @@ -39,16 +39,22 @@ public async Task<Tuple<ToggleCollection, string, bool>> FetchToggles(Cancellati

if (!result.HasChanged)
{
return new Tuple<ToggleCollection, string, bool>(null, string.Empty, false);
return result;
}

if (string.IsNullOrEmpty(result.Etag))
return new Tuple<ToggleCollection, string, bool>(null, string.Empty, false);
{
result.HasChanged = false;
return result;
}

if (result.Etag == Etag)
return new Tuple<ToggleCollection, string, bool>(null, string.Empty, false);
{
result.HasChanged = false;
return result;
}

return new Tuple<ToggleCollection, string, bool>(result.ToggleCollection, result.Etag, true);
return result;
}
}
}
12 changes: 7 additions & 5 deletions src/Unleash/Internal/UnleashServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class UnleashServices : IDisposable

internal CancellationToken CancellationToken { get; }
internal IUnleashContextProvider ContextProvider { get; }
internal ThreadSafeToggleCollection ToggleCollection { get; }
private ThreadSafeToggleCollection ToggleCollection { get ; }
internal bool IsMetricsDisabled { get; }

private readonly FetchFeatureToggles fetchFeatureToggles;
Expand Down Expand Up @@ -186,17 +186,19 @@ public void Dispose()
ToggleCollection?.Dispose();
}

public async Task<ThreadSafeToggleCollection> GetFeatureFlags()
public async Task<ThreadSafeToggleCollection> GetFeatureFlagsAsync()
{
if (LastUpdated == null)
{
(ToggleCollection collection, string etag, bool hasChanged) = await fetchFeatureToggles.FetchToggles(new CancellationToken());
ToggleCollection.Instance = collection;
StoreState(collection, etag);
var fetchTogglesResult = await fetchFeatureToggles.FetchToggles(new CancellationToken());
ToggleCollection.Instance = fetchTogglesResult.ToggleCollection;
StoreState(fetchTogglesResult.ToggleCollection, fetchTogglesResult.Etag);
fetchFeatureTogglesTask.Enabled = true;
}

return ToggleCollection;
}

public ThreadSafeToggleCollection GetFeatureFlags() => GetFeatureFlagsAsync().Result;
}
}
6 changes: 3 additions & 3 deletions src/Unleash/Scheduling/FetchFeatureTogglesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public FetchFeatureTogglesTask(

public async Task ExecuteAsync(CancellationToken cancellationToken)
{
(ToggleCollection collection, string etag, bool hasChanged) = await fetchFeatureToggles.FetchToggles(cancellationToken);
if (hasChanged)
var fetchTogglesResult = await fetchFeatureToggles.FetchToggles(cancellationToken);
if (fetchTogglesResult.HasChanged)
{
onFlagsUpdated(collection, etag);
onFlagsUpdated(fetchTogglesResult.ToggleCollection, fetchTogglesResult.Etag);
}
}

Expand Down

0 comments on commit b86c1aa

Please sign in to comment.