Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move target updates to the target manager #2316

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions lib/PuppeteerSharp/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,14 @@ protected virtual void Dispose(bool disposing) => _ = CloseAsync()
private void TargetManager_TargetDiscovered(object sender, TargetChangedArgs e)
=> TargetDiscovered?.Invoke(this, e);

private void TargetManager_TargetChanged(object sender, TargetChangedArgs e)
private void OnTargetChanged(object sender, TargetChangedArgs e)
{
var previousURL = e.Target.Url;
var wasInitialized = e.Target.IsInitialized;
e.Target.TargetInfoChanged(e.TargetInfo);
if (wasInitialized && previousURL != e.Target.Url)
{
var args = new TargetChangedArgs { Target = e.Target };
TargetChanged?.Invoke(this, args);
e.Target.BrowserContext.OnTargetChanged(this, args);
}
var args = new TargetChangedArgs { Target = e.Target };
TargetChanged?.Invoke(this, args);
e.Target.BrowserContext.OnTargetChanged(this, args);
}

private async void TargetManager_TargetGone(object sender, TargetChangedArgs e)
private async void OnDetachedFromTargetAsync(object sender, TargetChangedArgs e)
{
try
{
Expand All @@ -376,7 +370,7 @@ private async void TargetManager_TargetGone(object sender, TargetChangedArgs e)
}
}

private async void TargetManager_TargetAvailable(object sender, TargetChangedArgs e)
private async void OnAttachedToTargetAsync(object sender, TargetChangedArgs e)
{
try
{
Expand Down Expand Up @@ -454,19 +448,19 @@ private async void Connection_Disconnected(object sender, EventArgs e)
private Task AttachAsync()
{
Connection.Disconnected += Connection_Disconnected;
TargetManager.TargetAvailable += TargetManager_TargetAvailable;
TargetManager.TargetGone += TargetManager_TargetGone;
TargetManager.TargetChanged += TargetManager_TargetChanged;
TargetManager.TargetAvailable += OnAttachedToTargetAsync;
TargetManager.TargetGone += OnDetachedFromTargetAsync;
TargetManager.TargetChanged += OnTargetChanged;
TargetManager.TargetDiscovered += TargetManager_TargetDiscovered;
return TargetManager.InitializeAsync();
}

private void Detach()
{
Connection.Disconnected -= Connection_Disconnected;
TargetManager.TargetAvailable -= TargetManager_TargetAvailable;
TargetManager.TargetGone -= TargetManager_TargetGone;
TargetManager.TargetChanged -= TargetManager_TargetChanged;
TargetManager.TargetAvailable -= OnAttachedToTargetAsync;
TargetManager.TargetGone -= OnDetachedFromTargetAsync;
TargetManager.TargetChanged -= OnTargetChanged;
TargetManager.TargetDiscovered -= TargetManager_TargetDiscovered;
}

Expand Down
15 changes: 13 additions & 2 deletions lib/PuppeteerSharp/ChromeTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,18 @@ private void OnTargetInfoChanged(TargetCreatedResponse e)
return;
}

TargetChanged?.Invoke(this, new TargetChangedArgs { Target = target, TargetInfo = e.TargetInfo });
var previousURL = target.Url;
var wasInitialized = target.IsInitialized;
target.TargetInfoChanged(e.TargetInfo);

if (wasInitialized && previousURL != target.Url)
{
TargetChanged?.Invoke(this, new TargetChangedArgs
{
Target = target,
TargetInfo = e.TargetInfo,
});
}
}

private async Task OnAttachedToTargetAsync(object sender, TargetAttachedToTargetResponse e)
Expand Down Expand Up @@ -339,7 +350,7 @@ await Task.WhenAll(
}
catch (Exception ex)
{
_logger.LogError("Failed to call setautoAttach and runIfWaitingForDebugger", ex);
_logger.LogError("Failed to call setAutoAttach and runIfWaitingForDebugger", ex);
}
}

Expand Down
Loading