|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Diagnostics; |
4 | 4 | using System.IO; |
5 | | -using System.Linq; |
6 | 5 | using System.Text.Json; |
7 | 6 | using System.Threading.Tasks; |
8 | | -using System.Timers; |
9 | 7 | using PuppeteerSharp.Cdp; |
10 | 8 | using PuppeteerSharp.Cdp.Messaging; |
11 | 9 | using PuppeteerSharp.Helpers; |
@@ -43,14 +41,10 @@ public abstract class Page : IPage |
43 | 41 |
|
44 | 42 | private readonly TaskQueue _screenshotTaskQueue; |
45 | 43 | private readonly ConcurrentSet<Func<IRequest, Task>> _requestInterceptionTask = []; |
46 | | - private readonly ConcurrentSet<IRequest> _requests = new(); |
47 | | - private readonly TaskCompletionSource<bool> _closeTaskCompletionSource = |
48 | | - new(TaskCreationOptions.RunContinuationsAsynchronously); |
49 | 44 |
|
50 | 45 | internal Page(TaskQueue screenshotTaskQueue) |
51 | 46 | { |
52 | 47 | _screenshotTaskQueue = screenshotTaskQueue; |
53 | | - Request += (_, e) => _requests.Add(e.Request); |
54 | 48 | } |
55 | 49 |
|
56 | 50 | /// <inheritdoc/> |
@@ -234,9 +228,6 @@ public int DefaultTimeout |
234 | 228 | /// </summary> |
235 | 229 | protected ScreenshotOptions ScreenshotBurstModeOptions { get; set; } |
236 | 230 |
|
237 | | - private int NumRequestsInProgress |
238 | | - => _requests.Count(r => r.Response == null); |
239 | | - |
240 | 231 | /// <inheritdoc/> |
241 | 232 | public abstract Task SetGeolocationAsync(GeolocationOption options); |
242 | 233 |
|
@@ -692,58 +683,7 @@ public Task<IResponse> WaitForNavigationAsync(NavigationOptions options = null) |
692 | 683 | => MainFrame.WaitForNavigationAsync(options); |
693 | 684 |
|
694 | 685 | /// <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); |
747 | 687 |
|
748 | 688 | /// <inheritdoc/> |
749 | 689 | public Task<IRequest> WaitForRequestAsync(string url, WaitForOptions options = null) |
@@ -945,11 +885,7 @@ protected void OnRequest(IRequest request) |
945 | 885 | /// <summary> |
946 | 886 | /// Raises the <see cref="Close"/> event. |
947 | 887 | /// </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); |
953 | 889 |
|
954 | 890 | /// <summary> |
955 | 891 | /// Raises the <see cref="Console"/> event. |
|
0 commit comments