Api prepare refactor (#1391)
* Refactor API preparation * Run yomichan.prepare manually in the entry point function
This commit is contained in:
parent
94201ed44a
commit
efe8140f10
@ -24,8 +24,8 @@
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
api.forwardLogsToBackend();
|
||||
await yomichan.backendReady();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
const {tabId, frameId} = await api.frameInformationGet();
|
||||
if (typeof frameId !== 'number') {
|
||||
|
@ -20,6 +20,8 @@
|
||||
*/
|
||||
|
||||
(() => {
|
||||
yomichan.prepare(true);
|
||||
|
||||
const backend = new Backend();
|
||||
backend.prepare();
|
||||
})();
|
||||
|
@ -22,8 +22,8 @@
|
||||
const api = (() => {
|
||||
class API {
|
||||
constructor() {
|
||||
this._forwardLogsToBackendEnabled = false;
|
||||
this._crossFrame = new CrossFrameAPI();
|
||||
this._prepared = false;
|
||||
this._crossFrame = null;
|
||||
}
|
||||
|
||||
get crossFrame() {
|
||||
@ -31,20 +31,11 @@ const api = (() => {
|
||||
}
|
||||
|
||||
prepare() {
|
||||
if (this._prepared) { return; }
|
||||
this._crossFrame = new CrossFrameAPI();
|
||||
this._crossFrame.prepare();
|
||||
}
|
||||
|
||||
forwardLogsToBackend() {
|
||||
if (this._forwardLogsToBackendEnabled) { return; }
|
||||
this._forwardLogsToBackendEnabled = true;
|
||||
|
||||
yomichan.on('log', async ({error, level, context}) => {
|
||||
try {
|
||||
await this.log(serializeError(error), level, context);
|
||||
} catch (e) {
|
||||
// NOP
|
||||
}
|
||||
});
|
||||
yomichan.on('log', this._onLog.bind(this));
|
||||
this._prepared = true;
|
||||
}
|
||||
|
||||
// Invoke functions
|
||||
@ -161,10 +152,6 @@ const api = (() => {
|
||||
return this._invoke('getMedia', {targets});
|
||||
}
|
||||
|
||||
log(error, level, context) {
|
||||
return this._invoke('log', {error, level, context});
|
||||
}
|
||||
|
||||
logIndicatorClear() {
|
||||
return this._invoke('logIndicatorClear');
|
||||
}
|
||||
@ -331,10 +318,16 @@ const api = (() => {
|
||||
_checkLastError() {
|
||||
// NOP
|
||||
}
|
||||
|
||||
async _onLog({error, level, context}) {
|
||||
try {
|
||||
error = serializeError(error);
|
||||
await this._invoke('log', {error, level, context});
|
||||
} catch (e) {
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-shadow
|
||||
const api = new API();
|
||||
api.prepare();
|
||||
return api;
|
||||
return new API();
|
||||
})();
|
||||
|
@ -29,8 +29,8 @@
|
||||
const documentFocusController = new DocumentFocusController();
|
||||
documentFocusController.prepare();
|
||||
|
||||
api.forwardLogsToBackend();
|
||||
await yomichan.backendReady();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
const {tabId, frameId} = await api.frameInformationGet();
|
||||
|
||||
|
@ -30,8 +30,8 @@
|
||||
const documentFocusController = new DocumentFocusController();
|
||||
documentFocusController.prepare();
|
||||
|
||||
api.forwardLogsToBackend();
|
||||
await yomichan.backendReady();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
const {tabId, frameId} = await api.frameInformationGet();
|
||||
|
||||
|
@ -221,8 +221,8 @@ class DisplayController {
|
||||
}
|
||||
|
||||
(async () => {
|
||||
api.forwardLogsToBackend();
|
||||
await yomichan.backendReady();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
api.logIndicatorClear();
|
||||
|
||||
|
@ -54,7 +54,7 @@ function getOperatingSystemDisplayName(os) {
|
||||
const manifest = chrome.runtime.getManifest();
|
||||
const language = chrome.i18n.getUILanguage();
|
||||
|
||||
api.forwardLogsToBackend();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
const {userAgent} = navigator;
|
||||
|
@ -69,7 +69,7 @@ function setupPermissionsToggles() {
|
||||
node.textContent = chrome.runtime.getURL('/');
|
||||
}
|
||||
|
||||
api.forwardLogsToBackend();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
setupEnvironmentInfo();
|
||||
|
@ -49,7 +49,7 @@ async function setupGenericSettingsController(genericSettingController) {
|
||||
const statusFooter = new StatusFooter(document.querySelector('.status-footer-container'));
|
||||
statusFooter.prepare();
|
||||
|
||||
api.forwardLogsToBackend();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
setupEnvironmentInfo();
|
||||
|
@ -51,8 +51,8 @@ async function setupEnvironmentInfo() {
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
api.forwardLogsToBackend();
|
||||
await yomichan.backendReady();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
setupEnvironmentInfo();
|
||||
showExtensionInformation();
|
||||
|
@ -17,10 +17,14 @@
|
||||
|
||||
/* global
|
||||
* DisplayGenerator
|
||||
* api
|
||||
*/
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
const displayGenerator = new DisplayGenerator({
|
||||
japaneseUtil: null,
|
||||
mediaLoader: null
|
||||
|
@ -24,7 +24,8 @@
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
api.forwardLogsToBackend();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
const {tabId, frameId} = await api.frameInformationGet();
|
||||
|
||||
|
@ -66,7 +66,7 @@ async function setupGenericSettingsController(genericSettingController) {
|
||||
const statusFooter = new StatusFooter(document.querySelector('.status-footer-container'));
|
||||
statusFooter.prepare();
|
||||
|
||||
api.forwardLogsToBackend();
|
||||
api.prepare();
|
||||
await yomichan.prepare();
|
||||
|
||||
setupEnvironmentInfo();
|
||||
|
@ -71,13 +71,13 @@ const yomichan = (() => {
|
||||
return this._isExtensionUnloaded;
|
||||
}
|
||||
|
||||
prepare() {
|
||||
async prepare(isBackground=false) {
|
||||
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
|
||||
}
|
||||
|
||||
backendReady() {
|
||||
if (!isBackground) {
|
||||
this.sendMessage({action: 'requestBackendReadySignal'});
|
||||
return this._isBackendReadyPromise;
|
||||
await this._isBackendReadyPromise;
|
||||
}
|
||||
}
|
||||
|
||||
ready() {
|
||||
@ -302,5 +302,3 @@ const yomichan = (() => {
|
||||
|
||||
return new Yomichan();
|
||||
})();
|
||||
|
||||
yomichan.prepare();
|
||||
|
Loading…
Reference in New Issue
Block a user