[MessageHandler] Convert the code to a proper class

This commit is contained in:
Jonas Jenwald 2019-10-30 22:05:25 +01:00
parent 5d5733c0a7
commit 0293222b96

View File

@ -61,7 +61,8 @@ function wrapReason(reason) {
}
}
function MessageHandler(sourceName, targetName, comObj) {
class MessageHandler {
constructor(sourceName, targetName, comObj) {
this.sourceName = sourceName;
this.targetName = targetName;
this.comObj = comObj;
@ -134,9 +135,8 @@ function MessageHandler(sourceName, targetName, comObj) {
action(data.data);
};
comObj.addEventListener('message', this._onComObjOnMessage);
}
}
MessageHandler.prototype = {
on(actionName, handler) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
@ -148,7 +148,8 @@ MessageHandler.prototype = {
throw new Error(`There is already an actionName called "${actionName}"`);
}
ah[actionName] = handler;
},
}
/**
* Sends a message to the comObj to invoke the action with the supplied data.
* @param {string} actionName - Action to call.
@ -156,13 +157,14 @@ MessageHandler.prototype = {
* @param {Array} [transfers] - List of transfers/ArrayBuffers.
*/
send(actionName, data, transfers) {
this.postMessage({
this._postMessage({
sourceName: this.sourceName,
targetName: this.targetName,
action: actionName,
data,
}, transfers);
},
}
/**
* Sends a message to the comObj to invoke the action with the supplied data.
* Expects that the other side will callback with the response.
@ -176,7 +178,7 @@ MessageHandler.prototype = {
const capability = createPromiseCapability();
this.callbackCapabilities[callbackId] = capability;
try {
this.postMessage({
this._postMessage({
sourceName: this.sourceName,
targetName: this.targetName,
action: actionName,
@ -187,7 +189,8 @@ MessageHandler.prototype = {
capability.reject(ex);
}
return capability.promise;
},
}
/**
* Sends a message to the comObj to invoke the action with the supplied data.
* Expect that the other side will callback to signal 'start_complete'.
@ -214,7 +217,7 @@ MessageHandler.prototype = {
cancelCall: null,
isClosed: false,
};
this.postMessage({
this._postMessage({
sourceName,
targetName,
action: actionName,
@ -257,8 +260,11 @@ MessageHandler.prototype = {
return cancelCapability.promise;
},
}, queueingStrategy);
},
}
/**
* @private
*/
_createStreamSink(data) {
const self = this;
const action = this.actionHandler[data.action];
@ -281,7 +287,7 @@ MessageHandler.prototype = {
this.sinkCapability = createPromiseCapability();
this.ready = this.sinkCapability.promise;
}
self.postMessage({
self._postMessage({
sourceName,
targetName,
stream: StreamKind.ENQUEUE,
@ -349,8 +355,11 @@ MessageHandler.prototype = {
reason: wrapReason(reason),
});
});
},
}
/**
* @private
*/
_processStreamMessage(data) {
const streamId = data.streamId;
const sourceName = this.sourceName;
@ -482,8 +491,11 @@ MessageHandler.prototype = {
default:
throw new Error('Unexpected stream case');
}
},
}
/**
* @private
*/
async _deleteStreamController(streamId) {
// Delete the `streamController` only when the start, pull, and cancel
// capabilities have settled, to prevent `TypeError`s.
@ -495,26 +507,26 @@ MessageHandler.prototype = {
return capability && capability.promise.catch(function() { });
}));
delete this.streamControllers[streamId];
},
}
/**
* Sends raw message to the comObj.
* @private
* @param {Object} message - Raw message.
* @param transfers List of transfers/ArrayBuffers, or undefined.
* @private
*/
postMessage(message, transfers) {
_postMessage(message, transfers) {
if (transfers && this.postMessageTransfers) {
this.comObj.postMessage(message, transfers);
} else {
this.comObj.postMessage(message);
}
},
}
destroy() {
this.comObj.removeEventListener('message', this._onComObjOnMessage);
},
};
}
}
export {
MessageHandler,