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

Code Quality: Reduced usage of Await inside foreach loops #15005

Merged
merged 45 commits into from Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1137d19
Change `async void` ForEach loops to Task.WhenAll
gumbarros Mar 20, 2024
2b39496
Remove unnecessary `.ToList()` calls
gumbarros Mar 20, 2024
b7e0d5c
Convert places that cannot run concurrently to for each loop.
gumbarros Mar 20, 2024
9c450b5
Merge branch 'main' into await-foreach
gumbarros Mar 20, 2024
34251f4
Revert to Task.WhenAll
gumbarros Mar 20, 2024
c5400db
Merge branch 'main' into await-foreach
gumbarros Mar 20, 2024
bdfac31
Convert Task.WhenAll to for each loop.
gumbarros Mar 20, 2024
1c76c0a
Merge remote-tracking branch 'gumbarros/await-foreach' into await-for…
gumbarros Mar 20, 2024
468f60e
Use single foreach loop at ShellContextFlyoutHelper.cs
gumbarros Mar 20, 2024
cfae7a4
ShellContextFlyoutHelper.cs formatting
gumbarros Mar 20, 2024
13916c5
Merge branch 'main' into await-foreach
gumbarros Mar 21, 2024
a8b024c
Removed async Select
gumbarros Mar 25, 2024
3dc6d27
Use Task.WhenAll for both files and folders
gumbarros Mar 25, 2024
a5fd62d
Merge branch 'main' into await-foreach
gumbarros Mar 26, 2024
b54a066
Merge branch 'main' into await-foreach
gumbarros Mar 29, 2024
c66c891
Merge branch 'main' into await-foreach
gumbarros Mar 31, 2024
aeaf561
Use foreach instead of Task.WhenAll at OpenAllTaggedActions.cs
gumbarros Mar 31, 2024
ff403ed
Update src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs
gumbarros Apr 7, 2024
d5820b8
Use OpenPathInNewTag for folderPaths
gumbarros Apr 7, 2024
437ac5b
Update src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs
gumbarros Apr 7, 2024
f39ae34
Update src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs
gumbarros Apr 7, 2024
42baa68
Update src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs
gumbarros Apr 7, 2024
47ddf8e
Removed curly braces from OpenAllTaggedActions.cs
gumbarros Apr 7, 2024
f0dd74e
Merge remote-tracking branch 'gumbarros/await-foreach' into await-for…
gumbarros Apr 7, 2024
1f3114c
Merge branch 'main' into await-foreach
gumbarros Apr 7, 2024
1651ebb
Update OpenAllTaggedActions.cs
gumbarros Apr 7, 2024
eb675da
Merge branch 'main' into await-foreach
gumbarros Apr 8, 2024
a6a8768
Merge removeTasks in single line.
gumbarros Apr 8, 2024
f5588d4
Merge branch 'main' into await-foreach
gumbarros Apr 9, 2024
e55255a
Merge branch 'main' into await-foreach
gumbarros Apr 10, 2024
f7f2785
Merge branch 'main' into await-foreach
gumbarros Apr 13, 2024
2a3f706
Use `Task.WhenAll` where appropriate
gumbarros Apr 13, 2024
37d4c59
Merge remote-tracking branch 'gumbarros/await-foreach' into await-for…
gumbarros Apr 13, 2024
099d9f9
Update src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs
gumbarros Apr 13, 2024
c06eb69
Update src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs
gumbarros Apr 13, 2024
3068557
Change to Task.WhenAll
gumbarros Apr 13, 2024
95327d0
Merge remote-tracking branch 'gumbarros/await-foreach' into await-for…
gumbarros Apr 13, 2024
a7664e6
Don't wait the removal tasks
gumbarros Apr 13, 2024
9920480
AddItemsToMainMenu asynchronously
gumbarros Apr 13, 2024
175e1ce
Don't await folder removal
gumbarros Apr 13, 2024
a62c7e8
Better readability
gumbarros Apr 13, 2024
820294e
Fix comments and use single `Task.WhenAll` for submenu
gumbarros Apr 14, 2024
8c57090
Merge branch 'main' into await-foreach
gumbarros Apr 14, 2024
eff7d6a
Move await to the end of the method
gumbarros Apr 14, 2024
8e569e3
Merge remote-tracking branch 'gumbarros/await-foreach' into await-for…
gumbarros Apr 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here look good but I'd like some more time to go over the other files.

Expand Up @@ -38,8 +38,13 @@ public async Task ExecuteAsync()

await Task.WhenAll(files.Select(file
=> NavigationHelpers.OpenPath(file.path, _pageContext.ShellPage!)));

folders.ForEach(async folder => await NavigationHelpers.OpenPathInNewTab(folder.path, false));

var openTasks = folders.Select(async folder =>
{
await NavigationHelpers.OpenPathInNewTab(folder.path, false);
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
});

await Task.WhenAll(openTasks);
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
28 changes: 19 additions & 9 deletions src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs
Expand Up @@ -359,20 +359,30 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
itemContextMenuFlyout.SecondaryCommands.Insert(1, sendToItems.FirstOrDefault());
}

// Add items to shell submenu
shellMenuItems.Where(x => x.LoadSubMenuAction is not null).ForEach(async x =>
{
await x.LoadSubMenuAction();

if (!UserSettingsService.GeneralSettingsService.MoveShellExtensionsToSubMenu)
var itemsWithSubMenu = shellMenuItems.Where(x => x.LoadSubMenuAction is not null);
yaira2 marked this conversation as resolved.
Show resolved Hide resolved

await Task.WhenAll(itemsWithSubMenu.Select(x => x.LoadSubMenuAction()));

if (!UserSettingsService.GeneralSettingsService.MoveShellExtensionsToSubMenu)
{
foreach (var item in itemsWithSubMenu)
{
AddItemsToMainMenu(itemContextMenuFlyout.SecondaryCommands, x);
AddItemsToMainMenu(itemContextMenuFlyout.SecondaryCommands, item);
}
else if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is AppBarButton overflowItem)
}
else
{
var overflowItem = itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") as AppBarButton;

if (overflowItem != null)
{
AddItemsToOverflowMenu(overflowItem, x);
foreach (var item in itemsWithSubMenu)
{
AddItemsToOverflowMenu(overflowItem, item);
}
}
});
}
}
catch { }
}
Expand Down
17 changes: 11 additions & 6 deletions src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs
Expand Up @@ -161,11 +161,13 @@ public async Task<ReturnResult> DeleteItemsAsync(IEnumerable<IStorageItemWithPat

if (!permanently && registerHistory)
App.HistoryWrapper.AddHistory(history);


// Create a list of tasks to remove items from the jump list asynchronously
var removeTasks = source.Select(x => jumpListService.RemoveFolderAsync(x.Path));

var itemsDeleted = history?.Source.Count ?? 0;

// Remove items from jump list
source.ForEach(async x => await jumpListService.RemoveFolderAsync(x.Path));
// Await all removal tasks to complete concurrently
await Task.WhenAll(removeTasks);
yaira2 marked this conversation as resolved.
Show resolved Hide resolved

var itemsCount = banner.TotalItemsCount;

Expand Down Expand Up @@ -476,8 +478,11 @@ public async Task<ReturnResult> MoveItemsAsync(IEnumerable<IStorageItemWithPath>
App.HistoryWrapper.AddHistory(history);
}

// Remove items from jump list
source.ForEach(async x => await jumpListService.RemoveFolderAsync(x.Path));
// Create a list of tasks to remove items from the jump list asynchronously
var removeTasks = source.Select(x => jumpListService.RemoveFolderAsync(x.Path));

// Await all removal tasks to complete
await Task.WhenAll(removeTasks);
yaira2 marked this conversation as resolved.
Show resolved Hide resolved

var itemsCount = banner.TotalItemsCount;

Expand Down
20 changes: 12 additions & 8 deletions src/Files.App/Views/Layouts/BaseLayoutPage.cs
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -901,21 +901,25 @@ private async Task AddShellMenuItemsAsync(List<ContextMenuFlyoutItemViewModel> s
}
}

// Add items to main shell submenu
mainShellMenuItems.Where(x => x.LoadSubMenuAction is not null).ForEach(async x =>
// Filter mainShellMenuItems that have a non-null LoadSubMenuAction
var mainItemsWithSubMenu = mainShellMenuItems.Where(x => x.LoadSubMenuAction is not null).ToList();
gumbarros marked this conversation as resolved.
Show resolved Hide resolved

// Load main submenus asynchronously
await Task.WhenAll(mainItemsWithSubMenu.Select(async x =>
{
await x.LoadSubMenuAction();

ShellContextFlyoutFactory.AddItemsToMainMenu(mainItems, x);
});
}));

// Add items to overflow shell submenu
overflowShellMenuItems.Where(x => x.LoadSubMenuAction is not null).ForEach(async x =>
// Filter overflowShellMenuItems that have a non-null LoadSubMenuAction
var overflowItemsWithSubMenu = overflowShellMenuItems.Where(x => x.LoadSubMenuAction is not null).ToList();

// Load overflow submenus asynchronously
await Task.WhenAll(overflowItemsWithSubMenu.Select(async x =>
{
await x.LoadSubMenuAction();

ShellContextFlyoutFactory.AddItemsToOverflowMenu(overflowItem, x);
});
}));

itemsControl?.Items.OfType<FrameworkElement>().ForEach(item =>
{
Expand Down