Skip to content

Commit 720e602

Browse files
[dotnet] Replace Lazy<T> with ??= in BrowsingContext (#16664)
Co-authored-by: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com>
1 parent dcb967e commit 720e602

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
using System;
2121
using System.Text.Json.Serialization;
22+
using System.Threading;
2223
using System.Threading.Tasks;
2324

2425
namespace OpenQA.Selenium.BiDi.BrowsingContext;
@@ -29,39 +30,33 @@ internal BrowsingContext(BiDi bidi, string id)
2930
{
3031
BiDi = bidi;
3132
Id = id;
32-
33-
_logModule = new Lazy<BrowsingContextLogModule>(() => new BrowsingContextLogModule(this, BiDi.Log));
34-
_networkModule = new Lazy<BrowsingContextNetworkModule>(() => new BrowsingContextNetworkModule(this, BiDi.Network));
35-
_scriptModule = new Lazy<BrowsingContextScriptModule>(() => new BrowsingContextScriptModule(this, BiDi.Script));
36-
_storageModule = new Lazy<BrowsingContextStorageModule>(() => new BrowsingContextStorageModule(this, BiDi.Storage));
37-
_inputModule = new Lazy<BrowsingContextInputModule>(() => new BrowsingContextInputModule(this, BiDi.InputModule));
3833
}
3934

40-
private readonly Lazy<BrowsingContextLogModule> _logModule;
41-
private readonly Lazy<BrowsingContextNetworkModule> _networkModule;
42-
private readonly Lazy<BrowsingContextScriptModule> _scriptModule;
43-
private readonly Lazy<BrowsingContextStorageModule> _storageModule;
44-
private readonly Lazy<BrowsingContextInputModule> _inputModule;
35+
private BrowsingContextLogModule? _logModule;
36+
private BrowsingContextNetworkModule? _networkModule;
37+
private BrowsingContextScriptModule? _scriptModule;
38+
private BrowsingContextStorageModule? _storageModule;
39+
private BrowsingContextInputModule? _inputModule;
4540

4641
internal string Id { get; }
4742

4843
[JsonIgnore]
4944
public BiDi BiDi { get; }
5045

5146
[JsonIgnore]
52-
public BrowsingContextLogModule Log => _logModule.Value;
47+
public BrowsingContextLogModule Log => _logModule ?? Interlocked.CompareExchange(ref _logModule, new BrowsingContextLogModule(this, BiDi.Log), null) ?? _logModule;
5348

5449
[JsonIgnore]
55-
public BrowsingContextNetworkModule Network => _networkModule.Value;
50+
public BrowsingContextNetworkModule Network => _networkModule ?? Interlocked.CompareExchange(ref _networkModule, new BrowsingContextNetworkModule(this, BiDi.Network), null) ?? _networkModule;
5651

5752
[JsonIgnore]
58-
public BrowsingContextScriptModule Script => _scriptModule.Value;
53+
public BrowsingContextScriptModule Script => _scriptModule ?? Interlocked.CompareExchange(ref _scriptModule, new BrowsingContextScriptModule(this, BiDi.Script), null) ?? _scriptModule;
5954

6055
[JsonIgnore]
61-
public BrowsingContextStorageModule Storage => _storageModule.Value;
56+
public BrowsingContextStorageModule Storage => _storageModule ?? Interlocked.CompareExchange(ref _storageModule, new BrowsingContextStorageModule(this, BiDi.Storage), null) ?? _storageModule;
6257

6358
[JsonIgnore]
64-
public BrowsingContextInputModule Input => _inputModule.Value;
59+
public BrowsingContextInputModule Input => _inputModule ?? Interlocked.CompareExchange(ref _inputModule, new BrowsingContextInputModule(this, BiDi.InputModule), null) ?? _inputModule;
6560

6661
public Task<NavigateResult> NavigateAsync(string url, NavigateOptions? options = null)
6762
{

0 commit comments

Comments
 (0)