Add support for API handlers to be optionally asynchronous

This commit is contained in:
toasted-nutbread 2020-04-07 19:41:02 -04:00
parent f177e3699a
commit 82462edce0

View File

@ -77,33 +77,33 @@ class Backend {
this.messageToken = yomichan.generateId(16); this.messageToken = yomichan.generateId(16);
this._messageHandlers = new Map([ this._messageHandlers = new Map([
['yomichanCoreReady', this._onApiYomichanCoreReady.bind(this)], ['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: true}],
['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)], ['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: true}],
['optionsGet', this._onApiOptionsGet.bind(this)], ['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: true}],
['optionsGetFull', this._onApiOptionsGetFull.bind(this)], ['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: true}],
['optionsSet', this._onApiOptionsSet.bind(this)], ['optionsSet', {handler: this._onApiOptionsSet.bind(this), async: true}],
['optionsSave', this._onApiOptionsSave.bind(this)], ['optionsSave', {handler: this._onApiOptionsSave.bind(this), async: true}],
['kanjiFind', this._onApiKanjiFind.bind(this)], ['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}],
['termsFind', this._onApiTermsFind.bind(this)], ['termsFind', {handler: this._onApiTermsFind.bind(this), async: true}],
['textParse', this._onApiTextParse.bind(this)], ['textParse', {handler: this._onApiTextParse.bind(this), async: true}],
['textParseMecab', this._onApiTextParseMecab.bind(this)], ['textParseMecab', {handler: this._onApiTextParseMecab.bind(this), async: true}],
['definitionAdd', this._onApiDefinitionAdd.bind(this)], ['definitionAdd', {handler: this._onApiDefinitionAdd.bind(this), async: true}],
['definitionsAddable', this._onApiDefinitionsAddable.bind(this)], ['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}],
['noteView', this._onApiNoteView.bind(this)], ['noteView', {handler: this._onApiNoteView.bind(this), async: true}],
['templateRender', this._onApiTemplateRender.bind(this)], ['templateRender', {handler: this._onApiTemplateRender.bind(this), async: true}],
['commandExec', this._onApiCommandExec.bind(this)], ['commandExec', {handler: this._onApiCommandExec.bind(this), async: true}],
['audioGetUri', this._onApiAudioGetUri.bind(this)], ['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}],
['screenshotGet', this._onApiScreenshotGet.bind(this)], ['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}],
['forward', this._onApiForward.bind(this)], ['forward', {handler: this._onApiForward.bind(this), async: true}],
['frameInformationGet', this._onApiFrameInformationGet.bind(this)], ['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}],
['injectStylesheet', this._onApiInjectStylesheet.bind(this)], ['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}],
['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)], ['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}],
['clipboardGet', this._onApiClipboardGet.bind(this)], ['clipboardGet', {handler: this._onApiClipboardGet.bind(this), async: true}],
['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)], ['getDisplayTemplatesHtml', {handler: this._onApiGetDisplayTemplatesHtml.bind(this), async: true}],
['getQueryParserTemplatesHtml', this._onApiGetQueryParserTemplatesHtml.bind(this)], ['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}],
['getZoom', this._onApiGetZoom.bind(this)], ['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}],
['getMessageToken', this._onApiGetMessageToken.bind(this)], ['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: true}],
['getDefaultAnkiFieldTemplates', this._onApiGetDefaultAnkiFieldTemplates.bind(this)] ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: true}]
]); ]);
this._commandHandlers = new Map([ this._commandHandlers = new Map([
@ -167,16 +167,23 @@ class Backend {
} }
onMessage({action, params}, sender, callback) { onMessage({action, params}, sender, callback) {
const handler = this._messageHandlers.get(action); const messageHandler = this._messageHandlers.get(action);
if (typeof handler !== 'function') { return false; } if (typeof messageHandler === 'undefined') { return false; }
const {handler, async} = messageHandler;
try { try {
const promise = handler(params, sender); const promiseOrResult = handler(params, sender);
promise.then( if (async) {
promiseOrResult.then(
(result) => callback({result}), (result) => callback({result}),
(error) => callback({error: errorToJson(error)}) (error) => callback({error: errorToJson(error)})
); );
return true; return true;
} else {
callback({result: promiseOrResult});
return false;
}
} catch (error) { } catch (error) {
callback({error: errorToJson(error)}); callback({error: errorToJson(error)});
return false; return false;