aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-14 15:19:31 -0500
committerGitHub <noreply@github.com>2021-02-14 15:19:31 -0500
commitefe8140f103179f50b610f182148b9427af99010 (patch)
tree94afdefddf9985d2ec11cdcd9a1e2638e2005b3d
parent94201ed44ac1835637e77a6a7f583aca70bbf2d3 (diff)
Api prepare refactor (#1391)
* Refactor API preparation * Run yomichan.prepare manually in the entry point function
-rw-r--r--ext/js/app/content-script-main.js4
-rw-r--r--ext/js/background/background-main.js2
-rw-r--r--ext/js/comm/api.js39
-rw-r--r--ext/js/display/popup-main.js4
-rw-r--r--ext/js/display/search-main.js4
-rw-r--r--ext/js/pages/action-popup-main.js4
-rw-r--r--ext/js/pages/info-main.js2
-rw-r--r--ext/js/pages/permissions-main.js2
-rw-r--r--ext/js/pages/welcome-main.js2
-rw-r--r--ext/js/settings/main.js4
-rw-r--r--ext/js/settings/pitch-accents-preview-main.js4
-rw-r--r--ext/js/settings/popup-preview-frame-main.js3
-rw-r--r--ext/js/settings/settings-main.js2
-rw-r--r--ext/js/yomichan.js12
14 files changed, 43 insertions, 45 deletions
diff --git a/ext/js/app/content-script-main.js b/ext/js/app/content-script-main.js
index 5dee4c56..ee05034e 100644
--- a/ext/js/app/content-script-main.js
+++ b/ext/js/app/content-script-main.js
@@ -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') {
diff --git a/ext/js/background/background-main.js b/ext/js/background/background-main.js
index 01e57d0f..11df1706 100644
--- a/ext/js/background/background-main.js
+++ b/ext/js/background/background-main.js
@@ -20,6 +20,8 @@
*/
(() => {
+ yomichan.prepare(true);
+
const backend = new Backend();
backend.prepare();
})();
diff --git a/ext/js/comm/api.js b/ext/js/comm/api.js
index d37b091a..26397d1f 100644
--- a/ext/js/comm/api.js
+++ b/ext/js/comm/api.js
@@ -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();
})();
diff --git a/ext/js/display/popup-main.js b/ext/js/display/popup-main.js
index 7c048b62..f1228aa6 100644
--- a/ext/js/display/popup-main.js
+++ b/ext/js/display/popup-main.js
@@ -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();
diff --git a/ext/js/display/search-main.js b/ext/js/display/search-main.js
index c7ec595a..d3e8af0b 100644
--- a/ext/js/display/search-main.js
+++ b/ext/js/display/search-main.js
@@ -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();
diff --git a/ext/js/pages/action-popup-main.js b/ext/js/pages/action-popup-main.js
index 5cc56745..99a76bdf 100644
--- a/ext/js/pages/action-popup-main.js
+++ b/ext/js/pages/action-popup-main.js
@@ -221,8 +221,8 @@ class DisplayController {
}
(async () => {
- api.forwardLogsToBackend();
- await yomichan.backendReady();
+ api.prepare();
+ await yomichan.prepare();
api.logIndicatorClear();
diff --git a/ext/js/pages/info-main.js b/ext/js/pages/info-main.js
index 6cf82595..7d34d47a 100644
--- a/ext/js/pages/info-main.js
+++ b/ext/js/pages/info-main.js
@@ -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;
diff --git a/ext/js/pages/permissions-main.js b/ext/js/pages/permissions-main.js
index 5b17a5dd..7c0427b9 100644
--- a/ext/js/pages/permissions-main.js
+++ b/ext/js/pages/permissions-main.js
@@ -69,7 +69,7 @@ function setupPermissionsToggles() {
node.textContent = chrome.runtime.getURL('/');
}
- api.forwardLogsToBackend();
+ api.prepare();
await yomichan.prepare();
setupEnvironmentInfo();
diff --git a/ext/js/pages/welcome-main.js b/ext/js/pages/welcome-main.js
index 57b265dc..626cbd3a 100644
--- a/ext/js/pages/welcome-main.js
+++ b/ext/js/pages/welcome-main.js
@@ -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();
diff --git a/ext/js/settings/main.js b/ext/js/settings/main.js
index 9786d196..0707ea3c 100644
--- a/ext/js/settings/main.js
+++ b/ext/js/settings/main.js
@@ -51,8 +51,8 @@ async function setupEnvironmentInfo() {
(async () => {
try {
- api.forwardLogsToBackend();
- await yomichan.backendReady();
+ api.prepare();
+ await yomichan.prepare();
setupEnvironmentInfo();
showExtensionInformation();
diff --git a/ext/js/settings/pitch-accents-preview-main.js b/ext/js/settings/pitch-accents-preview-main.js
index f292170a..7bc995a2 100644
--- a/ext/js/settings/pitch-accents-preview-main.js
+++ b/ext/js/settings/pitch-accents-preview-main.js
@@ -17,10 +17,14 @@
/* global
* DisplayGenerator
+ * api
*/
(async () => {
try {
+ api.prepare();
+ await yomichan.prepare();
+
const displayGenerator = new DisplayGenerator({
japaneseUtil: null,
mediaLoader: null
diff --git a/ext/js/settings/popup-preview-frame-main.js b/ext/js/settings/popup-preview-frame-main.js
index dcbc0d96..f61b26dc 100644
--- a/ext/js/settings/popup-preview-frame-main.js
+++ b/ext/js/settings/popup-preview-frame-main.js
@@ -24,7 +24,8 @@
(async () => {
try {
- api.forwardLogsToBackend();
+ api.prepare();
+ await yomichan.prepare();
const {tabId, frameId} = await api.frameInformationGet();
diff --git a/ext/js/settings/settings-main.js b/ext/js/settings/settings-main.js
index 24248110..a7e6b7b0 100644
--- a/ext/js/settings/settings-main.js
+++ b/ext/js/settings/settings-main.js
@@ -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();
diff --git a/ext/js/yomichan.js b/ext/js/yomichan.js
index 61301e30..7d101a4c 100644
--- a/ext/js/yomichan.js
+++ b/ext/js/yomichan.js
@@ -71,13 +71,13 @@ const yomichan = (() => {
return this._isExtensionUnloaded;
}
- prepare() {
+ async prepare(isBackground=false) {
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
- }
- backendReady() {
- this.sendMessage({action: 'requestBackendReadySignal'});
- return this._isBackendReadyPromise;
+ if (!isBackground) {
+ this.sendMessage({action: 'requestBackendReadySignal'});
+ await this._isBackendReadyPromise;
+ }
}
ready() {
@@ -302,5 +302,3 @@ const yomichan = (() => {
return new Yomichan();
})();
-
-yomichan.prepare();