Skip to content

Commit

Permalink
Rename Closed to Disconnected (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Dec 14, 2018
1 parent 8057843 commit 0c42e64
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Browser.cs
Expand Up @@ -65,7 +65,7 @@ public class Browser : IDisposable
_contexts = contextIds.ToDictionary(keySelector: contextId => contextId,
elementSelector: contextId => new BrowserContext(Connection, this, contextId));

Connection.Closed += (object sender, EventArgs e) => Disconnected?.Invoke(this, new EventArgs());
Connection.Disconnected += (object sender, EventArgs e) => Disconnected?.Invoke(this, new EventArgs());
Connection.MessageReceived += Connect_MessageReceived;

_chromiumProcess = chromiumProcess;
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/CDPSession.cs 100755 → 100644
Expand Up @@ -87,7 +87,7 @@ internal CDPSession(IConnection connection, TargetType targetType, string sessio
/// <summary>
/// Occurs when the connection is closed.
/// </summary>
public event EventHandler Closed;
public event EventHandler Disconnected;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="CDPSession"/> is closed.
/// </summary>
Expand Down Expand Up @@ -295,7 +295,7 @@ internal void Close(string closeReason)
));
}
_callbacks.Clear();
Closed?.Invoke(this, EventArgs.Empty);
Disconnected?.Invoke(this, EventArgs.Empty);
Connection = null;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/PuppeteerSharp/Connection.cs
Expand Up @@ -61,7 +61,7 @@ internal Connection(string url, int delay, IConnectionTransport transport, ILogg
/// <summary>
/// Occurs when the connection is closed.
/// </summary>
public event EventHandler Closed;
public event EventHandler Disconnected;
/// <summary>
/// Occurs when a message from chromium is received.
/// </summary>
Expand Down Expand Up @@ -149,7 +149,7 @@ internal void Close(string closeReason)
CloseReason = closeReason;

Transport.StopReading();
Closed?.Invoke(this, new EventArgs());
Disconnected?.Invoke(this, new EventArgs());

foreach (var session in _sessions.Values.ToArray())
{
Expand Down Expand Up @@ -288,7 +288,7 @@ internal static async Task<Connection> Create(string url, IConnectionOptions con

/// <summary>
/// Releases all resource used by the <see cref="Connection"/> object.
/// It will raise the <see cref="Closed"/> event and dispose <see cref="Transport"/>.
/// It will raise the <see cref="Disconnected"/> event and dispose <see cref="Transport"/>.
/// </summary>
/// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="Connection"/>. The
/// <see cref="Dispose"/> method leaves the <see cref="Connection"/> in an unusable state.
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/IConnection.cs
Expand Up @@ -40,9 +40,9 @@ internal interface IConnection
/// </summary>
IConnection Connection { get; }
/// <summary>
/// Occurs when the connection is closed.
/// Occurs when the connection is Disconnected.
/// </summary>
event EventHandler Closed;
event EventHandler Disconnected;
/// <summary>
/// Close the connection.
/// </summary>
Expand Down
40 changes: 19 additions & 21 deletions lib/PuppeteerSharp/LifecycleWatcher.cs
Expand Up @@ -18,12 +18,11 @@ internal class LifecycleWatcher : IDisposable
[WaitUntilNavigation.Networkidle2] = "networkAlmostIdle"
};

private static readonly WaitUntilNavigation[] _defaultWaitUntil = new[] { WaitUntilNavigation.Load };
private static readonly WaitUntilNavigation[] _defaultWaitUntil = { WaitUntilNavigation.Load };

private readonly FrameManager _frameManager;
private readonly Frame _frame;
private readonly NavigationOptions _options;
private readonly IConnection _connection;
private readonly IEnumerable<string> _expectedLifecycle;
private readonly int _timeout;
private readonly string _initialLoaderId;
Expand All @@ -33,7 +32,7 @@ internal class LifecycleWatcher : IDisposable
private TaskCompletionSource<bool> _sameDocumentNavigationTaskWrapper;
private TaskCompletionSource<bool> _lifecycleTaskWrapper;
private TaskCompletionSource<bool> _terminationTaskWrapper;
private Task _timeoutTask;
private readonly Task _timeoutTask;

public LifecycleWatcher(
FrameManager frameManager,
Expand Down Expand Up @@ -66,8 +65,7 @@ internal class LifecycleWatcher : IDisposable
frameManager.FrameNavigatedWithinDocument += NavigatedWithinDocument;
frameManager.FrameDetached += OnFrameDetached;
frameManager.NetworkManager.Request += OnRequest;
_connection = Connection.FromSession(frameManager.Client);
_connection.Closed += OnConnectionClosed;
frameManager.Client.Disconnected += OnClientDisconnected;

_sameDocumentNavigationTaskWrapper = new TaskCompletionSource<bool>();
_newDocumentNavigationTaskWrapper = new TaskCompletionSource<bool>();
Expand All @@ -84,23 +82,23 @@ internal class LifecycleWatcher : IDisposable
public Task<Task> TimeoutOrTerminationTask => Task.WhenAny(_timeoutTask, _terminationTaskWrapper.Task);
public Task LifecycleTask => _lifecycleTaskWrapper.Task;

#endregion

#region Private methods
#endregion

private void OnConnectionClosed(object sender, EventArgs e)
=> Terminate(new TargetClosedException("Navigation failed because browser has disconnected!", _connection.CloseReason));
#region Private methods

private void OnFrameDetached(object sender, FrameEventArgs e)
{
var frame = e.Frame;
if (_frame == frame)
{
Terminate(new PuppeteerException("Navigating frame was detached"));
return;
}
CheckLifecycleComplete(sender, e);
}
private void OnClientDisconnected(object sender, EventArgs e)
=> Terminate(new TargetClosedException("Navigation failed because browser has disconnected!", _frameManager.Client.CloseReason));

private void OnFrameDetached(object sender, FrameEventArgs e)
{
var frame = e.Frame;
if (_frame == frame)
{
Terminate(new PuppeteerException("Navigating frame was detached"));
return;
}
CheckLifecycleComplete(sender, e);
}

private void CheckLifecycleComplete(object sender, FrameEventArgs e)
{
Expand Down Expand Up @@ -175,7 +173,7 @@ public void Dispose(bool disposing)
_frameManager.FrameNavigatedWithinDocument -= NavigatedWithinDocument;
_frameManager.FrameDetached -= OnFrameDetached;
_frameManager.NetworkManager.Request -= OnRequest;
_connection.Closed -= OnConnectionClosed;
_frameManager.Client.Disconnected -= OnClientDisconnected;
}

#endregion
Expand Down

0 comments on commit 0c42e64

Please sign in to comment.