Fix cross frame comm issues (#765)

* Send ack before sending error response

* Fix error response not being JSON'ified

* Use _sendResult
This commit is contained in:
toasted-nutbread 2020-09-04 17:59:38 -04:00 committed by GitHub
parent 95bfe2d901
commit cf35b9338f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -171,13 +171,14 @@ class CrossFrameAPIPort extends EventDispatcher {
// Invocation // Invocation
_onInvoke(id, {action, params}) { _onInvoke(id, {action, params}) {
const callback = (response) => this._sendResponse({type: 'result', id, data: response});
const messageHandler = this._messageHandlers.get(action); const messageHandler = this._messageHandlers.get(action);
this._sendAck(id);
if (typeof messageHandler === 'undefined') { if (typeof messageHandler === 'undefined') {
callback({error: new Error(`Unknown action: ${action}`)}); this._sendError(id, new Error(`Unknown action: ${action}`));
return false; return false;
} }
this._sendAck(id);
const callback = (data) => this._sendResult(id, data);
return yomichan.invokeMessageHandler(messageHandler, params, callback); return yomichan.invokeMessageHandler(messageHandler, params, callback);
} }
@ -194,8 +195,8 @@ class CrossFrameAPIPort extends EventDispatcher {
this._sendResponse({type: 'ack', id}); this._sendResponse({type: 'ack', id});
} }
_sendResult(id, result) { _sendResult(id, data) {
this._sendResponse({type: 'result', id, data: {result}}); this._sendResponse({type: 'result', id, data});
} }
_sendError(id, error) { _sendError(id, error) {