Skip to content

Commit 58f2b31

Browse files
authored
Fix repetititive setContent calls (#907)
* Fix repetitative setContent calls * Fix watcher order
1 parent e4f1bd2 commit 58f2b31

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

lib/PuppeteerSharp.Tests/PageTests/SetContentTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task ShouldWorkWithHtml4Doctype()
4646
}
4747

4848
[Fact]
49-
public async Task ShouldAwaitResourceToLoad()
49+
public async Task ShouldAwaitResourcesToLoad()
5050
{
5151
var imgPath = "/img.png";
5252
var imgResponse = new TaskCompletionSource<bool>();
@@ -60,5 +60,14 @@ public async Task ShouldAwaitResourceToLoad()
6060
imgResponse.SetResult(true);
6161
await contentTask;
6262
}
63+
64+
[Fact]
65+
public async Task ShouldWorkFastEnough()
66+
{
67+
for (var i = 0; i < 20; ++i)
68+
{
69+
await Page.SetContentAsync("<div>yo</div>");
70+
}
71+
}
6372
}
6473
}

lib/PuppeteerSharp/Frame.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ public async Task SetContentAsync(string html, NavigationOptions options = null)
564564
{
565565
var waitUntil = options?.WaitUntil ?? new[] { WaitUntilNavigation.Load };
566566
var timeout = options?.Timeout ?? Puppeteer.DefaultTimeout;
567-
var watcher = new LifecycleWatcher(FrameManager, this, waitUntil, timeout);
568567

569568
// We rely upon the fact that document.open() will reset frame lifecycle with "init"
570569
// lifecycle event. @see https://crrev.com/608658
@@ -574,6 +573,8 @@ await EvaluateFunctionAsync(@"html => {
574573
document.close();
575574
}", html);
576575

576+
var watcher = new LifecycleWatcher(FrameManager, this, waitUntil, timeout);
577+
577578
var watcherTask = await Task.WhenAny(
578579
watcher.TimeoutOrTerminationTask,
579580
watcher.LifecycleTask).ConfigureAwait(false);

lib/PuppeteerSharp/LifecycleWatcher.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ public LifecycleWatcher(
5656
_lifecycleTaskWrapper = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
5757
_terminationTaskWrapper = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
5858

59-
frameManager.LifecycleEvent += CheckLifecycleComplete;
59+
frameManager.LifecycleEvent += FrameManager_LifecycleEvent;
6060
frameManager.FrameNavigatedWithinDocument += NavigatedWithinDocument;
6161
frameManager.FrameDetached += OnFrameDetached;
6262
frameManager.NetworkManager.Request += OnRequest;
6363
frameManager.Client.Disconnected += OnClientDisconnected;
64+
65+
CheckLifecycleComplete();
6466
}
6567

6668
#region Properties
@@ -77,7 +79,9 @@ public Task TimeoutOrTerminationTask
7779
#region Private methods
7880

7981
private void OnClientDisconnected(object sender, EventArgs e)
80-
=> Terminate(new TargetClosedException("Navigation failed because browser has disconnected!", _frameManager.Client.CloseReason));
82+
=> Terminate(new TargetClosedException("Navigation failed because browser has disconnected!", _frameManager.Client.CloseReason));
83+
84+
void FrameManager_LifecycleEvent(object sender, FrameEventArgs e) => CheckLifecycleComplete();
8185

8286
private void OnFrameDetached(object sender, FrameEventArgs e)
8387
{
@@ -87,10 +91,10 @@ private void OnFrameDetached(object sender, FrameEventArgs e)
8791
Terminate(new PuppeteerException("Navigating frame was detached"));
8892
return;
8993
}
90-
CheckLifecycleComplete(sender, e);
94+
CheckLifecycleComplete();
9195
}
9296

93-
private void CheckLifecycleComplete(object sender, FrameEventArgs e)
97+
private void CheckLifecycleComplete()
9498
{
9599
// We expect navigation to commit.
96100
if (!CheckLifecycle(_frame, _expectedLifecycle))
@@ -131,7 +135,7 @@ private void NavigatedWithinDocument(object sender, FrameEventArgs e)
131135
return;
132136
}
133137
_hasSameDocumentNavigation = true;
134-
CheckLifecycleComplete(sender, e);
138+
CheckLifecycleComplete();
135139
}
136140

137141
private bool CheckLifecycle(Frame frame, IEnumerable<string> expectedLifecycle)
@@ -159,7 +163,7 @@ private bool CheckLifecycle(Frame frame, IEnumerable<string> expectedLifecycle)
159163

160164
public void Dispose(bool disposing)
161165
{
162-
_frameManager.LifecycleEvent -= CheckLifecycleComplete;
166+
_frameManager.LifecycleEvent -= FrameManager_LifecycleEvent;
163167
_frameManager.FrameNavigatedWithinDocument -= NavigatedWithinDocument;
164168
_frameManager.FrameDetached -= OnFrameDetached;
165169
_frameManager.NetworkManager.Request -= OnRequest;

0 commit comments

Comments
 (0)