From dbe0435669b880568c8bced2de35a59be2bfbd91 Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Sat, 10 Feb 2024 14:57:11 +0700 Subject: [PATCH] Use async/await in TouchController.getCustomLayouts() --- better-xcloud.user.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/better-xcloud.user.js b/better-xcloud.user.js index 25c4e6d..7e9eb56 100644 --- a/better-xcloud.user.js +++ b/better-xcloud.user.js @@ -3534,7 +3534,7 @@ class TouchController { }); }; - static getCustomLayouts(xboxTitleId, retries) { + static async getCustomLayouts(xboxTitleId, retries) { xboxTitleId = '' + xboxTitleId; if (xboxTitleId in TouchController.#customLayouts) { TouchController.#dispatchLayouts(TouchController.#customLayouts[xboxTitleId]); @@ -3553,27 +3553,27 @@ class TouchController { const url = `${baseUrl}/${xboxTitleId}.json`; // Get layout info - NATIVE_FETCH(url) - .then(resp => resp.json()) - .then(json => { - const layouts = {}; + try { + const resp = await NATIVE_FETCH(url); + const json = await resp.json(); - json.layouts.forEach(async file => { - const layoutUrl = `${baseUrl}/layouts/${file}.json`; - const json = await (await NATIVE_FETCH(layoutUrl)).json(); - Object.assign(layouts, json.layouts); - }); + const layouts = {}; - json.layouts = layouts; - TouchController.#customLayouts[xboxTitleId] = json; + json.layouts.forEach(async file => { + const layoutUrl = `${baseUrl}/layouts/${file}.json`; + const json = await (await NATIVE_FETCH(layoutUrl)).json(); + Object.assign(layouts, json.layouts); + }); - // Wait for BX_EXPOSED.touch_layout_manager - setTimeout(() => TouchController.#dispatchLayouts(json), 1000); - }) - .catch(() => { - // Retry - TouchController.getCustomLayouts(xboxTitleId, retries + 1); - }); + json.layouts = layouts; + TouchController.#customLayouts[xboxTitleId] = json; + + // Wait for BX_EXPOSED.touch_layout_manager + setTimeout(() => TouchController.#dispatchLayouts(json), 1000); + } catch (e) { + // Retry + TouchController.getCustomLayouts(xboxTitleId, retries + 1); + } } static loadCustomLayout(xboxTitleId, layoutId, delay) {