From bed997ec9816de75529df98505be4f2d31cb20f5 Mon Sep 17 00:00:00 2001 From: philippthiele Date: Thu, 27 Apr 2023 15:21:23 +0200 Subject: [PATCH] fix: queue on the node event loop to mitigate "waitAsync is already running bug" (#455) fixes #452 * fix for waitAsync is already running bug * only reset bWaitStarted to false when whole queue is processed --------- Co-authored-by: Philipp Thiele --- client-side-js/injectUI5.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/client-side-js/injectUI5.js b/client-side-js/injectUI5.js index 1c9e6d5a..99f7fa10 100644 --- a/client-side-js/injectUI5.js +++ b/client-side-js/injectUI5.js @@ -26,7 +26,9 @@ async function clientSide_injectUI5(config, waitForUI5Timeout, browserInstance) }, objectMap: { // GUID: {} - } + }, + bWaitStarted: false, + asyncControlRetrievalQueue: [] } /** @@ -57,13 +59,27 @@ async function clientSide_injectUI5(config, waitForUI5Timeout, browserInstance) oOptions = oOptions || {} _autoWaiterAsync.extendConfig(oOptions) - _autoWaiterAsync.waitAsync(function (sError) { - if (sError) { - errorCallback(new Error(sError)) - } else { - callback() - } - }) + const startWaiting = function () { + window.wdi5.bWaitStarted = true; + _autoWaiterAsync.waitAsync(function (sError) { + const nextWaitAsync = window.wdi5.asyncControlRetrievalQueue.shift(); + if (nextWaitAsync) { + setTimeout(nextWaitAsync); //use setTimeout to postpone execution to the next event cycle, so that bWaitStarted in the UI5 _autoWaiterAsync is also set to false first + } else { + window.wdi5.bWaitStarted = false; + } + if (sError) { + errorCallback(new Error(sError)) + } else { + callback() + } + }) + } + if (!window.wdi5.bWaitStarted) { + startWaiting(); + } else { + window.wdi5.asyncControlRetrievalQueue.push(startWaiting); + } } window.wdi5.Log.info("[browser wdi5] window._autoWaiterAsync used in waitForUI5 function") })