Ensure single popup factory (#554)

* Add createPopupFactory

* Ensure only a single PopupFactory is generated
This commit is contained in:
toasted-nutbread 2020-05-24 14:00:32 -04:00 committed by GitHub
parent 13f57cccba
commit 3c4c82dcfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,6 +24,19 @@
* api * 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) { async function createIframePopupProxy(frameOffsetForwarder, setDisabled) {
const rootPopupInformationPromise = yomichan.getTemporaryListenerResult( const rootPopupInformationPromise = yomichan.getTemporaryListenerResult(
chrome.runtime.onMessage, chrome.runtime.onMessage,
@ -44,20 +57,8 @@ async function createIframePopupProxy(frameOffsetForwarder, setDisabled) {
return popup; return popup;
} }
async function getOrCreatePopup(depth) { async function getOrCreatePopup(depth, popupFactory) {
const {frameId} = await api.frameInformationGet(); return popupFactory.getOrCreatePopup(null, null, depth);
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 createPopupProxy(depth, id, parentFrameId) { async function createPopupProxy(depth, id, parentFrameId) {
@ -85,6 +86,7 @@ async function createPopupProxy(depth, id, parentFrameId) {
let frontend = null; let frontend = null;
let frontendPreparePromise = null; let frontendPreparePromise = null;
let frameOffsetForwarder = null; let frameOffsetForwarder = null;
let popupFactoryPromise = null;
let iframePopupsInRootFrameAvailable = true; let iframePopupsInRootFrameAvailable = true;
@ -124,8 +126,16 @@ async function createPopupProxy(depth, id, parentFrameId) {
popup = popups.proxy || await createPopupProxy(depth, id, parentFrameId); popup = popups.proxy || await createPopupProxy(depth, id, parentFrameId);
popups.proxy = popup; popups.proxy = popup;
} else { } else {
popup = popups.normal || await getOrCreatePopup(depth); popup = popups.normal;
popups.normal = popup; 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) { if (frontend === null) {