Skip to content

Commit

Permalink
Inline the resolveCall helper function at its call-sites in `Messag…
Browse files Browse the repository at this point in the history
…eHandler`

There's only three call-sites and one of them doesn't even need the complete functionality of `resolveCall`, hence it seems reasonable to just inline this code.
An additional benefit of this is that the `Function.prototype.apply()` instance can also be converted into "normal" function calls, which should be a tiny bit more efficient.

The patch also replaces a number of unnecessary arrow functions, in relevant parts of the `MessageHandler` code, with "normal" functions instead.
Finally, all `Promise.resolve().then(...)` calls are replaced with `new Promise(...)` instead since the latter is a tiny bit more efficient. This also explains the test failures on the Linux bot, with a prior version of the patch, since the `Promise.resolve().then(...)` format essentially creates two Promises thus causing additional delay.
  • Loading branch information
Snuffleupagus committed Sep 1, 2019
1 parent 10165c0 commit 76a1d6c
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/shared/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ const StreamKind = {
START_COMPLETE: 8,
};

async function resolveCall(fn, args) {
if (!fn) {
return undefined;
}
return fn.apply(null, args);
}

function wrapReason(reason) {
if (typeof reason !== 'object') {
return reason;
Expand Down Expand Up @@ -100,8 +93,8 @@ function MessageHandler(sourceName, targetName, comObj) {
if (data.callbackId) {
let sourceName = this.sourceName;
let targetName = data.sourceName;
Promise.resolve().then(function() {
return action(data.data);
new Promise(function(resolve) {
resolve(action(data.data));
}).then((result) => {
comObj.postMessage({
sourceName,
Expand Down Expand Up @@ -318,15 +311,17 @@ MessageHandler.prototype = {
streamSink.sinkCapability.resolve();
streamSink.ready = streamSink.sinkCapability.promise;
this.streamSinks[streamId] = streamSink;
resolveCall(action, [data.data, streamSink]).then(() => {
new Promise(function(resolve) {
resolve(action(data.data, streamSink));
}).then(function() {
comObj.postMessage({
sourceName,
targetName,
stream: StreamKind.START_COMPLETE,
streamId,
success: true,
});
}, (reason) => {
}, function(reason) {
comObj.postMessage({
sourceName,
targetName,
Expand Down Expand Up @@ -385,15 +380,18 @@ MessageHandler.prototype = {
}
// Reset desiredSize property of sink on every pull.
this.streamSinks[data.streamId].desiredSize = data.desiredSize;
resolveCall(this.streamSinks[data.streamId].onPull).then(() => {
const { onPull, } = this.streamSinks[data.streamId];
new Promise(function(resolve) {
resolve(onPull && onPull());
}).then(function() {
comObj.postMessage({
sourceName,
targetName,
stream: StreamKind.PULL_COMPLETE,
streamId,
success: true,
});
}, (reason) => {
}, function(reason) {
comObj.postMessage({
sourceName,
targetName,
Expand Down Expand Up @@ -435,16 +433,18 @@ MessageHandler.prototype = {
if (!this.streamSinks[data.streamId]) {
break;
}
resolveCall(this.streamSinks[data.streamId].onCancel,
[wrapReason(data.reason)]).then(() => {
const { onCancel, } = this.streamSinks[data.streamId];
new Promise(function(resolve) {
resolve(onCancel && onCancel(wrapReason(data.reason)));
}).then(function() {
comObj.postMessage({
sourceName,
targetName,
stream: StreamKind.CANCEL_COMPLETE,
streamId,
success: true,
});
}, (reason) => {
}, function(reason) {
comObj.postMessage({
sourceName,
targetName,
Expand Down

0 comments on commit 76a1d6c

Please sign in to comment.