Skip to content

Commit

Permalink
Switch browser contexts from Dictionary to ConcurrentDictionary (#1899)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-GH-CA authored Jan 20, 2022
1 parent d86d6a9 commit 773e851
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/PuppeteerSharp/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ internal Browser(
TargetsMap = new ConcurrentDictionary<string, Target>();
ScreenshotTaskQueue = new TaskQueue();
DefaultContext = new BrowserContext(Connection, this, null);
_contexts = contextIds.ToDictionary(
_contexts = new ConcurrentDictionary<string, BrowserContext>(contextIds.ToDictionary(
contextId => contextId,
contextId => new BrowserContext(Connection, this, contextId));

contextId => new BrowserContext(Connection, this, contextId)));
Connection.Disconnected += Connection_Disconnected;
Connection.MessageReceived += Connect_MessageReceived;

Expand All @@ -72,7 +71,7 @@ internal Browser(

internal IDictionary<string, Target> TargetsMap { get; }

private readonly Dictionary<string, BrowserContext> _contexts;
private readonly ConcurrentDictionary<string, BrowserContext> _contexts;
private readonly ILogger<Browser> _logger;
private readonly Func<TargetInfo, bool> _targetFilterCallback;
private Task _closeTask;
Expand Down Expand Up @@ -212,7 +211,7 @@ public async Task<BrowserContext> CreateIncognitoBrowserContextAsync()
{
var response = await Connection.SendAsync<CreateBrowserContextResponse>("Target.createBrowserContext", null).ConfigureAwait(false);
var context = new BrowserContext(Connection, this, response.BrowserContextId);
_contexts[response.BrowserContextId] = context;
_contexts.TryAdd(response.BrowserContextId, context);
return context;
}

Expand Down Expand Up @@ -403,7 +402,7 @@ internal async Task DisposeContextAsync(string contextId)
{
BrowserContextId = contextId
}).ConfigureAwait(false);
_contexts.Remove(contextId);
_contexts.TryRemove(contextId, out var _);
}

private async void Connection_Disconnected(object sender, EventArgs e)
Expand Down

0 comments on commit 773e851

Please sign in to comment.