Add support for async vs non-async (#656)

This commit is contained in:
toasted-nutbread 2020-07-10 22:12:14 -04:00 committed by GitHub
parent f76a6ff1e3
commit 59c224d99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,9 +63,9 @@ class Frontend {
this._updatePopupToken = null; this._updatePopupToken = null;
this._runtimeMessageHandlers = new Map([ this._runtimeMessageHandlers = new Map([
['popupSetVisibleOverride', this._onMessagePopupSetVisibleOverride.bind(this)], ['popupSetVisibleOverride', {async: false, handler: this._onMessagePopupSetVisibleOverride.bind(this)}],
['rootPopupRequestInformationBroadcast', this._onMessageRootPopupRequestInformationBroadcast.bind(this)], ['rootPopupRequestInformationBroadcast', {async: false, handler: this._onMessageRootPopupRequestInformationBroadcast.bind(this)}],
['requestDocumentInformationBroadcast', this._onMessageRequestDocumentInformationBroadcast.bind(this)] ['requestDocumentInformationBroadcast', {async: false, handler: this._onMessageRequestDocumentInformationBroadcast.bind(this)}]
]); ]);
} }
@ -209,12 +209,27 @@ class Frontend {
} }
_onRuntimeMessage({action, params}, sender, callback) { _onRuntimeMessage({action, params}, sender, callback) {
const handler = this._runtimeMessageHandlers.get(action); const messageHandler = this._runtimeMessageHandlers.get(action);
if (typeof handler !== 'function') { return false; } if (typeof messageHandler === 'undefined') { return false; }
const result = handler(params, sender); const {handler, async} = messageHandler;
callback(result);
return false; 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}) { _onZoomChanged({newZoomFactor}) {