Skip to content

Commit

Permalink
fix workers
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Nov 21, 2023
1 parent 9419592 commit 5e436c2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
3 changes: 0 additions & 3 deletions lib/PuppeteerSharp/ChromeTargetManager.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Helpers.Json;
using PuppeteerSharp.Messaging;
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ internal void Close(string closeReason)

internal CDPSession GetSession(string sessionId) => _sessions.GetValueOrDefault(sessionId);

internal Task<CDPSession> GetSessionAsync(string sessionId) => _sessions.GetItemAsync(sessionId);

/// <summary>
/// Releases all resource used by the <see cref="Connection"/> object.
/// It will raise the <see cref="Disconnected"/> event and dispose <see cref="Transport"/>.
Expand All @@ -248,6 +246,8 @@ protected virtual void Dispose(bool disposing)
_callbackQueue.Dispose();
}

private Task<CDPSession> GetSessionAsync(string sessionId) => _sessions.GetItemAsync(sessionId);

private async void Transport_MessageReceived(object sender, MessageReceivedEventArgs e)
{
try
Expand Down
11 changes: 4 additions & 7 deletions lib/PuppeteerSharp/WebWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class WebWorker : IEnvironment
private readonly ILogger _logger;
private readonly Func<ConsoleType, IJSHandle[], StackTrace, Task> _consoleAPICalled;
private readonly Action<EvaluateExceptionResponseDetails> _exceptionThrown;
private readonly TaskCompletionSource<ExecutionContext> _executionContextCallback = new(TaskCreationOptions.RunContinuationsAsynchronously);

internal WebWorker(
CDPSession client,
Expand Down Expand Up @@ -82,8 +81,6 @@ internal WebWorker(

internal IsolatedWorld World { get; }

internal Task<ExecutionContext> ExecutionContextTask => _executionContextCallback.Task;

/// <summary>
/// Executes a script in browser context.
/// </summary>
Expand All @@ -95,7 +92,7 @@ internal WebWorker(
/// <seealso cref="ExecutionContext.EvaluateExpressionAsync(string)"/>
/// <returns>Task which resolves to script return value.</returns>
public async Task<T> EvaluateExpressionAsync<T>(string script)
=> await (await ExecutionContextTask.ConfigureAwait(false)).EvaluateExpressionAsync<T>(script).ConfigureAwait(false);
=> await World.EvaluateExpressionAsync<T>(script).ConfigureAwait(false);

/// <summary>
/// Executes a function in browser context.
Expand All @@ -108,7 +105,7 @@ public async Task<T> EvaluateExpressionAsync<T>(string script)
/// </remarks>
/// <returns>Task which resolves to script return value.</returns>
public async Task<JToken> EvaluateFunctionAsync(string script, params object[] args)
=> await (await ExecutionContextTask.ConfigureAwait(false)).EvaluateFunctionAsync(script, args).ConfigureAwait(false);
=> await World.EvaluateFunctionAsync(script, args).ConfigureAwait(false);

/// <summary>
/// Executes a function in the context.
Expand All @@ -122,7 +119,7 @@ public async Task<JToken> EvaluateFunctionAsync(string script, params object[] a
/// </remarks>
/// <returns>Task which resolves to script return value.</returns>
public async Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)
=> await (await ExecutionContextTask.ConfigureAwait(false)).EvaluateFunctionAsync<T>(script, args).ConfigureAwait(false);
=> await World.EvaluateFunctionAsync<T>(script, args).ConfigureAwait(false);

/// <summary>
/// Executes a script in browser context.
Expand All @@ -134,7 +131,7 @@ public async Task<T> EvaluateFunctionAsync<T>(string script, params object[] arg
/// <returns>Task which resolves to script return value.</returns>
/// <seealso cref="ExecutionContext.EvaluateExpressionHandleAsync(string)"/>
public async Task<IJSHandle> EvaluateExpressionHandleAsync(string script)
=> await (await ExecutionContextTask.ConfigureAwait(false)).EvaluateExpressionHandleAsync(script).ConfigureAwait(false);
=> await World.EvaluateExpressionHandleAsync(script).ConfigureAwait(false);

internal async void OnMessageReceived(object sender, MessageEventArgs e)
{
Expand Down

0 comments on commit 5e436c2

Please sign in to comment.