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

Script ready state change #672

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
12 changes: 6 additions & 6 deletions ext/bg/js/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Backend {
this._logErrorLevel = null;

this._messageHandlers = new Map([
['yomichanCoreReady', {async: false, contentScript: true, handler: this._onApiYomichanCoreReady.bind(this)}],
['requestBackendReadySignal', {async: false, contentScript: true, handler: this._onApiRequestBackendReadySignal.bind(this)}],
['optionsSchemaGet', {async: false, contentScript: true, handler: this._onApiOptionsSchemaGet.bind(this)}],
['optionsGet', {async: false, contentScript: true, handler: this._onApiOptionsGet.bind(this)}],
['optionsGetFull', {async: false, contentScript: true, handler: this._onApiOptionsGetFull.bind(this)}],
Expand Down Expand Up @@ -212,9 +212,9 @@ class Backend {

this._clipboardMonitor.on('change', this._onClipboardTextChange.bind(this));

this._sendMessageAllTabs('backendPrepared');
this._sendMessageAllTabs('backendReady');
const callback = () => this._checkLastError(chrome.runtime.lastError);
chrome.runtime.sendMessage({action: 'backendPrepared'}, callback);
chrome.runtime.sendMessage({action: 'backendReady'}, callback);
} catch (e) {
yomichan.logError(e);
throw e;
Expand Down Expand Up @@ -361,10 +361,10 @@ class Backend {

// Message handlers

_onApiYomichanCoreReady(_params, sender) {
_onApiRequestBackendReadySignal(_params, sender) {
// tab ID isn't set in background (e.g. browser_action)
const callback = () => this._checkLastError(chrome.runtime.lastError);
const data = {action: 'backendPrepared'};
const data = {action: 'backendReady'};
if (typeof sender.tab === 'undefined') {
chrome.runtime.sendMessage(data, callback);
return false;
Expand Down Expand Up @@ -1393,7 +1393,7 @@ class Backend {
sender.tab.id !== tabId ||
sender.frameId !== frameId ||
!isObject(message) ||
message.action !== 'yomichanCoreReady'
message.action !== 'yomichanReady'
) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion ext/bg/js/context-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function setupOptions() {

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

const manifest = chrome.runtime.getManifest();

Expand All @@ -87,4 +87,6 @@ async function setupOptions() {
setupButtonEvents('.action-open-search', 'search', chrome.runtime.getURL('/bg/search.html'));
setupButtonEvents('.action-open-options', 'options', chrome.runtime.getURL(manifest.options_ui.page));
setupButtonEvents('.action-open-help', 'help', 'https://foosoft.net/projects/yomichan/');

yomichan.ready();
})();
4 changes: 3 additions & 1 deletion ext/bg/js/search-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
(async () => {
try {
api.forwardLogsToBackend();
await yomichan.ready();
await yomichan.backendReady();

const displaySearch = new DisplaySearch();
await displaySearch.prepare();

yomichan.ready();
} catch (e) {
yomichan.logError(e);
}
Expand Down
62 changes: 34 additions & 28 deletions ext/bg/js/settings/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,45 +63,51 @@ async function setupEnvironmentInfo() {


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

setupEnvironmentInfo();
showExtensionInformation();
settingsPopulateModifierKeys();
setupEnvironmentInfo();
showExtensionInformation();
settingsPopulateModifierKeys();

const optionsFull = await api.optionsGetFull();
const optionsFull = await api.optionsGetFull();

const settingsController = new SettingsController(optionsFull.profileCurrent);
settingsController.prepare();
const settingsController = new SettingsController(optionsFull.profileCurrent);
settingsController.prepare();

const storageController = new StorageController();
storageController.prepare();
const storageController = new StorageController();
storageController.prepare();

const genericSettingController = new GenericSettingController(settingsController);
genericSettingController.prepare();
const genericSettingController = new GenericSettingController(settingsController);
genericSettingController.prepare();

const clipboardPopupsController = new ClipboardPopupsController(settingsController);
clipboardPopupsController.prepare();
const clipboardPopupsController = new ClipboardPopupsController(settingsController);
clipboardPopupsController.prepare();

const popupPreviewController = new PopupPreviewController(settingsController);
popupPreviewController.prepare();
const popupPreviewController = new PopupPreviewController(settingsController);
popupPreviewController.prepare();

const audioController = new AudioController(settingsController);
audioController.prepare();
const audioController = new AudioController(settingsController);
audioController.prepare();

const profileController = new ProfileController(settingsController);
profileController.prepare();
const profileController = new ProfileController(settingsController);
profileController.prepare();

const dictionaryController = new DictionaryController(settingsController, storageController);
dictionaryController.prepare();
const dictionaryController = new DictionaryController(settingsController, storageController);
dictionaryController.prepare();

const ankiController = new AnkiController(settingsController);
ankiController.prepare();
const ankiController = new AnkiController(settingsController);
ankiController.prepare();

const ankiTemplatesController = new AnkiTemplatesController(settingsController, ankiController);
ankiTemplatesController.prepare();
const ankiTemplatesController = new AnkiTemplatesController(settingsController, ankiController);
ankiTemplatesController.prepare();

const settingsBackup = new SettingsBackup(settingsController);
settingsBackup.prepare();
const settingsBackup = new SettingsBackup(settingsController);
settingsBackup.prepare();

yomichan.ready();
} catch (e) {
yomichan.logError(e);
}
})();
4 changes: 3 additions & 1 deletion ext/fg/js/content-script-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(async () => {
try {
api.forwardLogsToBackend();
await yomichan.ready();
await yomichan.backendReady();

const {frameId} = await api.frameInformationGet();
if (typeof frameId !== 'number') {
Expand All @@ -40,6 +40,8 @@
{}
);
await frontend.prepare();

yomichan.ready();
} catch (e) {
yomichan.logError(e);
}
Expand Down
4 changes: 3 additions & 1 deletion ext/fg/js/float-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
(async () => {
try {
api.forwardLogsToBackend();
await yomichan.ready();
await yomichan.backendReady();

const display = new DisplayFloat();
await display.prepare();

yomichan.ready();
} catch (e) {
yomichan.logError(e);
}
Expand Down
30 changes: 17 additions & 13 deletions ext/mixed/js/yomichan.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ const yomichan = (() => {
this._isReady = false;

const {promise, resolve} = deferPromise();
this._isBackendPreparedPromise = promise;
this._isBackendPreparedPromiseResolve = resolve;
this._isBackendReadyPromise = promise;
this._isBackendReadyPromiseResolve = resolve;

this._messageHandlers = new Map([
['isReady', {async: false, handler: this._onMessageIsReady.bind(this)}],
['backendPrepared', {async: false, handler: this._onMessageBackendPrepared.bind(this)}],
['getUrl', {async: false, handler: this._onMessageGetUrl.bind(this)}],
['optionsUpdated', {async: false, handler: this._onMessageOptionsUpdated.bind(this)}],
['zoomChanged', {async: false, handler: this._onMessageZoomChanged.bind(this)}]
['isReady', {async: false, handler: this._onMessageIsReady.bind(this)}],
['backendReady', {async: false, handler: this._onMessageBackendReady.bind(this)}],
['getUrl', {async: false, handler: this._onMessageGetUrl.bind(this)}],
['optionsUpdated', {async: false, handler: this._onMessageOptionsUpdated.bind(this)}],
['zoomChanged', {async: false, handler: this._onMessageZoomChanged.bind(this)}]
]);
}

Expand All @@ -73,10 +73,14 @@ const yomichan = (() => {
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
}

backendReady() {
this.sendMessage({action: 'requestBackendReadySignal'});
return this._isBackendReadyPromise;
}

ready() {
this._isReady = true;
this.sendMessage({action: 'yomichanCoreReady'});
return this._isBackendPreparedPromise;
this.sendMessage({action: 'yomichanReady'});
}

generateId(length) {
Expand Down Expand Up @@ -275,10 +279,10 @@ const yomichan = (() => {
return this._isReady;
}

_onMessageBackendPrepared() {
if (this._isBackendPreparedPromiseResolve === null) { return; }
this._isBackendPreparedPromiseResolve();
this._isBackendPreparedPromiseResolve = null;
_onMessageBackendReady() {
if (this._isBackendReadyPromiseResolve === null) { return; }
this._isBackendReadyPromiseResolve();
this._isBackendReadyPromiseResolve = null;
}

_onMessageGetUrl() {
Expand Down