[MessageHandler] Replace the internal isReply property, as sent when Promise callbacks are used, with enumeration values

Given that the `isReply` property is an internal implementation detail, changing its type shouldn't be a problem. Note that by directly indicating if either data or an Error is sent, it's no longer necessary to use `in` when handling the callback.
This commit is contained in:
Jonas Jenwald 2019-10-25 13:37:28 +02:00
parent 72bd8e8bdb
commit 3e46e800a0

View File

@ -18,6 +18,12 @@ import {
ReadableStream, UnexpectedResponseException, UnknownErrorException ReadableStream, UnexpectedResponseException, UnknownErrorException
} from './util'; } from './util';
const CallbackKind = {
UNKNOWN: 0,
DATA: 1,
ERROR: 2,
};
const StreamKind = { const StreamKind = {
UNKNOWN: 0, UNKNOWN: 0,
CANCEL: 1, CANCEL: 1,
@ -73,15 +79,18 @@ function MessageHandler(sourceName, targetName, comObj) {
} }
if (data.stream) { if (data.stream) {
this._processStreamMessage(data); this._processStreamMessage(data);
} else if (data.isReply) { } else if (data.callback) {
let callbackId = data.callbackId; let callbackId = data.callbackId;
if (data.callbackId in callbacksCapabilities) { if (data.callbackId in callbacksCapabilities) {
let callback = callbacksCapabilities[callbackId]; let callback = callbacksCapabilities[callbackId];
delete callbacksCapabilities[callbackId]; delete callbacksCapabilities[callbackId];
if ('reason' in data) {
if (data.callback === CallbackKind.DATA) {
callback.resolve(data.data);
} else if (data.callback === CallbackKind.ERROR) {
callback.reject(wrapReason(data.reason)); callback.reject(wrapReason(data.reason));
} else { } else {
callback.resolve(data.data); throw new Error('Unexpected callback case');
} }
} else { } else {
throw new Error(`Cannot resolve callback ${callbackId}`); throw new Error(`Cannot resolve callback ${callbackId}`);
@ -97,7 +106,7 @@ function MessageHandler(sourceName, targetName, comObj) {
comObj.postMessage({ comObj.postMessage({
sourceName, sourceName,
targetName, targetName,
isReply: true, callback: CallbackKind.DATA,
callbackId: data.callbackId, callbackId: data.callbackId,
data: result, data: result,
}); });
@ -105,7 +114,7 @@ function MessageHandler(sourceName, targetName, comObj) {
comObj.postMessage({ comObj.postMessage({
sourceName, sourceName,
targetName, targetName,
isReply: true, callback: CallbackKind.ERROR,
callbackId: data.callbackId, callbackId: data.callbackId,
reason: wrapReason(reason), reason: wrapReason(reason),
}); });