Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Nov 16, 2023
1 parent fd0beb0 commit 07345b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
37 changes: 18 additions & 19 deletions lib/PuppeteerSharp/ElementHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,33 +284,32 @@ public Task<IElementHandle[]> QuerySelectorAllAsync(string selector)
});

/// <inheritdoc/>
public Task<IJSHandle> QuerySelectorAllHandleAsync(string selector)
=> BindIsolatedHandleAsync(async handle =>
public async Task<IJSHandle> QuerySelectorAllHandleAsync(string selector)
{
if (string.IsNullOrEmpty(selector))
{
if (string.IsNullOrEmpty(selector))
{
throw new ArgumentNullException(nameof(selector));
}
throw new ArgumentNullException(nameof(selector));
}

var handles = await handle.QuerySelectorAllAsync(selector).ConfigureAwait(false);
var handles = await QuerySelectorAllAsync(selector).ConfigureAwait(false);

var elements = await handle.EvaluateFunctionHandleAsync(
@"(_, ...elements) => {
var elements = await EvaluateFunctionHandleAsync(
@"(_, ...elements) => {
return elements;
}",
handles).ConfigureAwait(false) as JSHandle;
handles).ConfigureAwait(false) as JSHandle;

elements.DisposeAction = async () =>
elements.DisposeAction = async () =>
{
// We can't use Task.WhenAll with ValueTask :(
foreach (var item in handles)
{
// We can't use Task.WhenAll with ValueTask :(
foreach (var item in handles)
{
await item.DisposeAsync().ConfigureAwait(false);
}
};
await item.DisposeAsync().ConfigureAwait(false);
}
};

return (IJSHandle)elements;
});
return elements;
}

/// <inheritdoc/>
public Task<IElementHandle[]> XPathAsync(string expression)
Expand Down
6 changes: 4 additions & 2 deletions lib/PuppeteerSharp/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,14 @@ internal void UpdateClient(CDPSession client)
MainRealm = new IsolatedWorld(
this,
null,
FrameManager.TimeoutSettings);
FrameManager.TimeoutSettings,
true);

IsolatedRealm = new IsolatedWorld(
this,
null,
FrameManager.TimeoutSettings);
FrameManager.TimeoutSettings,
false);
}

private Task<ElementHandle> GetDocumentAsync()
Expand Down
9 changes: 8 additions & 1 deletion lib/PuppeteerSharp/IsolatedWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,23 @@ internal class IsolatedWorld : Realm, IDisposable, IAsyncDisposable
public IsolatedWorld(
Frame frame,
WebWorker worker,
TimeoutSettings timeoutSettings) : base(timeoutSettings)
TimeoutSettings timeoutSettings,
bool isMainWorld) : base(timeoutSettings)
{
Frame = frame;
Worker = worker;
IsMainWorld = isMainWorld;
_logger = Client.Connection.LoggerFactory.CreateLogger<IsolatedWorld>();

_detached = false;
Client.MessageReceived += Client_MessageReceived;
}

/// <summary>
/// This property is not upstream. It's helpful for debugging.
/// </summary>
internal bool IsMainWorld { get; }

internal Frame Frame { get; }

internal CDPSession Client => Frame?.Client ?? Worker?.Client;
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/WebWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal WebWorker(
{
_logger = client.Connection.LoggerFactory.CreateLogger<WebWorker>();
Client = client;
World = new IsolatedWorld(null, this, new TimeoutSettings());
World = new IsolatedWorld(null, this, new TimeoutSettings(), true);
Url = url;
_consoleAPICalled = consoleAPICalled;
_exceptionThrown = exceptionThrown;
Expand Down

0 comments on commit 07345b3

Please sign in to comment.