Remove support for the scope parameter in the MessageHandler.on method

At this point in time it's easy to convert the `MessageHandler.on` call-sites to use arrow functions, and thus let the JavaScript engine handle scopes for us, rather than having to manually keep references to the relevant scopes in `MessageHandler`.[1]
An additional benefit of this is that a couple of `Function.prototype.call()` instances can now be converted into "normal" function calls, which should be a tiny bit more efficient.

All in all, I don't see any compelling reason why it'd be necessary to keep supporting custom `scope`s in the `MessageHandler` implementation.

---
[1] In the event that a custom scope is ever needed, simply using `bind` on the handler function when calling `MessageHandler.on` ought to work as well.
This commit is contained in:
Jonas Jenwald 2019-08-31 14:16:06 +02:00
parent d1e6d427cd
commit 055f03938b
2 changed files with 40 additions and 41 deletions

View file

@ -30,11 +30,11 @@ const StreamKind = {
START_COMPLETE: 8,
};
async function resolveCall(fn, args, thisArg = null) {
async function resolveCall(fn, args) {
if (!fn) {
return undefined;
}
return fn.apply(thisArg, args);
return fn.apply(null, args);
}
function wrapReason(reason) {
@ -100,8 +100,8 @@ function MessageHandler(sourceName, targetName, comObj) {
if (data.callbackId) {
let sourceName = this.sourceName;
let targetName = data.sourceName;
Promise.resolve().then(function () {
return action[0].call(action[1], data.data);
Promise.resolve().then(function() {
return action(data.data);
}).then((result) => {
comObj.postMessage({
sourceName,
@ -122,7 +122,7 @@ function MessageHandler(sourceName, targetName, comObj) {
} else if (data.streamId) {
this._createStreamSink(data);
} else {
action[0].call(action[1], data.data);
action(data.data);
}
} else {
throw new Error(`Unknown action from worker: ${data.action}`);
@ -132,12 +132,12 @@ function MessageHandler(sourceName, targetName, comObj) {
}
MessageHandler.prototype = {
on(actionName, handler, scope) {
on(actionName, handler) {
var ah = this.actionHandler;
if (ah[actionName]) {
throw new Error(`There is already an actionName called "${actionName}"`);
}
ah[actionName] = [handler, scope];
ah[actionName] = handler;
},
/**
* Sends a message to the comObj to invoke the action with the supplied data.
@ -318,7 +318,7 @@ MessageHandler.prototype = {
streamSink.sinkCapability.resolve();
streamSink.ready = streamSink.sinkCapability.promise;
this.streamSinks[streamId] = streamSink;
resolveCall(action[0], [data.data, streamSink], action[1]).then(() => {
resolveCall(action, [data.data, streamSink]).then(() => {
comObj.postMessage({
sourceName,
targetName,