From 59c224d99d7c871b7864da5cd09ec60dc729e3d9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 10 Jul 2020 22:12:14 -0400 Subject: [PATCH] Add support for async vs non-async (#656) --- ext/fg/js/frontend.js | 31 +++++++++++++++++++++++-------- 1 file 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}) {