Skip to content

Fix: Remove unnecessary async state machine generation #10853

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

Merged
merged 5 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 12 additions & 24 deletions src/Files.App/Filesystem/StorageItems/NativeStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ public override IAsyncAction DeleteAsync(StorageDeleteOption option)

public override IAsyncOperation<BaseBasicProperties> GetBasicPropertiesAsync()
{
return AsyncInfo.Run(async (cancellationToken) =>
{
return new BaseBasicProperties();
});
return Task.FromResult(new BaseBasicProperties()).AsAsyncOperation();
}

public override IAsyncOperation<BaseStorageFolder> GetParentAsync()
Expand All @@ -143,18 +140,15 @@ public override IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(Thumbnai
public override IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options)
=> throw new NotSupportedException();

public static IAsyncOperation<BaseStorageFile> FromPathAsync(string path)
public static IAsyncOperation<BaseStorageFile>? FromPathAsync(string path)
{
return AsyncInfo.Run<BaseStorageFile>(async (cancellationToken) =>
if (IsNativePath(path) && CheckAccess(path))
{
if (IsNativePath(path) && CheckAccess(path))
{
var name = IO.Path.GetFileName(path);
return new NativeStorageFile(path, name.Substring(name.LastIndexOf(":") + 1), DateTime.Now);
}
return null;
});
}
var name = IO.Path.GetFileName(path);
return Task.FromResult((BaseStorageFile)new NativeStorageFile(path, name[(name.LastIndexOf(":") + 1)..], DateTime.Now)).AsAsyncOperation();
}
return null;
}

private static bool CheckAccess(string path)
{
Expand Down Expand Up @@ -207,11 +201,8 @@ public override IAsyncAction MoveAsync(IStorageFolder destinationFolder, string

public override IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode)
{
return AsyncInfo.Run<IRandomAccessStream>(async (cancellationToken) =>
{
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path, accessMode == FileAccessMode.ReadWrite);
return new FileStream(hFile, accessMode == FileAccessMode.ReadWrite ? FileAccess.ReadWrite : FileAccess.Read).AsRandomAccessStream();
});
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path, accessMode == FileAccessMode.ReadWrite);
return Task.FromResult(new FileStream(hFile, accessMode == FileAccessMode.ReadWrite ? FileAccess.ReadWrite : FileAccess.Read).AsRandomAccessStream()).AsAsyncOperation();
}

public override IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode, StorageOpenOptions options) => OpenAsync(accessMode);
Expand All @@ -226,11 +217,8 @@ public override IAsyncOperation<IRandomAccessStreamWithContentType> OpenReadAsyn

public override IAsyncOperation<IInputStream> OpenSequentialReadAsync()
{
return AsyncInfo.Run(async (cancellationToken) =>
{
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path);
return new FileStream(hFile, FileAccess.Read).AsInputStream();
});
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path);
return Task.FromResult(new FileStream(hFile, FileAccess.Read).AsInputStream()).AsAsyncOperation();
}

public override IAsyncOperation<StorageStreamTransaction> OpenTransactedWriteAsync()
Expand Down
29 changes: 13 additions & 16 deletions src/Files.App/Filesystem/StorageItems/ZipStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,24 @@ public override IAsyncOperation<StorageFile> ToStorageFileAsync()

public static IAsyncOperation<BaseStorageFile> FromPathAsync(string path)
{
return AsyncInfo.Run(cancellationToken =>
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
{
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation();
}
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
if (marker is not -1)
{
var containerPath = path.Substring(0, marker + ext.Length);
if (path == containerPath)
{
return Task.FromResult<BaseStorageFile>(null);
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation(); // Root
}
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
if (marker is not -1)
if (CheckAccess(containerPath))
{
var containerPath = path.Substring(0, marker + ext.Length);
if (path == containerPath)
{
return Task.FromResult<BaseStorageFile>(null); // Root
}
if (CheckAccess(containerPath))
{
return Task.FromResult<BaseStorageFile>(new ZipStorageFile(path, containerPath));
}
return Task.FromResult<BaseStorageFile>(new ZipStorageFile(path, containerPath)).AsAsyncOperation();
}
return Task.FromResult<BaseStorageFile>(null);
});
}
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation();
}

public override bool IsEqual(IStorageItem item) => item?.Path == Path;
Expand Down
25 changes: 11 additions & 14 deletions src/Files.App/Filesystem/StorageItems/ZipStorageFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,20 @@ public static async Task<bool> CheckDefaultZipApp(string filePath)

public static IAsyncOperation<BaseStorageFolder> FromPathAsync(string path)
{
return AsyncInfo.Run<BaseStorageFolder>(async (cancellationToken) =>
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
{
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
{
return null;
}
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
if (marker is not -1)
return null;
}
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
if (marker is not -1)
{
var containerPath = path.Substring(0, marker + ext.Length);
if (CheckAccess(containerPath))
{
var containerPath = path.Substring(0, marker + ext.Length);
if (CheckAccess(containerPath))
{
return new ZipStorageFolder(path, containerPath);
}
return Task.FromResult((BaseStorageFolder)new ZipStorageFolder(path, containerPath)).AsAsyncOperation();
}
return null;
});
}
return null;
}

public static IAsyncOperation<BaseStorageFolder> FromStorageFileAsync(BaseStorageFile file)
Expand Down