Api prepare refactor (#1391)

* Refactor API preparation

* Run yomichan.prepare manually in the entry point function
This commit is contained in:
toasted-nutbread 2021-02-14 15:19:31 -05:00 committed by GitHub
parent 94201ed44a
commit efe8140f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 43 additions and 45 deletions

View File

@ -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') {

View File

@ -20,6 +20,8 @@
*/
(() => {
yomichan.prepare(true);
const backend = new Backend();
backend.prepare();
})();

View File

@ -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();
})();

View File

@ -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();

View File

@ -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();

View File

@ -221,8 +221,8 @@ class DisplayController {
}
(async () => {
api.forwardLogsToBackend();
await yomichan.backendReady();
api.prepare();
await yomichan.prepare();
api.logIndicatorClear();

View File

@ -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;

View File

@ -69,7 +69,7 @@ function setupPermissionsToggles() {
node.textContent = chrome.runtime.getURL('/');
}
api.forwardLogsToBackend();
api.prepare();
await yomichan.prepare();
setupEnvironmentInfo();

View File

@ -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();

View File

@ -51,8 +51,8 @@ async function setupEnvironmentInfo() {
(async () => {
try {
api.forwardLogsToBackend();
await yomichan.backendReady();
api.prepare();
await yomichan.prepare();
setupEnvironmentInfo();
showExtensionInformation();

View File

@ -17,10 +17,14 @@
/* global
* DisplayGenerator
* api
*/
(async () => {
try {
api.prepare();
await yomichan.prepare();
const displayGenerator = new DisplayGenerator({
japaneseUtil: null,
mediaLoader: null

View File

@ -24,7 +24,8 @@
(async () => {
try {
api.forwardLogsToBackend();
api.prepare();
await yomichan.prepare();
const {tabId, frameId} = await api.frameInformationGet();

View File

@ -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();

View File

@ -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();