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._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,13 +209,28 @@ 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);
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}) {
this._pageZoomFactor = newZoomFactor;