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

Switch browser contexts from Dictionary to ConcurrentDictionary #1899

Merged
merged 1 commit into from
Jan 20, 2022
Merged
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
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