Skip to content

Commit

Permalink
Merge branch 'master' into roll-to-chrome-125
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed May 22, 2024
2 parents 60e168a + 5d6c91c commit 4b4032e
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/PuppeteerSharp/Helpers/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ namespace PuppeteerSharp.Helpers
/// </summary>
public static class TaskHelper
{
private static readonly Func<TimeSpan, Exception> DefaultExceptionFactory =
private static readonly Func<TimeSpan, Exception> _defaultExceptionFactory =
timeout => new TimeoutException($"Timeout of {timeout.TotalMilliseconds} ms exceeded");

/// <summary>
/// Default timeout.
/// </summary>
public static int DefaultTimeout { get; set; } = 1_000;

// Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/

/// <summary>
Expand All @@ -24,10 +29,10 @@ public static class TaskHelper
/// <param name="cancellationToken">Cancellation token.</param>
public static Task WithTimeout(
this Task task,
int milliseconds = 1_000,
int? milliseconds = null,
Func<TimeSpan, Exception> exceptionFactory = null,
CancellationToken cancellationToken = default)
=> WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds), exceptionFactory, cancellationToken);
=> WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds ?? DefaultTimeout), exceptionFactory, cancellationToken);

// Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/

Expand All @@ -45,7 +50,7 @@ public static class TaskHelper
Func<TimeSpan, Exception> exceptionFactory = null,
CancellationToken cancellationToken = default)
=> task.WithTimeout(
() => throw (exceptionFactory ?? DefaultExceptionFactory)(timeout),
() => throw (exceptionFactory ?? _defaultExceptionFactory)(timeout),
timeout,
cancellationToken);

Expand Down Expand Up @@ -155,8 +160,8 @@ public static async Task<T> WithTimeout<T>(this Task<T> task, Action timeoutActi
/// <param name="milliseconds">Milliseconds timeout.</param>
/// <param name="exceptionFactory">Optional timeout exception factory.</param>
/// <typeparam name="T">Task return type.</typeparam>
public static Task<T> WithTimeout<T>(this Task<T> task, int milliseconds = 1_000, Func<TimeSpan, Exception> exceptionFactory = null)
=> WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds), exceptionFactory);
public static Task<T> WithTimeout<T>(this Task<T> task, int? milliseconds = null, Func<TimeSpan, Exception> exceptionFactory = null)
=> WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds ?? DefaultTimeout), exceptionFactory);

// Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/

Expand All @@ -177,7 +182,7 @@ public static async Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout,

if (await TimeoutTask(task, timeout).ConfigureAwait(false))
{
throw (exceptionFactory ?? DefaultExceptionFactory)(timeout);
throw (exceptionFactory ?? _defaultExceptionFactory)(timeout);
}

return await task.ConfigureAwait(false);
Expand Down

0 comments on commit 4b4032e

Please sign in to comment.