Skip to content

Commit dfac7b3

Browse files
authored
Add WorkerTarget and OtherTarget (#2315)
1 parent d6a2106 commit dfac7b3

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

lib/PuppeteerSharp/Browser.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,25 +479,37 @@ private Target CreateTarget(TargetInfo targetInfo, CDPSession session)
479479
context = _defaultContext;
480480
}
481481

482+
Func<bool, Task<CDPSession>> createSession = (bool isAutoAttachEmulated) => Connection.CreateSessionAsync(targetInfo, isAutoAttachEmulated);
483+
482484
if (IsPageTargetFunc(targetInfo))
483485
{
484486
return new PageTarget(
485487
targetInfo,
486488
session,
487489
context,
488490
TargetManager,
489-
(bool isAutoAttachEmulated) => Connection.CreateSessionAsync(targetInfo, isAutoAttachEmulated),
491+
createSession,
490492
IgnoreHTTPSErrors,
491493
DefaultViewport,
492494
ScreenshotTaskQueue);
493495
}
494496

495-
return new Target(
497+
if (targetInfo.Type == TargetType.ServiceWorker || targetInfo.Type == TargetType.SharedWorker)
498+
{
499+
return new WorkerTarget(
500+
targetInfo,
501+
session,
502+
context,
503+
TargetManager,
504+
createSession);
505+
}
506+
507+
return new OtherTarget(
496508
targetInfo,
497509
session,
498510
context,
499511
TargetManager,
500-
(bool isAutoAttachEmulated) => Connection.CreateSessionAsync(targetInfo, isAutoAttachEmulated));
512+
createSession);
501513
}
502514
}
503515
}

lib/PuppeteerSharp/OtherTarget.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace PuppeteerSharp
5+
{
6+
internal class OtherTarget : Target
7+
{
8+
public OtherTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> createSession) : base(targetInfo, session, context, targetManager, createSession)
9+
{
10+
}
11+
}
12+
}

lib/PuppeteerSharp/PageTarget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override async Task<IPage> PageAsync()
2626
{
2727
if (PageTask == null)
2828
{
29-
var session = Session ?? await SessionFactory(true).ConfigureAwait(false);
29+
var session = Session ?? await SessionFactory(false).ConfigureAwait(false);
3030

3131
PageTask = Page.CreateAsync(
3232
session,

lib/PuppeteerSharp/Target.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ namespace PuppeteerSharp
1212
[DebuggerDisplay("Target {Type} - {Url}")]
1313
public class Target : ITarget
1414
{
15-
private Task<Worker> _workerTask;
16-
1715
internal Target(
1816
TargetInfo targetInfo,
1917
CDPSession session,
@@ -73,20 +71,7 @@ internal Target(
7371
public virtual Task<IPage> PageAsync() => Task.FromResult<IPage>(null);
7472

7573
/// <inheritdoc/>
76-
public Task<Worker> WorkerAsync()
77-
{
78-
if (TargetInfo.Type != TargetType.ServiceWorker && TargetInfo.Type != TargetType.SharedWorker)
79-
{
80-
return Task.FromResult<Worker>(null);
81-
}
82-
83-
if (_workerTask == null)
84-
{
85-
_workerTask = WorkerInternalAsync();
86-
}
87-
88-
return _workerTask;
89-
}
74+
public virtual Task<Worker> WorkerAsync() => Task.FromResult<Worker>(null);
9075

9176
/// <inheritdoc/>
9277
public async Task<ICDPSession> CreateCDPSessionAsync() => await SessionFactory(false).ConfigureAwait(false);
@@ -114,15 +99,5 @@ protected virtual void CheckIfInitialized()
11499
IsInitialized = true;
115100
InitializedTaskWrapper.TrySetResult(true);
116101
}
117-
118-
private async Task<Worker> WorkerInternalAsync()
119-
{
120-
var client = Session ?? await SessionFactory(false).ConfigureAwait(false);
121-
return new Worker(
122-
client,
123-
TargetInfo.Url,
124-
(_, _, _) => Task.CompletedTask,
125-
_ => { });
126-
}
127102
}
128103
}

lib/PuppeteerSharp/WorkerTarget.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace PuppeteerSharp
5+
{
6+
internal class WorkerTarget : Target
7+
{
8+
private Task<Worker> _workerTask;
9+
10+
public WorkerTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> sessionFactory) : base(targetInfo, session, context, targetManager, sessionFactory)
11+
{
12+
}
13+
14+
/// <inheritdoc/>
15+
public override Task<Worker> WorkerAsync()
16+
{
17+
_workerTask ??= WorkerInternalAsync();
18+
return _workerTask;
19+
}
20+
21+
private async Task<Worker> WorkerInternalAsync()
22+
{
23+
var client = Session ?? await SessionFactory(false).ConfigureAwait(false);
24+
return new Worker(
25+
client,
26+
TargetInfo.Url,
27+
(_, _, _) => Task.CompletedTask,
28+
_ => { });
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)