1- using System . Diagnostics ;
1+ using System ;
2+ using System . Diagnostics ;
23using System . Threading . Tasks ;
4+ using PuppeteerSharp . Helpers ;
35
46namespace PuppeteerSharp
57{
@@ -10,25 +12,30 @@ namespace PuppeteerSharp
1012 public class Target
1113 {
1214 #region Private members
13- private Browser _browser ;
15+ private readonly Browser _browser ;
1416 private TargetInfo _targetInfo ;
17+ private string _targetId ;
18+ private Func < Task < CDPSession > > _sessionFactory ;
1519 private Task < Page > _pageTask ;
1620 #endregion
1721
1822 internal bool IsInitialized ;
1923
20- internal Target ( Browser browser , TargetInfo targetInfo )
24+ internal Target ( TargetInfo targetInfo , Func < Task < CDPSession > > sessionFactory , Browser browser )
2125 {
22- _browser = browser ;
2326 _targetInfo = targetInfo ;
27+ _targetId = targetInfo . TargetId ;
28+ _sessionFactory = sessionFactory ;
29+ _browser = browser ;
30+ _pageTask = null ;
2431
2532 InitilizedTaskWrapper = new TaskCompletionSource < bool > ( ) ;
2633 CloseTaskWrapper = new TaskCompletionSource < bool > ( ) ;
27- IsInitialized = _targetInfo . Type != "page" || _targetInfo . Url != string . Empty ;
34+ IsInitialized = _targetInfo . Type != TargetType . Page || _targetInfo . Url != string . Empty ;
2835
29- if ( IsInitialized )
30- {
31- InitilizedTaskWrapper . SetResult ( true ) ;
36+ if ( IsInitialized )
37+ {
38+ InitilizedTaskWrapper . SetResult ( true ) ;
3239 }
3340 }
3441
@@ -42,7 +49,8 @@ internal Target(Browser browser, TargetInfo targetInfo)
4249 /// Gets the type. It will be <see cref="TargetInfo.Type"/> if it's "page" or "service_worker". Otherwise it will be "other"
4350 /// </summary>
4451 /// <value>The type.</value>
45- public string Type => _targetInfo . Type == "page" || _targetInfo . Type == "service_worker" || _targetInfo . Type == "browser" ? _targetInfo . Type : "other" ;
52+ public TargetType Type => _targetInfo . Type ;
53+
4654 /// <summary>
4755 /// Gets the target identifier.
4856 /// </summary>
@@ -60,22 +68,26 @@ internal Target(Browser browser, TargetInfo targetInfo)
6068 /// <returns>a task that returns a new <see cref="Page"/></returns>
6169 public async Task < Page > PageAsync ( )
6270 {
63- if ( _targetInfo . Type == "page" && _pageTask == null )
64- {
65- _pageTask = await _browser . Connection . CreateSessionAsync ( _targetInfo . TargetId )
66- . ContinueWith ( clientTask
67- => Page . CreateAsync ( clientTask . Result , this , _browser . IgnoreHTTPSErrors , _browser . AppMode , _browser . ScreenshotTaskQueue ) ) ;
71+ if ( _targetInfo . Type == TargetType . Page && _pageTask == null )
72+ {
73+ _pageTask = CreatePageAsync ( ) ;
6874 }
6975
7076 return await ( _pageTask ?? Task . FromResult < Page > ( null ) ) ;
7177 }
7278
79+ private async Task < Page > CreatePageAsync ( )
80+ {
81+ var session = await _sessionFactory ( ) ;
82+ return await Page . CreateAsync ( session , this , _browser . IgnoreHTTPSErrors , _browser . AppMode , _browser . ScreenshotTaskQueue ) ;
83+ }
84+
7385 internal void TargetInfoChanged ( TargetInfo targetInfo )
7486 {
7587 var previousUrl = _targetInfo . Url ;
7688 _targetInfo = targetInfo ;
7789
78- if ( ! IsInitialized && ( _targetInfo . Type != "page" || _targetInfo . Url != string . Empty ) )
90+ if ( ! IsInitialized && ( _targetInfo . Type != TargetType . Page || _targetInfo . Url != string . Empty ) )
7991 {
8092 IsInitialized = true ;
8193 InitilizedTaskWrapper . SetResult ( true ) ;
0 commit comments