[MessageHandler] Remove unnecessary usage of in from the code

Note that using `in` leads to unnecessary stringification of the properties, which seems completely unnecessary here. To avoid future problems from these changes the `MessageHandler.on` method will now assert, in non-`PRODUCTION`/`TESTING` builds, that it's always called with a function as expected.

This patch also renames `callbacksCapabilities` to `callbackCapabilities`, note the removed "s", since using a double plural format looks a bit strange.
This commit is contained in:
Jonas Jenwald 2019-10-30 11:44:59 +01:00
parent 3e46e800a0
commit 62f28e11a3

View File

@ -69,8 +69,8 @@ function MessageHandler(sourceName, targetName, comObj) {
this.postMessageTransfers = true; this.postMessageTransfers = true;
this.streamSinks = Object.create(null); this.streamSinks = Object.create(null);
this.streamControllers = Object.create(null); this.streamControllers = Object.create(null);
let callbacksCapabilities = this.callbacksCapabilities = Object.create(null); this.callbackCapabilities = Object.create(null);
let ah = this.actionHandler = Object.create(null); const ah = this.actionHandler = Object.create(null);
this._onComObjOnMessage = (event) => { this._onComObjOnMessage = (event) => {
let data = event.data; let data = event.data;
@ -80,23 +80,24 @@ function MessageHandler(sourceName, targetName, comObj) {
if (data.stream) { if (data.stream) {
this._processStreamMessage(data); this._processStreamMessage(data);
} else if (data.callback) { } else if (data.callback) {
let callbackId = data.callbackId; const callbackId = data.callbackId;
if (data.callbackId in callbacksCapabilities) { const capability = this.callbackCapabilities[callbackId];
let callback = callbacksCapabilities[callbackId];
delete callbacksCapabilities[callbackId]; if (capability) {
delete this.callbackCapabilities[callbackId];
if (data.callback === CallbackKind.DATA) { if (data.callback === CallbackKind.DATA) {
callback.resolve(data.data); capability.resolve(data.data);
} else if (data.callback === CallbackKind.ERROR) { } else if (data.callback === CallbackKind.ERROR) {
callback.reject(wrapReason(data.reason)); capability.reject(wrapReason(data.reason));
} else { } else {
throw new Error('Unexpected callback case'); throw new Error('Unexpected callback case');
} }
} else { } else {
throw new Error(`Cannot resolve callback ${callbackId}`); throw new Error(`Cannot resolve callback ${callbackId}`);
} }
} else if (data.action in ah) { } else if (ah[data.action]) {
let action = ah[data.action]; const action = ah[data.action];
if (data.callbackId) { if (data.callbackId) {
let sourceName = this.sourceName; let sourceName = this.sourceName;
let targetName = data.sourceName; let targetName = data.sourceName;
@ -133,7 +134,12 @@ function MessageHandler(sourceName, targetName, comObj) {
MessageHandler.prototype = { MessageHandler.prototype = {
on(actionName, handler) { on(actionName, handler) {
var ah = this.actionHandler; if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(typeof handler === 'function',
'MessageHandler.on: Expected "handler" to be a function.');
}
const ah = this.actionHandler;
if (ah[actionName]) { if (ah[actionName]) {
throw new Error(`There is already an actionName called "${actionName}"`); throw new Error(`There is already an actionName called "${actionName}"`);
} }
@ -164,7 +170,7 @@ MessageHandler.prototype = {
sendWithPromise(actionName, data, transfers) { sendWithPromise(actionName, data, transfers) {
var callbackId = this.callbackId++; var callbackId = this.callbackId++;
var capability = createPromiseCapability(); var capability = createPromiseCapability();
this.callbacksCapabilities[callbackId] = capability; this.callbackCapabilities[callbackId] = capability;
try { try {
this.postMessage({ this.postMessage({
sourceName: this.sourceName, sourceName: this.sourceName,