Merge pull request #437 from toasted-nutbread/backend-api-handler-changes

Backend api handler changes
This commit is contained in:
toasted-nutbread 2020-04-11 11:32:52 -04:00 committed by GitHub
commit a864cf094f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 68 deletions

View File

@ -76,33 +76,33 @@ class Backend {
this.messageToken = yomichan.generateId(16);
this._messageHandlers = new Map([
['yomichanCoreReady', this._onApiYomichanCoreReady.bind(this)],
['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)],
['optionsGet', this._onApiOptionsGet.bind(this)],
['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
['optionsSet', this._onApiOptionsSet.bind(this)],
['optionsSave', this._onApiOptionsSave.bind(this)],
['kanjiFind', this._onApiKanjiFind.bind(this)],
['termsFind', this._onApiTermsFind.bind(this)],
['textParse', this._onApiTextParse.bind(this)],
['textParseMecab', this._onApiTextParseMecab.bind(this)],
['definitionAdd', this._onApiDefinitionAdd.bind(this)],
['definitionsAddable', this._onApiDefinitionsAddable.bind(this)],
['noteView', this._onApiNoteView.bind(this)],
['templateRender', this._onApiTemplateRender.bind(this)],
['commandExec', this._onApiCommandExec.bind(this)],
['audioGetUri', this._onApiAudioGetUri.bind(this)],
['screenshotGet', this._onApiScreenshotGet.bind(this)],
['forward', this._onApiForward.bind(this)],
['frameInformationGet', this._onApiFrameInformationGet.bind(this)],
['injectStylesheet', this._onApiInjectStylesheet.bind(this)],
['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)],
['clipboardGet', this._onApiClipboardGet.bind(this)],
['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)],
['getQueryParserTemplatesHtml', this._onApiGetQueryParserTemplatesHtml.bind(this)],
['getZoom', this._onApiGetZoom.bind(this)],
['getMessageToken', this._onApiGetMessageToken.bind(this)],
['getDefaultAnkiFieldTemplates', this._onApiGetDefaultAnkiFieldTemplates.bind(this)]
['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: false}],
['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: false}],
['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: false}],
['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: false}],
['optionsSet', {handler: this._onApiOptionsSet.bind(this), async: true}],
['optionsSave', {handler: this._onApiOptionsSave.bind(this), async: true}],
['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}],
['termsFind', {handler: this._onApiTermsFind.bind(this), async: true}],
['textParse', {handler: this._onApiTextParse.bind(this), async: true}],
['textParseMecab', {handler: this._onApiTextParseMecab.bind(this), async: true}],
['definitionAdd', {handler: this._onApiDefinitionAdd.bind(this), async: true}],
['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}],
['noteView', {handler: this._onApiNoteView.bind(this), async: true}],
['templateRender', {handler: this._onApiTemplateRender.bind(this), async: true}],
['commandExec', {handler: this._onApiCommandExec.bind(this), async: false}],
['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}],
['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}],
['broadcastTab', {handler: this._onApiBroadcastTab.bind(this), async: false}],
['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}],
['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}],
['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}],
['clipboardGet', {handler: this._onApiClipboardGet.bind(this), async: true}],
['getDisplayTemplatesHtml', {handler: this._onApiGetDisplayTemplatesHtml.bind(this), async: true}],
['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}],
['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}],
['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: false}],
['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: false}]
]);
this._commandHandlers = new Map([
@ -166,16 +166,23 @@ class Backend {
}
onMessage({action, params}, sender, callback) {
const handler = this._messageHandlers.get(action);
if (typeof handler !== 'function') { return false; }
const messageHandler = this._messageHandlers.get(action);
if (typeof messageHandler === 'undefined') { return false; }
const {handler, async} = messageHandler;
try {
const promise = handler(params, sender);
promise.then(
const promiseOrResult = handler(params, sender);
if (async) {
promiseOrResult.then(
(result) => callback({result}),
(error) => callback({error: errorToJson(error)})
);
return true;
} else {
callback({result: promiseOrResult});
return false;
}
} catch (error) {
callback({error: errorToJson(error)});
return false;
@ -312,27 +319,26 @@ class Backend {
_onApiYomichanCoreReady(_params, sender) {
// tab ID isn't set in background (e.g. browser_action)
if (typeof sender.tab === 'undefined') {
const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.runtime.sendMessage({action: 'backendPrepared'}, callback);
return Promise.resolve();
const data = {action: 'backendPrepared'};
if (typeof sender.tab === 'undefined') {
chrome.runtime.sendMessage(data, callback);
return false;
} else {
chrome.tabs.sendMessage(sender.tab.id, data, callback);
return true;
}
}
const tabId = sender.tab.id;
return new Promise((resolve) => {
chrome.tabs.sendMessage(tabId, {action: 'backendPrepared'}, resolve);
});
}
async _onApiOptionsSchemaGet() {
_onApiOptionsSchemaGet() {
return this.getOptionsSchema();
}
async _onApiOptionsGet({optionsContext}) {
_onApiOptionsGet({optionsContext}) {
return this.getOptions(optionsContext);
}
async _onApiOptionsGetFull() {
_onApiOptionsGetFull() {
return this.getFullOptions();
}
@ -539,7 +545,7 @@ class Backend {
return this._renderTemplate(template, data);
}
async _onApiCommandExec({command, params}) {
_onApiCommandExec({command, params}) {
return this._runCommand(command, params);
}
@ -559,15 +565,15 @@ class Backend {
});
}
_onApiForward({action, params}, sender) {
_onApiBroadcastTab({action, params}, sender) {
if (!(sender && sender.tab)) {
return Promise.resolve();
return false;
}
const tabId = sender.tab.id;
return new Promise((resolve) => {
chrome.tabs.sendMessage(tabId, {action, params}, (response) => resolve(response));
});
const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.tabs.sendMessage(tabId, {action, params}, callback);
return true;
}
_onApiFrameInformationGet(params, sender) {
@ -690,11 +696,11 @@ class Backend {
});
}
async _onApiGetMessageToken() {
_onApiGetMessageToken() {
return this.messageToken;
}
async _onApiGetDefaultAnkiFieldTemplates() {
_onApiGetDefaultAnkiFieldTemplates() {
return this.defaultAnkiFieldTemplates;
}

View File

@ -17,7 +17,7 @@
/* global
* Display
* apiForward
* apiBroadcastTab
* apiGetMessageToken
* popupNestedInitialize
*/
@ -79,7 +79,7 @@ class DisplayFloat extends Display {
this.setContentScale(scale);
apiForward('popupPrepareCompleted', {targetPopupId: this._popupId});
apiBroadcastTab('popupPrepareCompleted', {targetPopupId: this._popupId});
}
onError(error) {
@ -180,7 +180,7 @@ class DisplayFloat extends Display {
},
2000
);
apiForward('requestDocumentInformationBroadcast', {uniqueId});
apiBroadcastTab('requestDocumentInformationBroadcast', {uniqueId});
const {title} = await promise;
return title;

View File

@ -16,7 +16,7 @@
*/
/* global
* apiForward
* apiBroadcastTab
*/
class FrameOffsetForwarder {
@ -96,6 +96,6 @@ class FrameOffsetForwarder {
}
_forwardFrameOffsetOrigin(offset, uniqueId) {
apiForward('frameOffset', {offset, uniqueId});
apiBroadcastTab('frameOffset', {offset, uniqueId});
}
}

View File

@ -20,7 +20,7 @@
* Frontend
* PopupProxy
* PopupProxyHost
* apiForward
* apiBroadcastTab
* apiOptionsGet
*/
@ -43,7 +43,7 @@ async function main() {
}
}
);
apiForward('rootPopupRequestInformationBroadcast');
apiBroadcastTab('rootPopupRequestInformationBroadcast');
const {popupId, frameId} = await rootPopupInformationPromise;
const frameOffsetForwarder = new FrameOffsetForwarder();

View File

@ -17,7 +17,7 @@
/* global
* TextScanner
* apiForward
* apiBroadcastTab
* apiGetZoom
* apiKanjiFind
* apiOptionsGet
@ -260,12 +260,12 @@ class Frontend extends TextScanner {
_broadcastRootPopupInformation() {
if (!this.popup.isProxy() && this.popup.depth === 0) {
apiForward('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId});
apiBroadcastTab('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId});
}
}
_broadcastDocumentInformation(uniqueId) {
apiForward('documentInformationBroadcast', {
apiBroadcastTab('documentInformationBroadcast', {
uniqueId,
frameId: this.popup.frameId,
title: document.title

View File

@ -80,8 +80,8 @@ function apiScreenshotGet(options) {
return _apiInvoke('screenshotGet', {options});
}
function apiForward(action, params) {
return _apiInvoke('forward', {action, params});
function apiBroadcastTab(action, params) {
return _apiInvoke('broadcastTab', {action, params});
}
function apiFrameInformationGet() {

View File

@ -22,9 +22,9 @@
* DisplayGenerator
* WindowScroll
* apiAudioGetUri
* apiBroadcastTab
* apiDefinitionAdd
* apiDefinitionsAddable
* apiForward
* apiKanjiFind
* apiNoteView
* apiOptionsGet
@ -854,7 +854,7 @@ class Display {
}
setPopupVisibleOverride(visible) {
return apiForward('popupSetVisibleOverride', {visible});
return apiBroadcastTab('popupSetVisibleOverride', {visible});
}
setSpinnerVisible(visible) {