[MessageHandler] Re-factor the _onComObjOnMessage function to use early returns

When `ReadableStream` support was added to the `MessageHandler`, the `_onComObjOnMessage` function became more complex than previously.
All of the nested `if`/`else if`/`else` branches are now, at least in my opinion, making some of this code a bit difficult to follow. Hence this patch, which attempts to help readability by making use of early `return`s and `Error`s.

The patch also changes a couple of `var`/`let` occurences to `const`.
This commit is contained in:
Jonas Jenwald 2019-10-30 12:12:50 +01:00
parent 62f28e11a3
commit f61fb3e0f9

View File

@ -70,20 +70,23 @@ function MessageHandler(sourceName, targetName, comObj) {
this.streamSinks = Object.create(null); this.streamSinks = Object.create(null);
this.streamControllers = Object.create(null); this.streamControllers = Object.create(null);
this.callbackCapabilities = Object.create(null); this.callbackCapabilities = Object.create(null);
const ah = this.actionHandler = Object.create(null); this.actionHandler = Object.create(null);
this._onComObjOnMessage = (event) => { this._onComObjOnMessage = (event) => {
let data = event.data; const data = event.data;
if (data.targetName !== this.sourceName) { if (data.targetName !== this.sourceName) {
return; return;
} }
if (data.stream) { if (data.stream) {
this._processStreamMessage(data); this._processStreamMessage(data);
} else if (data.callback) { return;
}
if (data.callback) {
const callbackId = data.callbackId; const callbackId = data.callbackId;
const capability = this.callbackCapabilities[callbackId]; const capability = this.callbackCapabilities[callbackId];
if (!capability) {
if (capability) { throw new Error(`Cannot resolve callback ${callbackId}`);
}
delete this.callbackCapabilities[callbackId]; delete this.callbackCapabilities[callbackId];
if (data.callback === CallbackKind.DATA) { if (data.callback === CallbackKind.DATA) {
@ -93,14 +96,15 @@ function MessageHandler(sourceName, targetName, comObj) {
} else { } else {
throw new Error('Unexpected callback case'); throw new Error('Unexpected callback case');
} }
} else { return;
throw new Error(`Cannot resolve callback ${callbackId}`); }
const action = this.actionHandler[data.action];
if (!action) {
throw new Error(`Unknown action from worker: ${data.action}`);
} }
} else if (ah[data.action]) {
const action = ah[data.action];
if (data.callbackId) { if (data.callbackId) {
let sourceName = this.sourceName; const sourceName = this.sourceName;
let targetName = data.sourceName; const targetName = data.sourceName;
new Promise(function(resolve) { new Promise(function(resolve) {
resolve(action(data.data)); resolve(action(data.data));
}).then(function(result) { }).then(function(result) {
@ -120,14 +124,13 @@ function MessageHandler(sourceName, targetName, comObj) {
reason: wrapReason(reason), reason: wrapReason(reason),
}); });
}); });
} else if (data.streamId) { return;
}
if (data.streamId) {
this._createStreamSink(data); this._createStreamSink(data);
} else { return;
}
action(data.data); action(data.data);
}
} else {
throw new Error(`Unknown action from worker: ${data.action}`);
}
}; };
comObj.addEventListener('message', this._onComObjOnMessage); comObj.addEventListener('message', this._onComObjOnMessage);
} }