@@ -9,9 +9,14 @@ namespace PuppeteerSharp.Helpers
99 /// </summary>
1010 public static class TaskHelper
1111 {
12- private static readonly Func < TimeSpan , Exception > DefaultExceptionFactory =
12+ private static readonly Func < TimeSpan , Exception > _defaultExceptionFactory =
1313 timeout => new TimeoutException ( $ "Timeout of { timeout . TotalMilliseconds } ms exceeded") ;
1414
15+ /// <summary>
16+ /// Default timeout.
17+ /// </summary>
18+ public static int DefaultTimeout { get ; set ; } = 1_000 ;
19+
1520 // Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/
1621
1722 /// <summary>
@@ -24,10 +29,10 @@ public static class TaskHelper
2429 /// <param name="cancellationToken">Cancellation token.</param>
2530 public static Task WithTimeout (
2631 this Task task ,
27- int milliseconds = 1_000 ,
32+ int ? milliseconds = null ,
2833 Func < TimeSpan , Exception > exceptionFactory = null ,
2934 CancellationToken cancellationToken = default )
30- => WithTimeout ( task , TimeSpan . FromMilliseconds ( milliseconds ) , exceptionFactory , cancellationToken ) ;
35+ => WithTimeout ( task , TimeSpan . FromMilliseconds ( milliseconds ?? DefaultTimeout ) , exceptionFactory , cancellationToken ) ;
3136
3237 // Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/
3338
@@ -45,7 +50,7 @@ public static Task WithTimeout(
4550 Func < TimeSpan , Exception > exceptionFactory = null ,
4651 CancellationToken cancellationToken = default )
4752 => task . WithTimeout (
48- ( ) => throw ( exceptionFactory ?? DefaultExceptionFactory ) ( timeout ) ,
53+ ( ) => throw ( exceptionFactory ?? _defaultExceptionFactory ) ( timeout ) ,
4954 timeout ,
5055 cancellationToken ) ;
5156
@@ -155,8 +160,8 @@ public static async Task<T> WithTimeout<T>(this Task<T> task, Action timeoutActi
155160 /// <param name="milliseconds">Milliseconds timeout.</param>
156161 /// <param name="exceptionFactory">Optional timeout exception factory.</param>
157162 /// <typeparam name="T">Task return type.</typeparam>
158- public static Task < T > WithTimeout < T > ( this Task < T > task , int milliseconds = 1_000 , Func < TimeSpan , Exception > exceptionFactory = null )
159- => WithTimeout ( task , TimeSpan . FromMilliseconds ( milliseconds ) , exceptionFactory ) ;
163+ public static Task < T > WithTimeout < T > ( this Task < T > task , int ? milliseconds = null , Func < TimeSpan , Exception > exceptionFactory = null )
164+ => WithTimeout ( task , TimeSpan . FromMilliseconds ( milliseconds ?? DefaultTimeout ) , exceptionFactory ) ;
160165
161166 // Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/
162167
@@ -177,7 +182,7 @@ public static async Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout,
177182
178183 if ( await TimeoutTask ( task , timeout ) . ConfigureAwait ( false ) )
179184 {
180- throw ( exceptionFactory ?? DefaultExceptionFactory ) ( timeout ) ;
185+ throw ( exceptionFactory ?? _defaultExceptionFactory ) ( timeout ) ;
181186 }
182187
183188 return await task . ConfigureAwait ( false ) ;
0 commit comments