Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Api prepare refactor #1391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/js/app/content-script-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
2 changes: 2 additions & 0 deletions ext/js/background/background-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

(() => {
yomichan.prepare(true);

const backend = new Backend();
backend.prepare();
})();
39 changes: 16 additions & 23 deletions ext/js/comm/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,20 @@
const api = (() => {
class API {
constructor() {
this._forwardLogsToBackendEnabled = false;
this._crossFrame = new CrossFrameAPI();
this._prepared = false;
this._crossFrame = null;
}

get crossFrame() {
return this._crossFrame;
}

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
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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();
})();
4 changes: 2 additions & 2 deletions ext/js/display/popup-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions ext/js/display/search-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions ext/js/pages/action-popup-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ class DisplayController {
}

(async () => {
api.forwardLogsToBackend();
await yomichan.backendReady();
api.prepare();
await yomichan.prepare();

api.logIndicatorClear();

Expand Down
2 changes: 1 addition & 1 deletion ext/js/pages/info-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ext/js/pages/permissions-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function setupPermissionsToggles() {
node.textContent = chrome.runtime.getURL('/');
}

api.forwardLogsToBackend();
api.prepare();
await yomichan.prepare();

setupEnvironmentInfo();
Expand Down
2 changes: 1 addition & 1 deletion ext/js/pages/welcome-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions ext/js/settings/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async function setupEnvironmentInfo() {

(async () => {
try {
api.forwardLogsToBackend();
await yomichan.backendReady();
api.prepare();
await yomichan.prepare();

setupEnvironmentInfo();
showExtensionInformation();
Expand Down
4 changes: 4 additions & 0 deletions ext/js/settings/pitch-accents-preview-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

/* global
* DisplayGenerator
* api
*/

(async () => {
try {
api.prepare();
await yomichan.prepare();

const displayGenerator = new DisplayGenerator({
japaneseUtil: null,
mediaLoader: null
Expand Down
3 changes: 2 additions & 1 deletion ext/js/settings/popup-preview-frame-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

(async () => {
try {
api.forwardLogsToBackend();
api.prepare();
await yomichan.prepare();

const {tabId, frameId} = await api.frameInformationGet();

Expand Down
2 changes: 1 addition & 1 deletion ext/js/settings/settings-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 5 additions & 7 deletions ext/js/yomichan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -302,5 +302,3 @@ const yomichan = (() => {

return new Yomichan();
})();

yomichan.prepare();