Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using OpenQA.Selenium.BiDi.Browser;
Expand All @@ -25,11 +27,15 @@ namespace OpenQA.Selenium.BiDi.Json.Converters;

internal class BrowserUserContextConverter(IBiDi bidi) : JsonConverter<UserContext>
{
private static readonly ConditionalWeakTable<IBiDi, ConcurrentDictionary<string, UserContext>> s_cache = [];
Comment thread
nvborisenko marked this conversation as resolved.
Outdated

public override UserContext? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var id = reader.GetString();

return new UserContext(id!) { BiDi = bidi };
var sessionCache = s_cache.GetValue(bidi, _ => new ConcurrentDictionary<string, UserContext>());

return sessionCache.GetOrAdd(id!, key => new UserContext(key) { BiDi = bidi });
Comment thread
nvborisenko marked this conversation as resolved.
Outdated
}

public override void Write(Utf8JsonWriter writer, UserContext value, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@
// under the License.
// </copyright>

using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.BiDi.Json.Converters;

internal class BrowsingContextConverter(IBiDi bidi) : JsonConverter<BrowsingContext.BrowsingContext>
{
private static readonly ConditionalWeakTable<IBiDi, ConcurrentDictionary<string, BrowsingContext.BrowsingContext>> s_cache = [];

Comment thread
nvborisenko marked this conversation as resolved.
Outdated
Comment thread
nvborisenko marked this conversation as resolved.
Outdated
private readonly IBiDi _bidi = bidi;

public override BrowsingContext.BrowsingContext? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var id = reader.GetString();

return new BrowsingContext.BrowsingContext(id!) { BiDi = _bidi };
var sessionCache = s_cache.GetValue(_bidi, _ => new ConcurrentDictionary<string, BrowsingContext.BrowsingContext>());

return sessionCache.GetOrAdd(id!, key => new BrowsingContext.BrowsingContext(_bidi, key));
Comment thread
nvborisenko marked this conversation as resolved.
Outdated
}

public override void Write(Utf8JsonWriter writer, BrowsingContext.BrowsingContext value, JsonSerializerOptions options)
Expand Down
Loading