Skip to content

Commit

Permalink
Expose TaskHelper default timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed May 13, 2024
1 parent 2eaba48 commit 94aa9a1
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/PuppeteerSharp/Helpers/TaskHelper.cs
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 @@ -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 94aa9a1

Please sign in to comment.