Skip to content

Commit

Permalink
Introduce Target initialization status (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Sep 20, 2023
1 parent 79dc944 commit 4218fd4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docfx_project/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ await page.SetViewportAsync(new ViewPortOptions

```cs
using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
await browserFetcher.DownloadAsync();
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
Expand Down
6 changes: 3 additions & 3 deletions lib/PuppeteerSharp/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ private async void OnDetachedFromTargetAsync(object sender, TargetChangedArgs e)
{
try
{
e.Target.InitializedTaskWrapper.TrySetResult(false);
e.Target.InitializedTaskWrapper.TrySetResult(InitializationStatus.Aborted);
e.Target.CloseTaskWrapper.TrySetResult(true);

if (await e.Target.InitializedTask.ConfigureAwait(false))
if ((await e.Target.InitializedTask.ConfigureAwait(false)) == InitializationStatus.Success)
{
var args = new TargetChangedArgs { Target = e.Target };
TargetDestroyed?.Invoke(this, args);
Expand All @@ -374,7 +374,7 @@ private async void OnAttachedToTargetAsync(object sender, TargetChangedArgs e)
{
try
{
if (await e.Target.InitializedTask.ConfigureAwait(false))
if ((await e.Target.InitializedTask.ConfigureAwait(false)) == InitializationStatus.Success)
{
var args = new TargetChangedArgs { Target = e.Target };
TargetCreated?.Invoke(this, args);
Expand Down
8 changes: 8 additions & 0 deletions lib/PuppeteerSharp/InitializationStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PuppeteerSharp
{
internal enum InitializationStatus
{
Aborted,
Success,
}
}
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/PageTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void Initialize()
async initializedTask =>
{
var success = initializedTask.Result;
if (!success)
if (success != InitializationStatus.Success)
{
return;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ protected override void CheckIfInitialized()
IsInitialized = !string.IsNullOrEmpty(TargetInfo.Url);
if (IsInitialized)
{
InitializedTaskWrapper.TrySetResult(true);
InitializedTaskWrapper.TrySetResult(InitializationStatus.Success);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ internal Target(

internal BrowserContext BrowserContext { get; }

internal Task<bool> InitializedTask => InitializedTaskWrapper.Task;
internal Task<InitializationStatus> InitializedTask => InitializedTaskWrapper.Task;

internal TaskCompletionSource<bool> InitializedTaskWrapper { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
internal TaskCompletionSource<InitializationStatus> InitializedTaskWrapper { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);

internal Task CloseTask => CloseTaskWrapper.Task;

Expand Down Expand Up @@ -88,7 +88,7 @@ internal void TargetInfoChanged(TargetInfo targetInfo)
protected virtual void Initialize()
{
IsInitialized = true;
InitializedTaskWrapper.TrySetResult(true);
InitializedTaskWrapper.TrySetResult(InitializationStatus.Success);
}

/// <summary>
Expand All @@ -97,7 +97,7 @@ protected virtual void Initialize()
protected virtual void CheckIfInitialized()
{
IsInitialized = true;
InitializedTaskWrapper.TrySetResult(true);
InitializedTaskWrapper.TrySetResult(InitializationStatus.Success);
}
}
}

0 comments on commit 4218fd4

Please sign in to comment.