Skip to content

Commit f91ae1b

Browse files
authored
Fix: Removed unnecessary async state machine generation (#10853)
1 parent c3af5f0 commit f91ae1b

File tree

3 files changed

+36
-54
lines changed

3 files changed

+36
-54
lines changed

src/Files.App/Filesystem/StorageItems/NativeStorageFile.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ public override IAsyncAction DeleteAsync(StorageDeleteOption option)
125125

126126
public override IAsyncOperation<BaseBasicProperties> GetBasicPropertiesAsync()
127127
{
128-
return AsyncInfo.Run(async (cancellationToken) =>
129-
{
130-
return new BaseBasicProperties();
131-
});
128+
return Task.FromResult(new BaseBasicProperties()).AsAsyncOperation();
132129
}
133130

134131
public override IAsyncOperation<BaseStorageFolder> GetParentAsync()
@@ -145,16 +142,13 @@ public override IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(Thumbnai
145142

146143
public static IAsyncOperation<BaseStorageFile> FromPathAsync(string path)
147144
{
148-
return AsyncInfo.Run<BaseStorageFile>(async (cancellationToken) =>
145+
if (IsNativePath(path) && CheckAccess(path))
149146
{
150-
if (IsNativePath(path) && CheckAccess(path))
151-
{
152-
var name = IO.Path.GetFileName(path);
153-
return new NativeStorageFile(path, name.Substring(name.LastIndexOf(":") + 1), DateTime.Now);
154-
}
155-
return null;
156-
});
157-
}
147+
var name = IO.Path.GetFileName(path);
148+
return Task.FromResult((BaseStorageFile)new NativeStorageFile(path, name[(name.LastIndexOf(":") + 1)..], DateTime.Now)).AsAsyncOperation();
149+
}
150+
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation();
151+
}
158152

159153
private static bool CheckAccess(string path)
160154
{
@@ -207,11 +201,8 @@ public override IAsyncAction MoveAsync(IStorageFolder destinationFolder, string
207201

208202
public override IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode)
209203
{
210-
return AsyncInfo.Run<IRandomAccessStream>(async (cancellationToken) =>
211-
{
212-
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path, accessMode == FileAccessMode.ReadWrite);
213-
return new FileStream(hFile, accessMode == FileAccessMode.ReadWrite ? FileAccess.ReadWrite : FileAccess.Read).AsRandomAccessStream();
214-
});
204+
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path, accessMode == FileAccessMode.ReadWrite);
205+
return Task.FromResult(new FileStream(hFile, accessMode == FileAccessMode.ReadWrite ? FileAccess.ReadWrite : FileAccess.Read).AsRandomAccessStream()).AsAsyncOperation();
215206
}
216207

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

227218
public override IAsyncOperation<IInputStream> OpenSequentialReadAsync()
228219
{
229-
return AsyncInfo.Run(async (cancellationToken) =>
230-
{
231-
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path);
232-
return new FileStream(hFile, FileAccess.Read).AsInputStream();
233-
});
220+
var hFile = NativeFileOperationsHelper.OpenFileForRead(Path);
221+
return Task.FromResult(new FileStream(hFile, FileAccess.Read).AsInputStream()).AsAsyncOperation();
234222
}
235223

236224
public override IAsyncOperation<StorageStreamTransaction> OpenTransactedWriteAsync()
@@ -272,4 +260,4 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption
272260
public override IAsyncOperation<StorageFile> ToStorageFileAsync()
273261
=> throw new NotSupportedException();
274262
}
275-
}
263+
}

src/Files.App/Filesystem/StorageItems/ZipStorageFile.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,24 @@ public override IAsyncOperation<StorageFile> ToStorageFileAsync()
6464

6565
public static IAsyncOperation<BaseStorageFile> FromPathAsync(string path)
6666
{
67-
return AsyncInfo.Run(cancellationToken =>
67+
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
6868
{
69-
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
69+
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation();
70+
}
71+
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
72+
if (marker is not -1)
73+
{
74+
var containerPath = path.Substring(0, marker + ext.Length);
75+
if (path == containerPath)
7076
{
71-
return Task.FromResult<BaseStorageFile>(null);
77+
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation(); // Root
7278
}
73-
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
74-
if (marker is not -1)
79+
if (CheckAccess(containerPath))
7580
{
76-
var containerPath = path.Substring(0, marker + ext.Length);
77-
if (path == containerPath)
78-
{
79-
return Task.FromResult<BaseStorageFile>(null); // Root
80-
}
81-
if (CheckAccess(containerPath))
82-
{
83-
return Task.FromResult<BaseStorageFile>(new ZipStorageFile(path, containerPath));
84-
}
81+
return Task.FromResult<BaseStorageFile>(new ZipStorageFile(path, containerPath)).AsAsyncOperation();
8582
}
86-
return Task.FromResult<BaseStorageFile>(null);
87-
});
83+
}
84+
return Task.FromResult<BaseStorageFile>(null).AsAsyncOperation();
8885
}
8986

9087
public override bool IsEqual(IStorageItem item) => item?.Path == Path;

src/Files.App/Filesystem/StorageItems/ZipStorageFolder.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,20 @@ public static async Task<bool> CheckDefaultZipApp(string filePath)
112112

113113
public static IAsyncOperation<BaseStorageFolder> FromPathAsync(string path)
114114
{
115-
return AsyncInfo.Run<BaseStorageFolder>(async (cancellationToken) =>
115+
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
116116
{
117-
if (!FileExtensionHelpers.IsBrowsableZipFile(path, out var ext))
118-
{
119-
return null;
120-
}
121-
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
122-
if (marker is not -1)
117+
return Task.FromResult<BaseStorageFolder>(null).AsAsyncOperation();
118+
}
119+
var marker = path.IndexOf(ext, StringComparison.OrdinalIgnoreCase);
120+
if (marker is not -1)
121+
{
122+
var containerPath = path.Substring(0, marker + ext.Length);
123+
if (CheckAccess(containerPath))
123124
{
124-
var containerPath = path.Substring(0, marker + ext.Length);
125-
if (CheckAccess(containerPath))
126-
{
127-
return new ZipStorageFolder(path, containerPath);
128-
}
125+
return Task.FromResult((BaseStorageFolder)new ZipStorageFolder(path, containerPath)).AsAsyncOperation();
129126
}
130-
return null;
131-
});
127+
}
128+
return Task.FromResult<BaseStorageFolder>(null).AsAsyncOperation();
132129
}
133130

134131
public static IAsyncOperation<BaseStorageFolder> FromStorageFileAsync(BaseStorageFile file)

0 commit comments

Comments
 (0)