Skip to content

Commit

Permalink
Improved access to files to use true asynchronous methods
Browse files Browse the repository at this point in the history
- if file is not opened with parameter "useAsync: true", all calls to async method are in fact just wrappers to the synchronous methods, which might hurt performance
- you can read more info on this topic in article https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/using-async-for-file-access
  • Loading branch information
xMarkos authored and Marek Hübsch committed Aug 3, 2018
1 parent f19813b commit 7168795
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/ElementHandle.cs
Expand Up @@ -61,7 +61,7 @@ public async Task ScreenshotAsync(string file, ScreenshotOptions options)

var data = await ScreenshotDataAsync(options).ConfigureAwait(false);

using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, true))
{
await fs.WriteAsync(data, 0, data.Length).ConfigureAwait(false);
}
Expand Down
16 changes: 14 additions & 2 deletions lib/PuppeteerSharp/Frame.cs
Expand Up @@ -399,7 +399,13 @@ public async Task<ElementHandle> AddStyleTag(AddTagOptions options)

if (!string.IsNullOrEmpty(options.Path))
{
var contents = File.ReadAllText(options.Path, Encoding.UTF8);
string contents;
using (var fs = new FileStream(options.Path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
using (var reader = new StreamReader(fs, Encoding.UTF8))
{
contents = await reader.ReadToEndAsync().ConfigureAwait(false);
}

contents += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);
var context = await GetExecutionContextAsync().ConfigureAwait(false);
return (await context.EvaluateFunctionHandleAsync(addStyleContent, contents).ConfigureAwait(false)) as ElementHandle;
Expand Down Expand Up @@ -471,7 +477,13 @@ async Task<ElementHandle> AddScriptTagPrivate(string script, string urlOrContent

if (!string.IsNullOrEmpty(options.Path))
{
var contents = File.ReadAllText(options.Path, Encoding.UTF8);
string contents;
using (var fs = new FileStream(options.Path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
using (var reader = new StreamReader(fs, Encoding.UTF8))
{
contents = await reader.ReadToEndAsync().ConfigureAwait(false);
}

contents += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);
return await AddScriptTagPrivate(addScriptContent, contents, options.Type).ConfigureAwait(false);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/PuppeteerSharp/Page.cs
Expand Up @@ -746,8 +746,8 @@ public Task<Response> GoToAsync(string url, int? timeout = null, WaitUntilNaviga
public async Task PdfAsync(string file, PdfOptions options)
{
var data = await PdfDataAsync(options).ConfigureAwait(false);

using (var fs = File.OpenWrite(file))

using (var fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 4096, true))
{
await fs.WriteAsync(data, 0, data.Length).ConfigureAwait(false);
}
Expand Down Expand Up @@ -923,7 +923,7 @@ public async Task ScreenshotAsync(string file, ScreenshotOptions options)
}
var data = await ScreenshotDataAsync(options).ConfigureAwait(false);

using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, true))
{
await fs.WriteAsync(data, 0, data.Length).ConfigureAwait(false);
}
Expand Down

0 comments on commit 7168795

Please sign in to comment.