diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-04-07 19:41:02 -0400 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-04-07 19:41:02 -0400 |
commit | 82462edce0215e4de14ce7d9348304b2c2ca6a4d (patch) | |
tree | fd36bf58808b17cf7a8ecd15154da7271e4110d0 | |
parent | f177e3699ae85bafa3a648f5c03fbb8e303a6be3 (diff) |
Add support for API handlers to be optionally asynchronous
-rw-r--r-- | ext/bg/js/backend.js | 77 |
1 files changed, 42 insertions, 35 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 1fa7ede1..ed4a24ed 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -77,33 +77,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: true}], + ['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: true}], + ['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: true}], + ['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: true}], + ['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: true}], + ['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}], + ['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}], + ['forward', {handler: this._onApiForward.bind(this), async: true}], + ['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: true}], + ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: true}] ]); this._commandHandlers = new Map([ @@ -167,16 +167,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( - (result) => callback({result}), - (error) => callback({error: errorToJson(error)}) - ); - return true; + 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; |