Skip to content

Commit 6ab434f

Browse files
authored
Revert "Refactor WaitForNetworkIdle" (#2878)
* Revert "Refactor WaitForNetworkIdle (#2824)" This reverts commit 399890f. * Bump version
1 parent d9fdbfd commit 6ab434f

6 files changed

+67
-75
lines changed

lib/PuppeteerSharp/Cdp/CdpPage.cs

+56
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,62 @@ await Task.WhenAll(
419419
return navigationTask.Result;
420420
}
421421

422+
/// <inheritdoc/>
423+
public override async Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null)
424+
{
425+
var timeout = options?.Timeout ?? DefaultTimeout;
426+
var idleTime = options?.IdleTime ?? 500;
427+
428+
var networkIdleTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
429+
430+
var idleTimer = new Timer { Interval = idleTime, };
431+
432+
idleTimer.Elapsed += (_, _) => { networkIdleTcs.TrySetResult(true); };
433+
434+
var networkManager = FrameManager.NetworkManager;
435+
436+
void Evaluate()
437+
{
438+
idleTimer.Stop();
439+
440+
if (networkManager.NumRequestsInProgress == 0)
441+
{
442+
idleTimer.Start();
443+
}
444+
}
445+
446+
void RequestEventListener(object sender, RequestEventArgs e) => Evaluate();
447+
void ResponseEventListener(object sender, ResponseCreatedEventArgs e) => Evaluate();
448+
449+
void Cleanup()
450+
{
451+
idleTimer.Stop();
452+
idleTimer.Dispose();
453+
454+
networkManager.Request -= RequestEventListener;
455+
networkManager.Response -= ResponseEventListener;
456+
}
457+
458+
networkManager.Request += RequestEventListener;
459+
networkManager.Response += ResponseEventListener;
460+
461+
Evaluate();
462+
463+
await Task.WhenAny(networkIdleTcs.Task, SessionClosedTask).WithTimeout(timeout, t =>
464+
{
465+
Cleanup();
466+
467+
return new TimeoutException($"Timeout of {t.TotalMilliseconds} ms exceeded");
468+
}).ConfigureAwait(false);
469+
470+
Cleanup();
471+
472+
if (SessionClosedTask.IsFaulted)
473+
{
474+
await SessionClosedTask.ConfigureAwait(false);
475+
}
476+
}
477+
422478
/// <inheritdoc/>
423479
public override async Task<IRequest> WaitForRequestAsync(Func<IRequest, bool> predicate, WaitForOptions options = null)
424480
{

lib/PuppeteerSharp/Cdp/NetworkEventManager.cs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal class NetworkEventManager
1414
private readonly ConcurrentDictionary<string, List<RedirectInfo>> _queuedRedirectInfoMap = new();
1515
private readonly ConcurrentDictionary<string, List<ResponseReceivedExtraInfoResponse>> _responseReceivedExtraInfoMap = new();
1616

17+
public int NumRequestsInProgress
18+
=> _httpRequestsMap.Values.Count(r => r.Response == null);
19+
1720
internal void Forget(string requestId)
1821
{
1922
_requestWillBeSentMap.TryRemove(requestId, out _);

lib/PuppeteerSharp/Cdp/NetworkManager.cs

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ internal NetworkManager(bool acceptInsecureCerts, IFrameProvider frameManager, I
5656

5757
internal Dictionary<string, string> ExtraHTTPHeaders => _extraHTTPHeaders?.Clone();
5858

59+
internal int NumRequestsInProgress => _networkEventManager.NumRequestsInProgress;
60+
5961
internal Task AddClientAsync(ICDPSession client)
6062
{
6163
if (_clients.ContainsKey(client))

lib/PuppeteerSharp/Page.cs

+2-66
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5-
using System.Linq;
65
using System.Text.Json;
76
using System.Threading.Tasks;
8-
using System.Timers;
97
using PuppeteerSharp.Cdp;
108
using PuppeteerSharp.Cdp.Messaging;
119
using PuppeteerSharp.Helpers;
@@ -43,14 +41,10 @@ public abstract class Page : IPage
4341

4442
private readonly TaskQueue _screenshotTaskQueue;
4543
private readonly ConcurrentSet<Func<IRequest, Task>> _requestInterceptionTask = [];
46-
private readonly ConcurrentSet<IRequest> _requests = new();
47-
private readonly TaskCompletionSource<bool> _closeTaskCompletionSource =
48-
new(TaskCreationOptions.RunContinuationsAsynchronously);
4944

5045
internal Page(TaskQueue screenshotTaskQueue)
5146
{
5247
_screenshotTaskQueue = screenshotTaskQueue;
53-
Request += (_, e) => _requests.Add(e.Request);
5448
}
5549

5650
/// <inheritdoc/>
@@ -234,9 +228,6 @@ public int DefaultTimeout
234228
/// </summary>
235229
protected ScreenshotOptions ScreenshotBurstModeOptions { get; set; }
236230

237-
private int NumRequestsInProgress
238-
=> _requests.Count(r => r.Response == null);
239-
240231
/// <inheritdoc/>
241232
public abstract Task SetGeolocationAsync(GeolocationOption options);
242233

@@ -692,58 +683,7 @@ public Task<IResponse> WaitForNavigationAsync(NavigationOptions options = null)
692683
=> MainFrame.WaitForNavigationAsync(options);
693684

694685
/// <inheritdoc/>
695-
public async Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null)
696-
{
697-
var timeout = options?.Timeout ?? DefaultTimeout;
698-
var idleTime = options?.IdleTime ?? 500;
699-
700-
var networkIdleTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
701-
702-
var idleTimer = new Timer { Interval = idleTime, };
703-
704-
idleTimer.Elapsed += (_, _) => { networkIdleTcs.TrySetResult(true); };
705-
706-
void Evaluate()
707-
{
708-
idleTimer.Stop();
709-
710-
if (NumRequestsInProgress <= (options?.Concurrency ?? 0))
711-
{
712-
idleTimer.Start();
713-
}
714-
}
715-
716-
void RequestEventListener(object sender, RequestEventArgs e) => Evaluate();
717-
void ResponseEventListener(object sender, ResponseCreatedEventArgs e) => Evaluate();
718-
719-
void Cleanup()
720-
{
721-
idleTimer.Stop();
722-
idleTimer.Dispose();
723-
724-
Request -= RequestEventListener;
725-
Response -= ResponseEventListener;
726-
}
727-
728-
Request += RequestEventListener;
729-
Response += ResponseEventListener;
730-
731-
Evaluate();
732-
733-
await Task.WhenAny(networkIdleTcs.Task, _closeTaskCompletionSource.Task).WithTimeout(timeout, t =>
734-
{
735-
Cleanup();
736-
737-
return new TimeoutException($"Timeout of {t.TotalMilliseconds} ms exceeded");
738-
}).ConfigureAwait(false);
739-
740-
Cleanup();
741-
742-
if (_closeTaskCompletionSource.Task.IsFaulted)
743-
{
744-
await _closeTaskCompletionSource.Task.ConfigureAwait(false);
745-
}
746-
}
686+
public abstract Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null);
747687

748688
/// <inheritdoc/>
749689
public Task<IRequest> WaitForRequestAsync(string url, WaitForOptions options = null)
@@ -945,11 +885,7 @@ protected void OnRequest(IRequest request)
945885
/// <summary>
946886
/// Raises the <see cref="Close"/> event.
947887
/// </summary>
948-
protected void OnClose()
949-
{
950-
_closeTaskCompletionSource?.TrySetException(new TargetClosedException("Target closed", "Session closed"));
951-
Close?.Invoke(this, EventArgs.Empty);
952-
}
888+
protected void OnClose() => Close?.Invoke(this, EventArgs.Empty);
953889

954890
/// <summary>
955891
/// Raises the <see cref="Console"/> event.

lib/PuppeteerSharp/PuppeteerSharp.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<Description>Headless Browser .NET API</Description>
1313
<PackageId>PuppeteerSharp</PackageId>
1414
<PackageReleaseNotes></PackageReleaseNotes>
15-
<PackageVersion>20.1.0</PackageVersion>
16-
<ReleaseVersion>20.1.0</ReleaseVersion>
17-
<AssemblyVersion>20.1.0</AssemblyVersion>
18-
<FileVersion>20.1.0</FileVersion>
15+
<PackageVersion>20.1.1</PackageVersion>
16+
<ReleaseVersion>20.1.1</ReleaseVersion>
17+
<AssemblyVersion>20.1.1</AssemblyVersion>
18+
<FileVersion>20.1.1</FileVersion>
1919
<SynchReleaseVersion>false</SynchReleaseVersion>
2020
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
2121
<DebugType>embedded</DebugType>

lib/PuppeteerSharp/WaitForNetworkIdleOptions.cs

-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,5 @@ public class WaitForNetworkIdleOptions : WaitForOptions
1010
/// How long to wait for no network requests in milliseconds, defaults to 500 milliseconds.
1111
/// </summary>
1212
public int? IdleTime { get; set; }
13-
14-
/// <summary>
15-
/// Maximum number concurrent of network connections to be considered inactive.
16-
/// </summary>
17-
public int Concurrency { get; set; }
1813
}
1914
}

0 commit comments

Comments
 (0)