From 3c4c82dcfc66a1b24a3df3d4b15283235c72cf66 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 24 May 2020 14:00:32 -0400 Subject: Ensure single popup factory (#554) * Add createPopupFactory * Ensure only a single PopupFactory is generated --- ext/fg/js/content-script-main.js | 42 +++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/ext/fg/js/content-script-main.js b/ext/fg/js/content-script-main.js index b057ae3d..cebda2d7 100644 --- a/ext/fg/js/content-script-main.js +++ b/ext/fg/js/content-script-main.js @@ -24,6 +24,19 @@ * api */ +async function createPopupFactory() { + const {frameId} = await api.frameInformationGet(); + if (typeof frameId !== 'number') { + const error = new Error('Failed to get frameId'); + yomichan.logError(error); + throw error; + } + + const popupFactory = new PopupFactory(frameId); + await popupFactory.prepare(); + return popupFactory; +} + async function createIframePopupProxy(frameOffsetForwarder, setDisabled) { const rootPopupInformationPromise = yomichan.getTemporaryListenerResult( chrome.runtime.onMessage, @@ -44,20 +57,8 @@ async function createIframePopupProxy(frameOffsetForwarder, setDisabled) { return popup; } -async function getOrCreatePopup(depth) { - const {frameId} = await api.frameInformationGet(); - if (typeof frameId !== 'number') { - const error = new Error('Failed to get frameId'); - yomichan.logError(error); - throw error; - } - - const popupFactory = new PopupFactory(frameId); - await popupFactory.prepare(); - - const popup = popupFactory.getOrCreatePopup(null, null, depth); - - return popup; +async function getOrCreatePopup(depth, popupFactory) { + return popupFactory.getOrCreatePopup(null, null, depth); } async function createPopupProxy(depth, id, parentFrameId) { @@ -85,6 +86,7 @@ async function createPopupProxy(depth, id, parentFrameId) { let frontend = null; let frontendPreparePromise = null; let frameOffsetForwarder = null; + let popupFactoryPromise = null; let iframePopupsInRootFrameAvailable = true; @@ -124,8 +126,16 @@ async function createPopupProxy(depth, id, parentFrameId) { popup = popups.proxy || await createPopupProxy(depth, id, parentFrameId); popups.proxy = popup; } else { - popup = popups.normal || await getOrCreatePopup(depth); - popups.normal = popup; + popup = popups.normal; + if (!popup) { + if (popupFactoryPromise === null) { + popupFactoryPromise = createPopupFactory(); + } + const popupFactory = await popupFactoryPromise; + const popupNormal = await getOrCreatePopup(depth, popupFactory); + popups.normal = popupNormal; + popup = popupNormal; + } } if (frontend === null) { -- cgit v1.2.3