Skip to content

Commit 005ebd4

Browse files
IHaidovivan.haidov
and
ivan.haidov
authored
Code Quality: Enhance cancellation handling in methods (#16739)
Co-authored-by: ivan.haidov <[email protected]>
1 parent 4d7f200 commit 005ebd4

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public WidgetFileTagsContainerItem(string tagUid)
5959
/// <inheritdoc/>
6060
public async Task InitAsync(CancellationToken cancellationToken = default)
6161
{
62-
await foreach (var item in FileTagsService.GetItemsForTagAsync(_tagUid))
62+
await foreach (var item in FileTagsService.GetItemsForTagAsync(_tagUid, cancellationToken))
6363
{
6464
var icon = await ImageService.GetIconAsync(item.Storable, default);
6565
Tags.Add(new(item.Storable, icon));

src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ await DialogDisplayHelper.ShowDialogAsync(
166166
{
167167
// CopyFileFromApp only works on file not directories
168168
var fsSourceFolder = await source.ToStorageItemResult();
169-
var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
169+
var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken);
170170
var fsResult = (FilesystemResult)(fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode);
171171

172172
if (fsResult)
@@ -219,7 +219,7 @@ await DialogDisplayHelper.ShowDialogAsync(
219219
{
220220
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
221221

222-
FilesystemResult<BaseStorageFolder> destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
222+
FilesystemResult<BaseStorageFolder> destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken);
223223
var sourceResult = await source.ToStorageItemResult();
224224
fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode;
225225

@@ -373,7 +373,7 @@ await DialogDisplayHelper.ShowDialogAsync(
373373
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
374374

375375
var fsSourceFolder = await source.ToStorageItemResult();
376-
var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
376+
var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken);
377377
fsResult = fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode;
378378

379379
if (fsResult)
@@ -432,7 +432,7 @@ await DialogDisplayHelper.ShowDialogAsync(
432432
{
433433
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
434434

435-
FilesystemResult<BaseStorageFolder> destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
435+
FilesystemResult<BaseStorageFolder> destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken);
436436
var sourceResult = await source.ToStorageItemResult();
437437
fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode;
438438

@@ -512,12 +512,12 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItemWithPath source, IPro
512512
{
513513
if (source.ItemType == FilesystemItemType.File)
514514
{
515-
fsResult = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path)
515+
fsResult = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path, cancellationToken)
516516
.OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask());
517517
}
518518
else if (source.ItemType == FilesystemItemType.Directory)
519519
{
520-
fsResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path)
520+
fsResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path, cancellationToken)
521521
.OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask());
522522
}
523523
}
@@ -539,7 +539,7 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItemWithPath source, IPro
539539
// Recycle bin also stores a file starting with $I for each item
540540
string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I", StringComparison.Ordinal));
541541

542-
await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath)
542+
await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath, cancellationToken)
543543
.OnSuccess(iFile => iFile.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask());
544544
}
545545
fsProgress.ReportStatus(fsResult);
@@ -738,8 +738,8 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
738738
{
739739
if (source.ItemType == FilesystemItemType.Directory)
740740
{
741-
FilesystemResult<BaseStorageFolder> sourceFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path);
742-
FilesystemResult<BaseStorageFolder> destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
741+
FilesystemResult<BaseStorageFolder> sourceFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path, cancellationToken);
742+
FilesystemResult<BaseStorageFolder> destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken);
743743

744744
fsResult = sourceFolder.ErrorCode | destinationFolder.ErrorCode;
745745
fsProgress.ReportStatus(fsResult);
@@ -759,8 +759,8 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
759759
}
760760
else
761761
{
762-
FilesystemResult<BaseStorageFile> sourceFile = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path);
763-
FilesystemResult<BaseStorageFolder> destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
762+
FilesystemResult<BaseStorageFile> sourceFile = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path, cancellationToken);
763+
FilesystemResult<BaseStorageFolder> destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken);
764764

765765
fsResult = sourceFile.ErrorCode | destinationFolder.ErrorCode;
766766
fsProgress.ReportStatus(fsResult);
@@ -782,7 +782,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
782782
// Recycle bin also stores a file starting with $I for each item
783783
string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I", StringComparison.Ordinal));
784784

785-
await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath)
785+
await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath, cancellationToken)
786786
.OnSuccess(iFile => iFile.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask());
787787
}
788788

src/Files.App/Utils/Storage/Search/FolderSearch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ await Task.Run(() =>
336336
} while (hasNextFile);
337337

338338
Win32PInvoke.FindClose(hFile);
339-
});
339+
}, token);
340340
}
341341
}
342342

src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
8686
await using (var inStream = await this.OpenStreamForReadAsync())
8787
await using (var outStream = await destFile.OpenStreamForWriteAsync())
8888
{
89-
await inStream.CopyToAsync(outStream);
90-
await outStream.FlushAsync();
89+
await inStream.CopyToAsync(outStream, cancellationToken);
90+
await outStream.FlushAsync(cancellationToken);
9191
}
9292
}
9393
return destFile;
@@ -251,8 +251,8 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption
251251
await using (var inStream = await this.OpenStreamForReadAsync())
252252
await using (var outStream = await destFile.OpenStreamForWriteAsync())
253253
{
254-
await inStream.CopyToAsync(outStream);
255-
await outStream.FlushAsync();
254+
await inStream.CopyToAsync(outStream, cancellationToken);
255+
await outStream.FlushAsync(cancellationToken);
256256
}
257257
await DeleteAsync();
258258
}

src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public IAsyncOperation<bool> FlushAsync()
117117

118118
return AsyncInfo.Run<bool>(async (cancellationToken) =>
119119
{
120-
await stream.FlushAsync();
120+
await stream.FlushAsync(cancellationToken);
121121
return true;
122122
});
123123
}

src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
7878
await using (var inStream = await this.OpenStreamForReadAsync())
7979
await using (var outStream = await destFile.OpenStreamForWriteAsync())
8080
{
81-
await inStream.CopyToAsync(outStream);
82-
await outStream.FlushAsync();
81+
await inStream.CopyToAsync(outStream, cancellationToken);
82+
await outStream.FlushAsync(cancellationToken);
8383
}
8484
return destFile;
8585
}
@@ -96,8 +96,8 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
9696
await using (var inStream = await this.OpenStreamForReadAsync())
9797
await using (var outStream = new FileStream(hFile, FileAccess.Write))
9898
{
99-
await inStream.CopyToAsync(outStream);
100-
await outStream.FlushAsync();
99+
await inStream.CopyToAsync(outStream, cancellationToken);
100+
await outStream.FlushAsync(cancellationToken);
101101
}
102102
return new NativeStorageFile(destination, desiredNewName, DateTime.Now);
103103
}
@@ -143,8 +143,8 @@ public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace)
143143
await using var inStream = await this.OpenStreamForReadAsync();
144144
await using var outStream = await fileToReplace.OpenStreamForWriteAsync();
145145

146-
await inStream.CopyToAsync(outStream);
147-
await outStream.FlushAsync();
146+
await inStream.CopyToAsync(outStream, cancellationToken);
147+
await outStream.FlushAsync(cancellationToken);
148148
});
149149
}
150150
public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace)
@@ -154,8 +154,8 @@ public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace)
154154
await using var inStream = await this.OpenStreamForReadAsync();
155155
await using var outStream = await fileToReplace.OpenStreamForWriteAsync();
156156

157-
await inStream.CopyToAsync(outStream);
158-
await outStream.FlushAsync();
157+
await inStream.CopyToAsync(outStream, cancellationToken);
158+
await outStream.FlushAsync(cancellationToken);
159159
// Move unsupported, copy but do not delete original
160160
});
161161
}

src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
128128
await using (var inStream = await this.OpenStreamForReadAsync())
129129
await using (var outStream = await destFile.OpenStreamForWriteAsync())
130130
{
131-
await inStream.CopyToAsync(outStream);
132-
await outStream.FlushAsync();
131+
await inStream.CopyToAsync(outStream, cancellationToken);
132+
await outStream.FlushAsync(cancellationToken);
133133
}
134134
return destFile;
135135
}

0 commit comments

Comments
 (0)