diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-07-10 22:12:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-10 22:12:14 -0400 |
commit | 59c224d99d7c871b7864da5cd09ec60dc729e3d9 (patch) | |
tree | 66e6357c3e0f876321a7c58b3b2b854f1781b933 /ext/fg/js | |
parent | f76a6ff1e320646098620a6109919ddb8dbcf747 (diff) |
Add support for async vs non-async (#656)
Diffstat (limited to 'ext/fg/js')
-rw-r--r-- | ext/fg/js/frontend.js | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 86dcf57a..67dbcf4b 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -63,9 +63,9 @@ class Frontend { this._updatePopupToken = null; this._runtimeMessageHandlers = new Map([ - ['popupSetVisibleOverride', this._onMessagePopupSetVisibleOverride.bind(this)], - ['rootPopupRequestInformationBroadcast', this._onMessageRootPopupRequestInformationBroadcast.bind(this)], - ['requestDocumentInformationBroadcast', this._onMessageRequestDocumentInformationBroadcast.bind(this)] + ['popupSetVisibleOverride', {async: false, handler: this._onMessagePopupSetVisibleOverride.bind(this)}], + ['rootPopupRequestInformationBroadcast', {async: false, handler: this._onMessageRootPopupRequestInformationBroadcast.bind(this)}], + ['requestDocumentInformationBroadcast', {async: false, handler: this._onMessageRequestDocumentInformationBroadcast.bind(this)}] ]); } @@ -209,12 +209,27 @@ class Frontend { } _onRuntimeMessage({action, params}, sender, callback) { - const handler = this._runtimeMessageHandlers.get(action); - if (typeof handler !== 'function') { return false; } + const messageHandler = this._runtimeMessageHandlers.get(action); + if (typeof messageHandler === 'undefined') { return false; } - const result = handler(params, sender); - callback(result); - return false; + const {handler, async} = messageHandler; + + try { + 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; + } } _onZoomChanged({newZoomFactor}) { |