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
*/
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) {