Skip to content

Commit a030a37

Browse files
authored
Remove obsolete binding code (#2352)
1 parent 0fd03f8 commit a030a37

File tree

4 files changed

+50
-104
lines changed

4 files changed

+50
-104
lines changed

lib/PuppeteerSharp/IsolatedWorld.cs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -263,72 +263,6 @@ await EvaluateFunctionAsync(
263263
}
264264
}
265265

266-
internal async Task<IElementHandle> WaitForSelectorInPageAsync(
267-
string queryOne,
268-
IElementHandle root,
269-
string selector,
270-
WaitForSelectorOptions options,
271-
PageBinding[] bindings = null)
272-
{
273-
try
274-
{
275-
var waitForVisible = options?.Visible ?? false;
276-
var waitForHidden = options?.Hidden ?? false;
277-
var timeout = options?.Timeout ?? _timeoutSettings.Timeout;
278-
279-
var predicate = @$"async (PuppeteerUtil, query, selector, root, visible) => {{
280-
if (!PuppeteerUtil) {{
281-
return;
282-
}}
283-
const node = (await PuppeteerUtil.createFunction(query)(
284-
root || document,
285-
selector,
286-
PuppeteerUtil,
287-
));
288-
return PuppeteerUtil.checkVisibility(node, visible);
289-
}}";
290-
291-
var args = new List<object>
292-
{
293-
new LazyArg(async context => await context.GetPuppeteerUtilAsync().ConfigureAwait(false)),
294-
queryOne,
295-
selector,
296-
root,
297-
};
298-
299-
// Puppeteer's injected code checks for visible to be undefined
300-
// As we don't support passing undefined values we need to ignore sending this value
301-
// if visible is false
302-
if (waitForVisible || waitForHidden)
303-
{
304-
args.Add(waitForVisible);
305-
}
306-
307-
var jsHandle = await WaitForFunctionAsync(
308-
predicate,
309-
new()
310-
{
311-
Bindings = bindings,
312-
Polling = waitForVisible || waitForHidden ? WaitForFunctionPollingOption.Raf : WaitForFunctionPollingOption.Mutation,
313-
Root = root,
314-
Timeout = timeout,
315-
},
316-
args.ToArray()).ConfigureAwait(false);
317-
318-
if (jsHandle is not ElementHandle elementHandle)
319-
{
320-
await jsHandle.DisposeAsync().ConfigureAwait(false);
321-
return null;
322-
}
323-
324-
return elementHandle;
325-
}
326-
catch (Exception ex)
327-
{
328-
throw new WaitTaskTimeoutException($"Waiting for selector `{selector}` failed: {ex.Message}", ex);
329-
}
330-
}
331-
332266
internal async Task ClickAsync(string selector, ClickOptions options = null)
333267
{
334268
var handle = await QuerySelectorAsync(selector).ConfigureAwait(false) ?? throw new SelectorException($"No node found for selector: {selector}", selector);
@@ -389,7 +323,6 @@ internal async Task<IJSHandle> WaitForFunctionAsync(string script, WaitForFuncti
389323
options.PollingInterval,
390324
options.Timeout ?? _timeoutSettings.Timeout,
391325
options.Root,
392-
options.Bindings,
393326
args);
394327

395328
return await waitTask
@@ -407,7 +340,6 @@ internal async Task<IJSHandle> WaitForExpressionAsync(string script, WaitForFunc
407340
options.PollingInterval,
408341
options.Timeout ?? _timeoutSettings.Timeout,
409342
null, // Root
410-
null, // PageBinding
411343
null); // args
412344

413345
return await waitTask

lib/PuppeteerSharp/QueryHandlers/QueryHandler.cs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,62 @@ internal virtual async Task<IElementHandle> WaitForAsync(
103103
element = await frame.PuppeteerWorld.AdoptHandleAsync(element).ConfigureAwait(false) as ElementHandle;
104104
}
105105

106-
var result = await frame.PuppeteerWorld.WaitForSelectorInPageAsync(
107-
QuerySelector,
108-
element,
109-
selector,
110-
options).ConfigureAwait(false);
111-
112-
if (element != null)
106+
try
113107
{
114-
await element.DisposeAsync().ConfigureAwait(false);
115-
}
108+
var waitForVisible = options?.Visible ?? false;
109+
var waitForHidden = options?.Hidden ?? false;
110+
var timeout = options?.Timeout;
111+
112+
var predicate = @$"async (PuppeteerUtil, query, selector, root, visible) => {{
113+
if (!PuppeteerUtil) {{
114+
return;
115+
}}
116+
const node = (await PuppeteerUtil.createFunction(query)(
117+
root || document,
118+
selector,
119+
PuppeteerUtil,
120+
));
121+
return PuppeteerUtil.checkVisibility(node, visible);
122+
}}";
123+
124+
var args = new List<object>
125+
{
126+
new LazyArg(async context => await context.GetPuppeteerUtilAsync().ConfigureAwait(false)),
127+
QuerySelector,
128+
selector,
129+
element,
130+
};
131+
132+
// Puppeteer's injected code checks for visible to be undefined
133+
// As we don't support passing undefined values we need to ignore sending this value
134+
// if visible is false
135+
if (waitForVisible || waitForHidden)
136+
{
137+
args.Add(waitForVisible);
138+
}
116139

117-
if (result is not ElementHandle)
118-
{
119-
if (element != null)
140+
var jsHandle = await frame.PuppeteerWorld.WaitForFunctionAsync(
141+
predicate,
142+
new()
143+
{
144+
Polling = waitForVisible || waitForHidden ? WaitForFunctionPollingOption.Raf : WaitForFunctionPollingOption.Mutation,
145+
Root = element,
146+
Timeout = timeout,
147+
},
148+
args.ToArray()).ConfigureAwait(false);
149+
150+
if (jsHandle is not ElementHandle elementHandle)
120151
{
121-
await element.DisposeAsync().ConfigureAwait(false);
152+
await jsHandle.DisposeAsync().ConfigureAwait(false);
153+
return null;
122154
}
123155

124-
return null;
156+
return await frame.MainWorld.TransferHandleAsync(elementHandle).ConfigureAwait(false) as IElementHandle;
157+
}
158+
catch (Exception ex)
159+
{
160+
throw new WaitTaskTimeoutException($"Waiting for selector `{selector}` failed: {ex.Message}", ex);
125161
}
126-
127-
return await frame.MainWorld.TransferHandleAsync(result).ConfigureAwait(false) as IElementHandle;
128162
}
129163

130164
internal virtual async IAsyncEnumerable<IElementHandle> QueryAllAsync(IElementHandle element, string selector)

lib/PuppeteerSharp/WaitForFunctionOptions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,5 @@ public class WaitForFunctionOptions
2828
/// Root element.
2929
/// </summary>
3030
internal IElementHandle Root { get; set; }
31-
32-
/// <summary>
33-
/// Page bindings.
34-
/// </summary>
35-
internal PageBinding[] Bindings { get; set; }
3631
}
3732
}

lib/PuppeteerSharp/WaitTask.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ internal sealed class WaitTask : IDisposable
1616
private readonly IElementHandle _root;
1717
private readonly CancellationTokenSource _cts = new();
1818
private readonly TaskCompletionSource<IJSHandle> _result = new(TaskCreationOptions.RunContinuationsAsynchronously);
19-
private readonly PageBinding[] _bindings;
2019

2120
private bool _isDisposed;
2221
private IJSHandle _poller;
@@ -30,7 +29,6 @@ internal WaitTask(
3029
int? pollingInterval,
3130
int timeout,
3231
IElementHandle root,
33-
PageBinding[] bindings = null,
3432
object[] args = null)
3533
{
3634
if (string.IsNullOrEmpty(fn))
@@ -49,13 +47,6 @@ internal WaitTask(
4947
_polling = _pollingInterval.HasValue ? null : polling;
5048
_args = args ?? Array.Empty<object>();
5149
_root = root;
52-
_bindings = bindings ?? Array.Empty<PageBinding>();
53-
54-
foreach (var binding in _bindings)
55-
{
56-
var bindingObj = new Binding(binding.Name, binding.Function);
57-
_isolatedWorld.Bindings.AddOrUpdate(binding.Name, bindingObj, (_, __) => bindingObj);
58-
}
5950

6051
_isolatedWorld.TaskManager.Add(this);
6152

@@ -93,12 +84,6 @@ internal async Task Rerun()
9384
{
9485
try
9586
{
96-
if (_bindings.Length > 0)
97-
{
98-
var context = await _isolatedWorld.GetExecutionContextAsync().ConfigureAwait(false);
99-
await System.Threading.Tasks.Task.WhenAll(_bindings.Select(binding => _isolatedWorld.AddBindingToContextAsync(context, binding.Name))).ConfigureAwait(false);
100-
}
101-
10287
if (_pollingInterval.HasValue)
10388
{
10489
_poller = await _isolatedWorld.EvaluateFunctionHandleAsync(

0 commit comments

Comments
 (0)