Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary async code #15941

Merged
merged 1 commit into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 9 additions & 26 deletions src/OrchardCore/OrchardCore/Extensions/ExtensionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OrchardCore.Environment.Extensions.Features;
Expand Down Expand Up @@ -37,7 +36,7 @@ public class ExtensionManager : IExtensionManager
private readonly ConcurrentDictionary<string, Lazy<IEnumerable<IFeatureInfo>>> _dependentFeatures = new();

private bool _isInitialized;
private readonly SemaphoreSlim _semaphore = new(1);
private readonly object _synLock = new();

public ExtensionManager(
IApplicationContext applicationContext,
Expand Down Expand Up @@ -81,7 +80,7 @@ public IEnumerable<IFeatureInfo> GetFeatures(string[] featureIdsToLoad)
EnsureInitialized();

var allDependencyIds = new HashSet<string>(featureIdsToLoad
.SelectMany(featureId => GetFeatureDependencies(featureId))
.SelectMany(GetFeatureDependencies)
.Select(x => x.Id));

foreach (var featureInfo in _featureInfos)
Expand All @@ -102,22 +101,22 @@ public Task<ExtensionEntry> LoadExtensionAsync(IExtensionInfo extensionInfo)
return Task.FromResult(extension);
}

public async Task<IEnumerable<FeatureEntry>> LoadFeaturesAsync()
public Task<IEnumerable<FeatureEntry>> LoadFeaturesAsync()
{
await EnsureInitializedAsync();
return _features.Values;
EnsureInitialized();
return Task.FromResult<IEnumerable<FeatureEntry>>(_features.Values);
}

public async Task<IEnumerable<FeatureEntry>> LoadFeaturesAsync(string[] featureIdsToLoad)
public Task<IEnumerable<FeatureEntry>> LoadFeaturesAsync(string[] featureIdsToLoad)
{
await EnsureInitializedAsync();
EnsureInitialized();

var features = new HashSet<string>(GetFeatures(featureIdsToLoad).Select(f => f.Id));

var loadedFeatures = _features.Values
.Where(f => features.Contains(f.FeatureInfo.Id));

return loadedFeatures;
return Task.FromResult<IEnumerable<FeatureEntry>>(loadedFeatures);
}

public IEnumerable<IFeatureInfo> GetFeatureDependencies(string featureId)
Expand Down Expand Up @@ -275,20 +274,8 @@ private void EnsureInitialized()
return;
}

EnsureInitializedAsync().GetAwaiter().GetResult();
}

private async Task EnsureInitializedAsync()
{
if (_isInitialized)
lock (_synLock)
{
return;
}

await _semaphore.WaitAsync();
try
{

if (_isInitialized)
{
return;
Expand Down Expand Up @@ -377,10 +364,6 @@ private async Task EnsureInitializedAsync()

_isInitialized = true;
}
finally
{
_semaphore.Release();
}
}

private static bool IsComponentType(Type type)
Expand Down