Skip to content
Snippets Groups Projects
Verified Commit 8cd56ed2 authored by Björn Eßwein's avatar Björn Eßwein
Browse files

fixed race condition in tab.getDocument by replacing it with an awaitable property

parent 9ea4b1c5
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,22 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -25,7 +25,22 @@ namespace bessw.Unity.WebView.ChromeDevTools
public Dictionary<int, DomNodeWrapper> domNodes = new Dictionary<int, DomNodeWrapper>(); public Dictionary<int, DomNodeWrapper> domNodes = new Dictionary<int, DomNodeWrapper>();
private DomNodeWrapper document; private Task<DomNodeWrapper> _document;
public Task<DomNodeWrapper> Document
{
get
{
if (_document is null)
{
_document = DomNodeWrapper.getDocumentAsync(this);
// unset _document on error, so it will be re-fetched on next access
_document.ContinueWith(_ => _document = null, TaskContinuationOptions.NotOnRanToCompletion);
}
return _document;
}
}
private Dictionary<string, Action<string>> runtimeBindings = new(); private Dictionary<string, Action<string>> runtimeBindings = new();
public BrowserTab(PageTargetInfo pageTarget) public BrowserTab(PageTargetInfo pageTarget)
...@@ -52,7 +67,7 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -52,7 +67,7 @@ namespace bessw.Unity.WebView.ChromeDevTools
{ {
// clear the domNodes dictionary // clear the domNodes dictionary
domNodes.Clear(); domNodes.Clear();
document = null; _document = null;
} }
public IEnumerator Update() public IEnumerator Update()
...@@ -227,16 +242,6 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -227,16 +242,6 @@ namespace bessw.Unity.WebView.ChromeDevTools
_ = devtools.SendCommandAsync(new cancelDragging()); _ = devtools.SendCommandAsync(new cancelDragging());
} }
/// <summary>
/// returns the cached document DomNodeWrapper or fetches it from the browser
/// </summary>
/// <returns></returns>
public async Task<DomNodeWrapper> GetDocument()
{
if (document is not null) return document;
else return await DomNodeWrapper.getDocumentAsync(this);
}
/// <summary> /// <summary>
/// returns the cached node if it exists, otherwise fetches it from the browser /// returns the cached node if it exists, otherwise fetches it from the browser
/// </summary> /// </summary>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment