Skip to content

Commit 3c97c3f

Browse files
kblokMeir017
authored andcommitted
Remove callback items (#641)
* Remove callback items * 0 warnings policy
1 parent c9e4daf commit 3c97c3f

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

lib/PuppeteerSharp.Tests/ConnectionTests/ConnectionTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public async Task ShouldThrowNiceErrors()
2323
Assert.Contains("ThisCommand.DoesNotExist", exception.Message);
2424
}
2525

26+
[Fact]
27+
public async Task ShouldCleanCallbackList()
28+
{
29+
await Browser.GetVersionAsync();
30+
await Browser.GetVersionAsync();
31+
Assert.False(Browser.Connection.HasPendingCallbacks());
32+
33+
await Page.SetJavaScriptEnabledAsync(false);
34+
await Page.SetJavaScriptEnabledAsync(true);
35+
Assert.False(Page.Client.HasPendingCallbacks());
36+
}
37+
2638
private async Task TheSourceOfTheProblems()
2739
{
2840
await Page.Client.SendAsync("ThisCommand.DoesNotExist");

lib/PuppeteerSharp/CDPSession.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ internal async Task<dynamic> SendAsync(string method, bool rawContent, dynamic a
155155
public Task DetachAsync()
156156
=> Connection.SendAsync("Target.detachFromTarget", new { sessionId = SessionId });
157157

158+
internal bool HasPendingCallbacks() => _callbacks.Count != 0;
159+
158160
#endregion
159161

160162
#region Private Methods

lib/PuppeteerSharp/Connection.cs

100755100644
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Net.WebSockets;
@@ -130,6 +131,8 @@ internal async Task<CDPSession> CreateSessionAsync(TargetInfo targetInfo)
130131
_sessions.Add(sessionId, session);
131132
return session;
132133
}
134+
135+
internal bool HasPendingCallbacks() => _callbacks.Count != 0;
133136
#endregion
134137

135138
private void OnClose()
@@ -237,7 +240,7 @@ private void ProcessResponse(string response)
237240

238241
//If we get the object we are waiting for we return if
239242
//if not we add this to the list, sooner or later some one will come for it
240-
if (_callbacks.TryGetValue(id, out var callback))
243+
if (_callbacks.TryGetValue(id, out var callback) && _callbacks.Remove(id))
241244
{
242245
if (objAsJObject["error"] != null)
243246
{

lib/PuppeteerSharp/PuppeteerSharp.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
<PropertyGroup>
3030
<LangVersion>7.1</LangVersion>
3131
</PropertyGroup>
32+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
33+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
34+
</PropertyGroup>
35+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
36+
<DebugType></DebugType>
37+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
38+
</PropertyGroup>
3239
<ItemGroup>
3340
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.2" />
3441
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />

lib/PuppeteerSharp/WaitTaskTimeoutException.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,47 @@ public class WaitTaskTimeoutException : PuppeteerException
2020
/// <value>The element.</value>
2121
public string ElementType { get; }
2222

23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="PuppeteerSharp.WaitTaskTimeoutException"/> class.
25+
/// </summary>
2326
public WaitTaskTimeoutException()
2427
{
2528
}
2629

30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="PuppeteerSharp.WaitTaskTimeoutException"/> class.
32+
/// </summary>
33+
/// <param name="message">Message.</param>
2734
public WaitTaskTimeoutException(string message) : base(message)
2835
{
2936
}
3037

38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="PuppeteerSharp.WaitTaskTimeoutException"/> class.
40+
/// </summary>
41+
/// <param name="timeout">Timeout.</param>
42+
/// <param name="elementType">Element type.</param>
3143
public WaitTaskTimeoutException(int timeout, string elementType) :
3244
base($"waiting for {elementType} failed: timeout {timeout}ms exceeded")
3345
{
3446
Timeout = timeout;
3547
ElementType = elementType;
3648
}
3749

50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="PuppeteerSharp.WaitTaskTimeoutException"/> class.
52+
/// </summary>
53+
/// <param name="message">Message.</param>
54+
/// <param name="innerException">Inner exception.</param>
3855
public WaitTaskTimeoutException(string message, Exception innerException) : base(message, innerException)
3956
{
4057
}
4158

59+
/// <summary>
60+
/// Initializes a new instance of the <see cref="PuppeteerSharp.WaitTaskTimeoutException"/> class.
61+
/// </summary>
62+
/// <param name="info">Info.</param>
63+
/// <param name="context">Context.</param>
4264
protected WaitTaskTimeoutException(SerializationInfo info, StreamingContext context) : base(info, context)
4365
{
4466
}

0 commit comments

Comments
 (0)