diff --git a/Runtime/ChromeDevtools/BrowserTab.cs b/Runtime/ChromeDevtools/BrowserTab.cs index 4f0bbe53ecbdafd81372449c6966619e8d160dae..5cc23dbb73a326cd7856691cb37a303596988fab 100644 --- a/Runtime/ChromeDevtools/BrowserTab.cs +++ b/Runtime/ChromeDevtools/BrowserTab.cs @@ -25,7 +25,22 @@ namespace bessw.Unity.WebView.ChromeDevTools 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(); public BrowserTab(PageTargetInfo pageTarget) @@ -52,7 +67,7 @@ namespace bessw.Unity.WebView.ChromeDevTools { // clear the domNodes dictionary domNodes.Clear(); - document = null; + _document = null; } public IEnumerator Update() @@ -227,16 +242,6 @@ namespace bessw.Unity.WebView.ChromeDevTools _ = 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> /// returns the cached node if it exists, otherwise fetches it from the browser /// </summary>