From cd82b81bc797b3664ec1aa63afcd9b1e536563fb Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 1 Sep 2019 12:25:31 +0200 Subject: [PATCH] Inline the `resolveCall` helper function at its call-sites in `MessageHandler` 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. --- src/shared/message_handler.js | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/shared/message_handler.js b/src/shared/message_handler.js index c148598af6c08..2a7a64d0b712e 100644 --- a/src/shared/message_handler.js +++ b/src/shared/message_handler.js @@ -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; @@ -100,9 +93,9 @@ function MessageHandler(sourceName, targetName, comObj) { if (data.callbackId) { let sourceName = this.sourceName; let targetName = data.sourceName; - Promise.resolve().then(function() { - return action(data.data); - }).then((result) => { + new Promise(function(resolve) { + resolve(action(data.data)); + }).then(function(result) { comObj.postMessage({ sourceName, targetName, @@ -110,7 +103,7 @@ function MessageHandler(sourceName, targetName, comObj) { callbackId: data.callbackId, data: result, }); - }, (reason) => { + }, function(reason) { comObj.postMessage({ sourceName, targetName, @@ -318,7 +311,9 @@ 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, @@ -326,7 +321,7 @@ MessageHandler.prototype = { streamId, success: true, }); - }, (reason) => { + }, function(reason) { comObj.postMessage({ sourceName, targetName, @@ -385,7 +380,10 @@ 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, @@ -393,7 +391,7 @@ MessageHandler.prototype = { streamId, success: true, }); - }, (reason) => { + }, function(reason) { comObj.postMessage({ sourceName, targetName, @@ -435,8 +433,10 @@ 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, @@ -444,7 +444,7 @@ MessageHandler.prototype = { streamId, success: true, }); - }, (reason) => { + }, function(reason) { comObj.postMessage({ sourceName, targetName,