From b86c1aa08bd0ebe07d228f29f220957285a7dd53 Mon Sep 17 00:00:00 2001 From: Erikson Bahr Date: Thu, 18 Jan 2024 21:07:08 -0300 Subject: [PATCH] no longer use anonymous objects since the project does not support yet --- src/Unleash/DefaultUnleash.cs | 13 ++++++++++--- src/Unleash/Internal/FetchFeatureToggles.cs | 16 +++++++++++----- src/Unleash/Internal/UnleashServices.cs | 12 +++++++----- .../Scheduling/FetchFeatureTogglesTask.cs | 6 +++--- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Unleash/DefaultUnleash.cs b/src/Unleash/DefaultUnleash.cs index ab4f9f55..67df249e 100644 --- a/src/Unleash/DefaultUnleash.cs +++ b/src/Unleash/DefaultUnleash.cs @@ -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; @@ -82,7 +83,13 @@ public DefaultUnleash(UnleashSettings settings, bool overrideDefaultStrategies, } /// - public ICollection FeatureToggles => services.ToggleCollection.Instance.Features; + public ICollection FeatureToggles => services.GetFeatureFlags().Instance.Features; + + public async Task> GetFeatureTogglesAsync() + { + var toggles = await services.GetFeatureFlagsAsync(); + return toggles.Instance.Features; + } private EventCallbackConfig EventConfig { get; } = new EventCallbackConfig(); @@ -283,7 +290,7 @@ public IEnumerable GetVariants(string toggleName, UnleashCont private FeatureToggle GetToggle(string toggleName) { return services - .ToggleCollection + .GetFeatureFlags() .Instance .GetToggleByName(toggleName); } @@ -337,7 +344,7 @@ private IEnumerable 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) diff --git a/src/Unleash/Internal/FetchFeatureToggles.cs b/src/Unleash/Internal/FetchFeatureToggles.cs index ab659ea5..4f5bf92f 100644 --- a/src/Unleash/Internal/FetchFeatureToggles.cs +++ b/src/Unleash/Internal/FetchFeatureToggles.cs @@ -23,7 +23,7 @@ public FetchFeatureToggles( this.eventConfig = eventConfig; } - public async Task> FetchToggles(CancellationToken cancellationToken) + public async Task FetchToggles(CancellationToken cancellationToken) { FetchTogglesResult result; try @@ -39,16 +39,22 @@ public async Task> FetchToggles(Cancellati if (!result.HasChanged) { - return new Tuple(null, string.Empty, false); + return result; } if (string.IsNullOrEmpty(result.Etag)) - return new Tuple(null, string.Empty, false); + { + result.HasChanged = false; + return result; + } if (result.Etag == Etag) - return new Tuple(null, string.Empty, false); + { + result.HasChanged = false; + return result; + } - return new Tuple(result.ToggleCollection, result.Etag, true); + return result; } } } \ No newline at end of file diff --git a/src/Unleash/Internal/UnleashServices.cs b/src/Unleash/Internal/UnleashServices.cs index e4481cab..e575aa8f 100644 --- a/src/Unleash/Internal/UnleashServices.cs +++ b/src/Unleash/Internal/UnleashServices.cs @@ -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; @@ -186,17 +186,19 @@ public void Dispose() ToggleCollection?.Dispose(); } - public async Task GetFeatureFlags() + public async Task 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; } } \ No newline at end of file diff --git a/src/Unleash/Scheduling/FetchFeatureTogglesTask.cs b/src/Unleash/Scheduling/FetchFeatureTogglesTask.cs index b2c3fe39..91399477 100644 --- a/src/Unleash/Scheduling/FetchFeatureTogglesTask.cs +++ b/src/Unleash/Scheduling/FetchFeatureTogglesTask.cs @@ -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); } }